Fixed adding observations, added listing of tutor observations.

This commit is contained in:
neviyn 2018-09-15 21:15:48 +01:00
parent 64c828b87e
commit e4120b1ca3
3 changed files with 25 additions and 19 deletions

View File

@ -10,7 +10,7 @@ import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext; import org.jdbi.v3.core.statement.StatementContext;
import org.joda.time.LocalDate; import org.joda.time.DateTime;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ -25,10 +25,6 @@ public class Observation {
@JsonProperty @JsonProperty
private int siteId; private int siteId;
@NonNull
@JsonProperty
private int tutorManyId;
@NonNull @NonNull
@JsonProperty @JsonProperty
private String observed; private String observed;
@ -43,12 +39,11 @@ public class Observation {
@NonNull @NonNull
@JsonProperty @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) { int teamwork, int knowledge, String rawData) {
this.siteId = siteId; this.siteId = siteId;
this.tutorManyId = tutorManyId;
this.observed = observed; this.observed = observed;
this.monitoring = monitoring; this.monitoring = monitoring;
this.control = control; this.control = control;
@ -56,15 +51,15 @@ public class Observation {
this.teamwork = teamwork; this.teamwork = teamwork;
this.knowledge = knowledge; this.knowledge = knowledge;
this.rawData = rawData; this.rawData = rawData;
this.date = LocalDate.now(); this.date = DateTime.now();
} }
public static class Mapper implements RowMapper<Observation>{ public static class Mapper implements RowMapper<Observation>{
public Observation map(ResultSet rs, StatementContext ctx) throws SQLException { 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("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")));
} }
} }
} }

View File

@ -6,7 +6,7 @@ import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean; import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.SqlQuery; import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate; 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.api.NewObservation;
import uk.co.neviyn.Observations.core.Observation; 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, " + @SqlUpdate("INSERT INTO observations (siteId, observed, monitoring, control, conservatism, teamwork, knowledge, " +
"rawData, date) VALUES (:siteId, :observed, :monitoring, :control, :conservatism, :teamwork, :knowledge, " + "rawData, date) VALUES (:siteId, :observed, :monitoring, :control, :conservatism, :teamwork, :knowledge, " +
":rawData, :date)") ":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)") @SqlUpdate("INSERT INTO observation_tutor (tutorId, observationId) VALUES (:tutorId, :observationId)")
void addObservationTutor(@Bind("observationId")int observationId, @Bind("tutorId")int tutorId); void addObservationTutor(@Bind("observationId")int observationId, @Bind("tutorId")int tutorId);
@SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate") @SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate")
List<Observation> observationsBetweenDates(@Bind("startDate")LocalDate startDate, @Bind("endDate")LocalDate endDate); List<Observation> observationsBetweenDates(@Bind("startDate")DateTime startDate, @Bind("endDate")DateTime endDate);
@SqlQuery("SELECT * FROM observations LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " + @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<Observation> observationsByTutor(@Bind("tutorId")int tutorId); List<Observation> observationsByTutor(@Bind("tutorId")int tutorId);
@SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate " + @SqlQuery("SELECT * FROM observations WHERE date BETWEEN :startDate AND :endDate " +
"LEFT JOIN observation_tutor ON observation_tutor.observationId = observations.id " + "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<Observation> observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")LocalDate startDate, List<Observation> observationsByTutorBetweenDates(@Bind("tutorId")int tutorId, @Bind("startDate")DateTime startDate,
@Bind("endDate")LocalDate endDate); @Bind("endDate")DateTime endDate);
} }

View File

@ -1,15 +1,20 @@
package uk.co.neviyn.Observations.resources; package uk.co.neviyn.Observations.resources;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import uk.co.neviyn.Observations.api.NewObservation; import uk.co.neviyn.Observations.api.NewObservation;
import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.dao.ObservationDao; import uk.co.neviyn.Observations.dao.ObservationDao;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -23,10 +28,16 @@ public class ObservationResource {
@POST @POST
public int add(@NotNull @Valid NewObservation observation){ public int add(@NotNull @Valid NewObservation observation){
int observationId = dao.addObservation(observation); int observationId = dao.addObservation(observation, DateTime.now());
for(int tutorId: observation.getTutorIds()){ for(int tutorId: observation.getTutorIds()){
dao.addObservationTutor(observationId, tutorId); dao.addObservationTutor(observationId, tutorId);
} }
return observationId; return observationId;
} }
@Path("/by/{tutorId}")
@GET
public List<Observation> observationsBy(@PathParam("tutorId") int tutorId){
return dao.observationsByTutor(tutorId);
}
} }