Added ability to create new observations.

This commit is contained in:
neviyn 2018-09-15 20:36:04 +01:00
parent 9de27e9998
commit 41875b0c5f
4 changed files with 79 additions and 0 deletions

View File

@ -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<ObservationsConfiguration> {
@ -41,5 +43,7 @@ public class ObservationsApplication extends Application<ObservationsConfigurati
observationDao.createObservationTutorTable();
final TutorResource tutorResource = new TutorResource(tutorDao);
jersey.register(tutorResource);
final ObservationResource observationResource = new ObservationResource(observationDao);
jersey.register(observationResource);
}
}

View File

@ -0,0 +1,33 @@
package uk.co.neviyn.Observations.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data
@NoArgsConstructor
public class NewObservation {
@NonNull
@JsonProperty
private int siteId;
@NonNull
@JsonProperty
private List<Integer> tutorIds;
@NonNull
@JsonProperty
private String observed;
@NonNull
@JsonProperty
private int monitoring, control, conservatism, teamwork, knowledge;
@NonNull
@JsonProperty
private String rawData;
}

View File

@ -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<Observation> observationsBetweenDates(@Bind("startDate")LocalDate startDate, @Bind("endDate")LocalDate endDate);

View File

@ -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;
}
}