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.TutorDao;
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.TutorResource;
public class ObservationsApplication extends Application<ObservationsConfiguration> {
@ -48,7 +48,7 @@ public class ObservationsApplication extends Application<ObservationsConfigurati
TutorDao tutorDao = new TutorDao(hibernate.getSessionFactory());
SiteDao siteDao = new SiteDao(hibernate.getSessionFactory());
ObservationDao observationDao = new ObservationDao(hibernate.getSessionFactory());
final TutorResource tutorResource = new TutorResource(tutorDao);
final TutorResource tutorResource = new TutorResource(tutorDao, siteDao);
jersey.register(tutorResource);
final ObservationResource observationResource = new ObservationResource(observationDao, tutorDao, siteDao);
jersey.register(observationResource);

View File

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

View File

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

View File

@ -1,20 +1,18 @@
package uk.co.neviyn.Observations.resources;
import java.util.List;
import java.util.stream.Collectors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.hibernate.UnitOfWork;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor;
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.Response;
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;
import java.util.List;
@RequiredArgsConstructor
@Produces(MediaType.APPLICATION_JSON)
@ -22,6 +20,15 @@ import uk.co.neviyn.Observations.dao.TutorDao;
public class TutorResource {
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")
@GET
@ -32,4 +39,12 @@ public class TutorResource {
return tutors;
throw new WebApplicationException("No tutors found!", Response.Status.NOT_FOUND);
}
@AllArgsConstructor
private class NewTutor {
@JsonProperty
final long siteId;
@JsonProperty
final String name;
}
}