diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java b/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java index 291d9f9..862e377 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java @@ -10,7 +10,7 @@ import lombok.NoArgsConstructor; import lombok.NonNull; import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.statement.StatementContext; -import org.joda.time.LocalDate; +import org.joda.time.DateTime; @Data @NoArgsConstructor @@ -25,10 +25,6 @@ public class Observation { @JsonProperty private int siteId; - @NonNull - @JsonProperty - private int tutorManyId; - @NonNull @JsonProperty private String observed; @@ -43,12 +39,11 @@ public class Observation { @NonNull @JsonProperty - private LocalDate date; + private DateTime date; - public Observation(int siteId, int tutorManyId, String observed, int monitoring, int control, int conservatism, + public Observation(int siteId, String observed, int monitoring, int control, int conservatism, int teamwork, int knowledge, String rawData) { this.siteId = siteId; - this.tutorManyId = tutorManyId; this.observed = observed; this.monitoring = monitoring; this.control = control; @@ -56,15 +51,15 @@ public class Observation { this.teamwork = teamwork; this.knowledge = knowledge; this.rawData = rawData; - this.date = LocalDate.now(); + this.date = DateTime.now(); } public static class Mapper implements RowMapper{ public Observation map(ResultSet rs, StatementContext ctx) throws SQLException { - return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getInt("tutorManyId"), rs.getString("observed"), + return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getString("observed"), rs.getInt("monitoring"), rs.getInt("control"), rs.getInt("conservatism"), rs.getInt("teamwork"), - rs.getInt("knowledge"), rs.getString("rawData"), new LocalDate(rs.getDate("date"))); + rs.getInt("knowledge"), rs.getString("rawData"), new DateTime(rs.getDate("date"))); } } } diff --git a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java index a719344..cf0a513 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java @@ -6,7 +6,7 @@ import org.jdbi.v3.sqlobject.customizer.Bind; import org.jdbi.v3.sqlobject.customizer.BindBean; import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlUpdate; -import org.joda.time.LocalDate; +import org.joda.time.DateTime; import uk.co.neviyn.Observations.api.NewObservation; import uk.co.neviyn.Observations.core.Observation; @@ -25,21 +25,21 @@ public interface ObservationDao { @SqlUpdate("INSERT INTO observations (siteId, observed, monitoring, control, conservatism, teamwork, knowledge, " + "rawData, date) VALUES (:siteId, :observed, :monitoring, :control, :conservatism, :teamwork, :knowledge, " + ":rawData, :date)") - int addObservation(@BindBean NewObservation observation); + int addObservation(@BindBean NewObservation observation, @Bind("date") DateTime date); @SqlUpdate("INSERT INTO observation_tutor (tutorId, observationId) VALUES (:tutorId, :observationId)") void addObservationTutor(@Bind("observationId")int observationId, @Bind("tutorId")int tutorId); @SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate") - List observationsBetweenDates(@Bind("startDate")LocalDate startDate, @Bind("endDate")LocalDate endDate); + List observationsBetweenDates(@Bind("startDate")DateTime startDate, @Bind("endDate")DateTime endDate); @SqlQuery("SELECT * FROM observations LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " + - "LEFT JOIN tutor observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId") + "LEFT JOIN tutor ON observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId") List observationsByTutor(@Bind("tutorId")int tutorId); @SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate " + "LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " + - "LEFT JOIN tutor observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId") - List observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")LocalDate startDate, - @Bind("endDate")LocalDate endDate); + "LEFT JOIN tutor ON observation_tutor.tutorId = tutor.id WHERE tutor.id = :tutorId") + List observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")DateTime startDate, + @Bind("endDate")DateTime endDate); } diff --git a/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java b/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java index 20b8b74..f05a331 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java @@ -1,15 +1,20 @@ package uk.co.neviyn.Observations.resources; +import java.util.List; import javax.validation.Valid; import javax.validation.constraints.NotNull; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; import uk.co.neviyn.Observations.api.NewObservation; +import uk.co.neviyn.Observations.core.Observation; import uk.co.neviyn.Observations.dao.ObservationDao; @RequiredArgsConstructor @@ -23,10 +28,16 @@ public class ObservationResource { @POST public int add(@NotNull @Valid NewObservation observation){ - int observationId = dao.addObservation(observation); + int observationId = dao.addObservation(observation, DateTime.now()); for(int tutorId: observation.getTutorIds()){ dao.addObservationTutor(observationId, tutorId); } return observationId; } + + @Path("/by/{tutorId}") + @GET + public List observationsBy(@PathParam("tutorId") int tutorId){ + return dao.observationsByTutor(tutorId); + } }