diff --git a/backend/configuration.yml b/backend/configuration.yml new file mode 100644 index 0000000..9302209 --- /dev/null +++ b/backend/configuration.yml @@ -0,0 +1,6 @@ +database: + driverClass: org.h2.Driver + url: jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 + +server: + rootPath: /api/ \ No newline at end of file diff --git a/backend/pom.xml b/backend/pom.xml index aca56ce..5f1d79d 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -24,7 +24,6 @@ org.projectlombok lombok 1.18.2 - provided io.dropwizard @@ -41,6 +40,17 @@ dropwizard-assets ${dropwizard.version} + + io.dropwizard + dropwizard-testing + ${dropwizard.version} + test + + + com.h2database + h2 + 1.4.197 + @@ -150,6 +160,7 @@ server + configuration.yml uk.co.neviyn.Observations.ObservationsApplication 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 bbb9376..735613e 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/ObservationsApplication.java @@ -2,8 +2,14 @@ package uk.co.neviyn.Observations; import io.dropwizard.Application; import io.dropwizard.assets.AssetsBundle; +import io.dropwizard.jdbi3.JdbiFactory; +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.SiteDao; +import uk.co.neviyn.Observations.dao.TutorDao; +import uk.co.neviyn.Observations.resources.TutorResource; public class ObservationsApplication extends Application { @@ -19,6 +25,16 @@ public class ObservationsApplication extends Application { + public Site map(ResultSet rs, StatementContext ctx) throws SQLException { + return new Site(rs.getInt("id"), rs.getString("name")); + } + } +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java b/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java new file mode 100644 index 0000000..894794b --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java @@ -0,0 +1,37 @@ +package uk.co.neviyn.Observations.core; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.sql.ResultSet; +import java.sql.SQLException; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import org.jdbi.v3.core.mapper.RowMapper; +import org.jdbi.v3.core.statement.StatementContext; + +@Data +@NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PROTECTED) +public class Tutor { + + @NonNull + @JsonProperty + private int id; + + @NonNull + @JsonProperty + private String name; + + @NonNull + @JsonProperty + private int site; + + public static class Mapper implements RowMapper{ + + public Tutor map(ResultSet rs, StatementContext ctx) throws SQLException { + return new Tutor(rs.getInt("id"), rs.getString("name"), rs.getInt("site")); + } + } +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/dao/SiteDao.java b/backend/src/main/java/uk/co/neviyn/Observations/dao/SiteDao.java new file mode 100644 index 0000000..12b2843 --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/SiteDao.java @@ -0,0 +1,21 @@ +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 uk.co.neviyn.Observations.core.Site; + +@RegisterRowMapper(Site.Mapper.class) +public interface SiteDao { + + @SqlUpdate("CREATE TABLE site (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100))") + void createSiteTable(); + + @SqlQuery("SELECT * FROM site") + List allSites(); + + @SqlQuery("SELECT * FROM site WHERE id = :id") + Site getSiteById(@Bind("id") int id); +} 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 new file mode 100644 index 0000000..0f5fc9e --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/TutorDao.java @@ -0,0 +1,27 @@ +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 uk.co.neviyn.Observations.core.Tutor; + +@RegisterRowMapper(Tutor.Mapper.class) +public interface TutorDao { + + @SqlUpdate("CREATE TABLE tutor (id INTEGER PRIMARY KEY AUTO_INCREMENT, " + + "name VARCHAR(100), site INT, FOREIGN KEY (site) REFERENCES site(id))") + void createTutorTable(); + + @SqlUpdate("INSERT INTO tutor(name, site) VALUES (:name, :site)") + void add(@BindBean Tutor tutor); + + @SqlQuery("SELECT * FROM tutor WHERE site = :siteId") + List tutorsForSite(@Bind("siteId")int siteId); + + @SqlQuery("SELECT * FROM tutor") + List allTutors(); + +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java b/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java new file mode 100644 index 0000000..32c5774 --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java @@ -0,0 +1,31 @@ +package uk.co.neviyn.Observations.resources; + +import java.util.List; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import uk.co.neviyn.Observations.core.Tutor; +import uk.co.neviyn.Observations.dao.TutorDao; + +@RequiredArgsConstructor +@Produces(MediaType.APPLICATION_JSON) +@Path("/tutor") +@Slf4j +public class TutorResource { + + private final TutorDao dao; + + @Path("/all") + @GET + public List allTutors(){ + List tutors = dao.allTutors(); + if(tutors != null && !tutors.isEmpty()) + return tutors; + throw new WebApplicationException("No tutors found!", Response.Status.NOT_FOUND); + } +}