From 9de27e999877ff847f78d2d35057bd52b791099a Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sat, 15 Sep 2018 19:55:35 +0100 Subject: [PATCH] Added Observations. --- .../Observations/ObservationsApplication.java | 5 ++ .../neviyn/Observations/core/Observation.java | 66 +++++++++++++++++++ .../Observations/dao/ObservationDao.java | 35 ++++++++++ .../co/neviyn/Observations/dao/TutorDao.java | 4 +- 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.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 735613e..ff045df 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java @@ -7,6 +7,7 @@ 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.dao.ObservationDao; import uk.co.neviyn.Observations.dao.SiteDao; import uk.co.neviyn.Observations.dao.TutorDao; import uk.co.neviyn.Observations.resources.TutorResource; @@ -30,10 +31,14 @@ public class ObservationsApplication extends Application{ + + public Observation map(ResultSet rs, StatementContext ctx) throws SQLException { + return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getInt("tutorManyId"), 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"))); + } + } } 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 new file mode 100644 index 0000000..e92e029 --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java @@ -0,0 +1,35 @@ +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.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; +import org.joda.time.LocalDate; +import uk.co.neviyn.Observations.core.Observation; + +@RegisterRowMapper(Observation.Mapper.class) +public interface ObservationDao { + + @SqlUpdate("CREATE TABLE observations (id INTEGER PRIMARY KEY AUTO_INCREMENT, siteId INTEGER, tutorManyId INTEGER, " + + "observed TEXT, monitoring INTEGER, control INTEGER, conservatism INTEGER, teamwork INTEGER, knowledge INTEGER, " + + "rawData TEXT, date DATE)") + void createObservationTable(); + + @SqlUpdate("CREATE TABLE observation_tutor (tutorId INT NOT NULL, observationId INT NOT NULL, " + + "FOREIGN KEY (tutorID) REFERENCES tutor(id), FOREIGN KEY (observationId) REFERENCES observations(id))") + void createObservationTutorTable(); + + @SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate") + List observationsBetweenDates(@Bind("startDate")LocalDate startDate, @Bind("endDate")LocalDate 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") + 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); +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/dao/TutorDao.java b/backend/src/main/java/uk/co/neviyn/Observations/dao/TutorDao.java index 0f5fc9e..f47d06e 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/dao/TutorDao.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/TutorDao.java @@ -4,6 +4,7 @@ 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.GetGeneratedKeys; import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlUpdate; import uk.co.neviyn.Observations.core.Tutor; @@ -16,7 +17,8 @@ public interface TutorDao { void createTutorTable(); @SqlUpdate("INSERT INTO tutor(name, site) VALUES (:name, :site)") - void add(@BindBean Tutor tutor); + @GetGeneratedKeys("id") + int add(@BindBean Tutor tutor); @SqlQuery("SELECT * FROM tutor WHERE site = :siteId") List tutorsForSite(@Bind("siteId")int siteId);