Commit 10fb3372 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

generic errors

parent 6264e980
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()))
......
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()))
......
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));
}
}
......@@ -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.TeamRepository;
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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment