Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CTF
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grzegorz
CTF
Commits
10fb3372
Commit
10fb3372
authored
Nov 12, 2016
by
Grzegorz Pietrusza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generic errors
parent
6264e980
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
8 deletions
+37
-8
ElementAlreadyExistsException.java
...yournet/ctf/exceptions/ElementAlreadyExistsException.java
+2
-2
ElementDoesNotExistException.java
...kyournet/ctf/exceptions/ElementDoesNotExistException.java
+2
-2
TeamRepository.java
...ephoners/krakyournet/ctf/repositories/TeamRepository.java
+23
-0
RegistrationResource.java
...oners/krakyournet/ctf/resources/RegistrationResource.java
+10
-4
No files found.
service/src/main/java/com/telephoners/krakyournet/ctf/exceptions/ElementAlreadyExistsException.java
View file @
10fb3372
package
com
.
telephoners
.
krakyournet
.
ctf
.
exceptions
;
import
com.telephoners.krakyournet.ctf.beans.User
;
import
org.ektorp.support.CouchDbDocument
;
import
javax.ws.rs.WebApplicationException
;
import
javax.ws.rs.core.Response
;
public
class
ElementAlreadyExistsException
extends
WebApplicationException
{
public
ElementAlreadyExistsException
(
final
Class
<
User
>
elementName
)
public
ElementAlreadyExistsException
(
final
Class
<
?
extends
CouchDbDocument
>
elementName
)
{
super
(
Response
.
status
(
Response
.
Status
.
BAD_REQUEST
)
.
entity
(
String
.
format
(
"Element %s already exists"
,
elementName
.
getSimpleName
()))
...
...
service/src/main/java/com/telephoners/krakyournet/ctf/exceptions/ElementDoesNotExistException.java
View file @
10fb3372
package
com
.
telephoners
.
krakyournet
.
ctf
.
exceptions
;
import
com.telephoners.krakyournet.ctf.beans.Team
;
import
org.ektorp.support.CouchDbDocument
;
import
javax.ws.rs.WebApplicationException
;
import
javax.ws.rs.core.Response
;
public
class
ElementDoesNotExistException
extends
WebApplicationException
{
public
ElementDoesNotExistException
(
final
Class
<
Team
>
elementName
)
public
ElementDoesNotExistException
(
final
Class
<
?
extends
CouchDbDocument
>
elementName
)
{
super
(
Response
.
status
(
Response
.
Status
.
NOT_FOUND
)
.
entity
(
String
.
format
(
"Element %s does not exist"
,
elementName
.
getSimpleName
()))
...
...
service/src/main/java/com/telephoners/krakyournet/ctf/repositories/TeamRepository.java
0 → 100644
View file @
10fb3372
package
com
.
telephoners
.
krakyournet
.
ctf
.
repositories
;
import
com.google.inject.Inject
;
import
com.telephoners.krakyournet.ctf.beans.Team
;
import
com.telephoners.krakyournet.ctf.core.DataConnector
;
public
class
TeamRepository
extends
Repository
<
Team
>
{
@Inject
public
TeamRepository
(
final
Class
<
Team
>
type
,
final
DataConnector
<
Team
>
connector
)
{
super
(
type
,
connector
);
}
//todo: this should be handled by driver itself
//todo: merge with User
public
boolean
exists
(
final
String
teamName
)
{
return
getAll
()
.
stream
()
.
anyMatch
(
team
->
team
.
getName
().
equals
(
teamName
));
}
}
service/src/main/java/com/telephoners/krakyournet/ctf/resources/RegistrationResource.java
View file @
10fb3372
...
...
@@ -4,7 +4,7 @@ import com.telephoners.krakyournet.ctf.beans.Team;
import
com.telephoners.krakyournet.ctf.beans.User
;
import
com.telephoners.krakyournet.ctf.exceptions.ElementAlreadyExistsException
;
import
com.telephoners.krakyournet.ctf.exceptions.ElementDoesNotExistException
;
import
com.telephoners.krakyournet.ctf.repositories.Repository
;
import
com.telephoners.krakyournet.ctf.repositories.
Team
Repository
;
import
com.telephoners.krakyournet.ctf.repositories.UserRepository
;
import
javax.inject.Inject
;
...
...
@@ -20,12 +20,12 @@ import javax.ws.rs.core.MediaType;
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
RegistrationResource
{
private
final
Repository
<
Team
>
teamsRepository
;
private
final
TeamRepository
teamsRepository
;
private
final
UserRepository
userRepository
;
@Inject
public
RegistrationResource
(
final
UserRepository
userRepository
,
final
Repository
<
Team
>
teamsRepository
)
final
TeamRepository
teamsRepository
)
{
//todo: checkNotNUll
this
.
teamsRepository
=
teamsRepository
;
...
...
@@ -36,7 +36,8 @@ public class RegistrationResource
@Path
(
"/team"
)
public
String
registerTeam
(
@QueryParam
(
"teamName"
)
final
String
teamName
)
{
//todo: check if exists
validateTeamName
(
teamName
);
Team
team
=
new
Team
();
team
.
setName
(
teamName
);
team
.
setDescription
(
"descr"
);
...
...
@@ -64,6 +65,11 @@ public class RegistrationResource
return
user
.
getId
();
}
private
void
validateTeamName
(
final
String
teamName
)
{
if
(
teamsRepository
.
exists
(
teamName
))
throw
new
ElementAlreadyExistsException
(
Team
.
class
);
}
private
void
validateTeam
(
final
String
teamId
)
{
if
(
teamsRepository
.
contains
(
teamId
))
throw
new
ElementDoesNotExistException
(
Team
.
class
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment