Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import io.smallrye.reactive.messaging.annotations.Blocking;
import org.eclipse.microprofile.opentracing.Traced;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.geoandri.developers.dto.TeamDto;
import org.geoandri.developers.entity.Team;
import org.geoandri.developers.event.TeamEvent;
import org.geoandri.developers.exception.EntityNotFoundException;
import org.geoandri.developers.exception.EntityPersistenceException;
import org.geoandri.developers.exception.TeamNotFoundException;
import org.geoandri.developers.mapper.TeamMapper;
import org.geoandri.developers.service.TeamService;
import org.slf4j.Logger;
Expand All @@ -33,32 +34,21 @@ public void consumeEvents(TeamEvent event) {
switch (event.getEventType()) {
case TEAM_CREATED: {
try {
teamService.save(teamMapper.toTeam(event.getTeamDto()));
teamService.save(teamMapper.toTeam(new TeamDto(event.getTeamId())));
} catch (EntityPersistenceException e) {
LOGGER.warn("Error while persisting team {}. The error was: {}", event.getTeamDto(), e.getMessage());
LOGGER.warn("Error while persisting team {}. The error was: {}", event.getTeamId(), e.getMessage());
}
break;
}
case TEAM_DELETED: {
Team team = teamMapper.toTeam(event.getTeamDto());
Team team = teamMapper.toTeam(new TeamDto(event.getTeamId()));
try {
teamService.delete(team.getId());
} catch (EntityNotFoundException e) {
} catch (TeamNotFoundException e) {
LOGGER.warn("Team {} could not be found.", team);
}
break;
}
case TEAM_UPDATED: {
Team team = teamMapper.toTeam(event.getTeamDto());
try {
teamService.update(team);
} catch (EntityNotFoundException e) {
LOGGER.warn("Team {} could not be found.", team);
} catch (EntityPersistenceException e) {
LOGGER.error(e.getMessage());
}
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.geoandri.developers.dao;

import org.geoandri.developers.entity.Developer;
import org.geoandri.developers.exception.EntityNotFoundException;
import org.geoandri.developers.exception.DeveloperNotFoundException;
import org.geoandri.developers.exception.EntityPersistenceException;

import javax.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -54,7 +54,7 @@ public Developer getDeveloper(long id) {
return developer;
}

throw new EntityNotFoundException(String.format("Developer with id %s could not be found.", id));
throw new DeveloperNotFoundException(String.format("Developer with id %s could not be found.", id));
}

public Developer updateDeveloper(Developer developer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.geoandri.developers.dao;

import org.geoandri.developers.entity.Team;
import org.geoandri.developers.exception.EntityNotFoundException;
import org.geoandri.developers.exception.EntityPersistenceException;
import org.geoandri.developers.exception.TeamNotFoundException;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import java.util.Optional;

@ApplicationScoped
public class TeamDao {
Expand All @@ -35,35 +33,11 @@ public Team getTeam(long id) {
return team;
}

throw new EntityNotFoundException(String.format("Team with id %s could not be found.", id));
}

public Team updateTeam(Team team) {
Team persistedTeam = getTeam(team.getId());
persistedTeam.setName(team.getName());
persistedTeam.setDescription(team.getDescription());
try {
entityManager.merge(persistedTeam);
entityManager.flush();

return persistedTeam;
}
catch (PersistenceException e) {
throw new EntityPersistenceException(e.getMessage());
}
throw new TeamNotFoundException(String.format("Team with id %s could not be found.", id));
}

public void deleteTeam(long id) {
Team team = getTeam(id);
entityManager.remove(team);
}

public Team findByName(String name) {
Query query = entityManager.createQuery("select t from Team t where t.name = :name");
query.setParameter("name", name);
Optional<Team> team = query.getResultList().stream().findFirst();

return team.orElseThrow(
() -> new EntityNotFoundException(String.format("Team with name %s could not be found.", name)));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.geoandri.developers.dto;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public class DeveloperDto {

Expand All @@ -9,8 +10,8 @@ public class DeveloperDto {
@NotEmpty(message = "Developer's name is required.")
private String name;

@NotEmpty(message = "Developer's team is required.")
private String team;
@NotNull(message = "Developer's teamId is required.")
private Long teamId;

public long getId() {
return id;
Expand All @@ -28,11 +29,11 @@ public void setName(String name) {
this.name = name;
}

public String getTeam() {
return team;
public long getTeamId() {
return teamId;
}

public void setTeam(String team) {
this.team = team;
public void setTeamId(long teamId) {
this.teamId = teamId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.util.Objects;

public class TeamDto {
private long id;

@NotEmpty(message = "Team's name is required.")
private String name;
public TeamDto(long id) {
this.id = id;
}

private String description;
private long id;

public long getId() {
return id;
Expand All @@ -19,41 +19,23 @@ public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "TeamDto{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TeamDto teamDto = (TeamDto) o;
return id == teamDto.id && name.equals(teamDto.name) && Objects.equals(description, teamDto.description);
return id == teamDto.id;
}

@Override
public int hashCode() {
return Objects.hash(id, name, description);
return Objects.hash(id);
}

@Override
public String toString() {
return "TeamDto{" +
"id=" + id +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public class Team {
@Id
private long id;

@NotEmpty
private String name;

private String description;

public long getId() {
return id;
}
Expand All @@ -22,28 +17,10 @@ public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "Team{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.geoandri.developers.event;

public enum EventType {
TEAM_CREATED, TEAM_UPDATED, TEAM_DELETED
TEAM_CREATED, TEAM_DELETED
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

public class TeamEvent {
private EventType eventType;
private TeamDto teamDto;
private long teamId;

public TeamEvent() {
}

public TeamEvent(EventType eventType, TeamDto teamDto) {
public TeamEvent(EventType eventType, long teamId) {
this.eventType = eventType;
this.teamDto = teamDto;
this.teamId = teamId;
}

public EventType getEventType() {
Expand All @@ -24,19 +24,19 @@ public void setEventType(EventType eventType) {
this.eventType = eventType;
}

public TeamDto getTeamDto() {
return teamDto;
public long getTeamId() {
return teamId;
}

public void setTeamDto(TeamDto teamDto) {
this.teamDto = teamDto;
public void setTeamId(long teamId) {
this.teamId = teamId;
}

@Override
public String toString() {
return "TeamEvent{" +
"eventType=" + eventType +
", teamDto=" + teamDto +
", teamId=" + teamId +
'}';
}

Expand All @@ -45,11 +45,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TeamEvent teamEvent = (TeamEvent) o;
return eventType == teamEvent.eventType && teamDto.equals(teamEvent.teamDto);
return teamId == teamEvent.teamId && eventType == teamEvent.eventType;
}

@Override
public int hashCode() {
return Objects.hash(eventType, teamDto);
return Objects.hash(eventType, teamId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.geoandri.developers.exception;

public class DeveloperNotFoundException extends RuntimeException {
public DeveloperNotFoundException() {
}

public DeveloperNotFoundException(String message) {
super(message);
}

public DeveloperNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.geoandri.developers.exception;

public class TeamNotFoundException extends RuntimeException {
public TeamNotFoundException() {
}

public TeamNotFoundException(String message) {
super(message);
}

public TeamNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.geoandri.developers.exception.handler;

import org.geoandri.developers.exception.EntityNotFoundException;
import org.geoandri.developers.exception.DeveloperNotFoundException;
import org.geoandri.developers.exception.error.ErrorMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -10,11 +10,11 @@
import javax.ws.rs.ext.Provider;

@Provider
public class EntityNotFoundExceptionHandler implements ExceptionMapper<EntityNotFoundException> {
private static final Logger LOGGER = LoggerFactory.getLogger(EntityNotFoundExceptionHandler.class);
public class DeveloperNotFoundExceptionHandler implements ExceptionMapper<DeveloperNotFoundException> {
private static final Logger LOGGER = LoggerFactory.getLogger(DeveloperNotFoundExceptionHandler.class);

@Override
public Response toResponse(EntityNotFoundException e) {
public Response toResponse(DeveloperNotFoundException e) {
LOGGER.warn(e.getMessage());

return Response.status(Response.Status.NOT_FOUND).entity(new ErrorMessage(e.getMessage())).build();
Expand Down
Loading