From 41875b0c5f29633e99158a43ecca0f62990a3b7c Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sat, 15 Sep 2018 20:36:04 +0100 Subject: [PATCH] Added ability to create new observations. --- .../Observations/ObservationsApplication.java | 4 +++ .../Observations/api/NewObservation.java | 33 +++++++++++++++++++ .../Observations/dao/ObservationDao.java | 10 ++++++ .../resources/ObservationResource.java | 32 ++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 backend/src/main/java/uk/co/neviyn/Observations/api/NewObservation.java create mode 100644 backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java diff --git a/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java b/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java index ff045df..9aaeea3 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java @@ -7,9 +7,11 @@ import io.dropwizard.jersey.setup.JerseyEnvironment; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import org.jdbi.v3.core.Jdbi; +import uk.co.neviyn.Observations.core.Observation; import uk.co.neviyn.Observations.dao.ObservationDao; import uk.co.neviyn.Observations.dao.SiteDao; import uk.co.neviyn.Observations.dao.TutorDao; +import uk.co.neviyn.Observations.resources.ObservationResource; import uk.co.neviyn.Observations.resources.TutorResource; public class ObservationsApplication extends Application { @@ -41,5 +43,7 @@ public class ObservationsApplication extends Application tutorIds; + + @NonNull + @JsonProperty + private String observed; + + @NonNull + @JsonProperty + private int monitoring, control, conservatism, teamwork, knowledge; + + @NonNull + @JsonProperty + private String rawData; + +} 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 e92e029..61dd3d8 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 @@ -3,9 +3,11 @@ package uk.co.neviyn.Observations.dao; import java.util.List; import org.jdbi.v3.sqlobject.config.RegisterRowMapper; 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 uk.co.neviyn.Observations.api.NewObservation; import uk.co.neviyn.Observations.core.Observation; @RegisterRowMapper(Observation.Mapper.class) @@ -20,6 +22,14 @@ public interface ObservationDao { "FOREIGN KEY (tutorID) REFERENCES tutor(id), FOREIGN KEY (observationId) REFERENCES observations(id))") void createObservationTutorTable(); + @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); + + @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); 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 new file mode 100644 index 0000000..20b8b74 --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/ObservationResource.java @@ -0,0 +1,32 @@ +package uk.co.neviyn.Observations.resources; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import uk.co.neviyn.Observations.api.NewObservation; +import uk.co.neviyn.Observations.dao.ObservationDao; + +@RequiredArgsConstructor +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Path("/observation") +@Slf4j +public class ObservationResource { + + private final ObservationDao dao; + + @POST + public int add(@NotNull @Valid NewObservation observation){ + int observationId = dao.addObservation(observation); + for(int tutorId: observation.getTutorIds()){ + dao.addObservationTutor(observationId, tutorId); + } + return observationId; + } +}