Can now add Sites and Tutors via resources.

This commit is contained in:
neviyn 2018-09-19 14:28:38 +01:00
parent 6a45056363
commit c8a8edbbc7
4 changed files with 40 additions and 19 deletions

View File

@ -14,8 +14,8 @@ import uk.co.neviyn.Observations.dao.ObservationDao;
import uk.co.neviyn.Observations.dao.SiteDao; import uk.co.neviyn.Observations.dao.SiteDao;
import uk.co.neviyn.Observations.dao.TutorDao; import uk.co.neviyn.Observations.dao.TutorDao;
import uk.co.neviyn.Observations.resources.ObservationResource; import uk.co.neviyn.Observations.resources.ObservationResource;
import uk.co.neviyn.Observations.resources.TutorResource;
import uk.co.neviyn.Observations.resources.SiteResource; import uk.co.neviyn.Observations.resources.SiteResource;
import uk.co.neviyn.Observations.resources.TutorResource;
public class ObservationsApplication extends Application<ObservationsConfiguration> { public class ObservationsApplication extends Application<ObservationsConfiguration> {
@ -48,7 +48,7 @@ public class ObservationsApplication extends Application<ObservationsConfigurati
TutorDao tutorDao = new TutorDao(hibernate.getSessionFactory()); TutorDao tutorDao = new TutorDao(hibernate.getSessionFactory());
SiteDao siteDao = new SiteDao(hibernate.getSessionFactory()); SiteDao siteDao = new SiteDao(hibernate.getSessionFactory());
ObservationDao observationDao = new ObservationDao(hibernate.getSessionFactory()); ObservationDao observationDao = new ObservationDao(hibernate.getSessionFactory());
final TutorResource tutorResource = new TutorResource(tutorDao); final TutorResource tutorResource = new TutorResource(tutorDao, siteDao);
jersey.register(tutorResource); jersey.register(tutorResource);
final ObservationResource observationResource = new ObservationResource(observationDao, tutorDao, siteDao); final ObservationResource observationResource = new ObservationResource(observationDao, tutorDao, siteDao);
jersey.register(observationResource); jersey.register(observationResource);

View File

@ -13,6 +13,8 @@ import java.util.Set;
@Table(name = "TUTOR") @Table(name = "TUTOR")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
@Builder
public class Tutor extends BaseEntity implements Serializable { public class Tutor extends BaseEntity implements Serializable {
@NonNull @NonNull

View File

@ -3,13 +3,11 @@ package uk.co.neviyn.Observations.resources;
import io.dropwizard.hibernate.UnitOfWork; import io.dropwizard.hibernate.UnitOfWork;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import uk.co.neviyn.Observations.api.SelectOption; import uk.co.neviyn.Observations.api.SelectOption;
import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor; import uk.co.neviyn.Observations.core.Tutor;
import uk.co.neviyn.Observations.dao.SiteDao; import uk.co.neviyn.Observations.dao.SiteDao;
import javax.ws.rs.GET; import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -22,6 +20,12 @@ public class SiteResource {
private final SiteDao dao; private final SiteDao dao;
@POST
@UnitOfWork
public Site add(String name) {
return dao.persist(new Site(name));
}
@Path("/all") @Path("/all")
@GET @GET
@UnitOfWork @UnitOfWork

View File

@ -1,20 +1,18 @@
package uk.co.neviyn.Observations.resources; package uk.co.neviyn.Observations.resources;
import java.util.List; import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.stream.Collectors; import io.dropwizard.hibernate.UnitOfWork;
import javax.ws.rs.GET; import lombok.AllArgsConstructor;
import javax.ws.rs.Path; import lombok.RequiredArgsConstructor;
import javax.ws.rs.PathParam; import uk.co.neviyn.Observations.core.Site;
import javax.ws.rs.Produces; import uk.co.neviyn.Observations.core.Tutor;
import javax.ws.rs.WebApplicationException; import uk.co.neviyn.Observations.dao.SiteDao;
import uk.co.neviyn.Observations.dao.TutorDao;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List;
import io.dropwizard.hibernate.UnitOfWork;
import lombok.RequiredArgsConstructor;
import uk.co.neviyn.Observations.api.SelectOption;
import uk.co.neviyn.Observations.core.Tutor;
import uk.co.neviyn.Observations.dao.TutorDao;
@RequiredArgsConstructor @RequiredArgsConstructor
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -22,6 +20,15 @@ import uk.co.neviyn.Observations.dao.TutorDao;
public class TutorResource { public class TutorResource {
private final TutorDao dao; private final TutorDao dao;
private final SiteDao siteDao;
@POST
@UnitOfWork
public Tutor add(NewTutor newTutor) {
final Site site = siteDao.get(newTutor.siteId);
final Tutor tutor = Tutor.builder().name(newTutor.name).site(site).build();
return dao.persist(tutor);
}
@Path("/all") @Path("/all")
@GET @GET
@ -32,4 +39,12 @@ public class TutorResource {
return tutors; return tutors;
throw new WebApplicationException("No tutors found!", Response.Status.NOT_FOUND); throw new WebApplicationException("No tutors found!", Response.Status.NOT_FOUND);
} }
@AllArgsConstructor
private class NewTutor {
@JsonProperty
final long siteId;
@JsonProperty
final String name;
}
} }