Started Sites and Tutors.
This commit is contained in:
parent
cd340f3032
commit
05b2b32895
6
backend/configuration.yml
Normal file
6
backend/configuration.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
database:
|
||||||
|
driverClass: org.h2.Driver
|
||||||
|
url: jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
|
||||||
|
|
||||||
|
server:
|
||||||
|
rootPath: /api/
|
@ -24,7 +24,6 @@
|
|||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<version>1.18.2</version>
|
<version>1.18.2</version>
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.dropwizard</groupId>
|
<groupId>io.dropwizard</groupId>
|
||||||
@ -41,6 +40,17 @@
|
|||||||
<artifactId>dropwizard-assets</artifactId>
|
<artifactId>dropwizard-assets</artifactId>
|
||||||
<version>${dropwizard.version}</version>
|
<version>${dropwizard.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.dropwizard</groupId>
|
||||||
|
<artifactId>dropwizard-testing</artifactId>
|
||||||
|
<version>${dropwizard.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.197</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -150,6 +160,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<arguments>
|
<arguments>
|
||||||
<argument>server</argument>
|
<argument>server</argument>
|
||||||
|
<argument>configuration.yml</argument>
|
||||||
</arguments>
|
</arguments>
|
||||||
<mainClass>uk.co.neviyn.Observations.ObservationsApplication</mainClass>
|
<mainClass>uk.co.neviyn.Observations.ObservationsApplication</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
@ -2,8 +2,14 @@ package uk.co.neviyn.Observations;
|
|||||||
|
|
||||||
import io.dropwizard.Application;
|
import io.dropwizard.Application;
|
||||||
import io.dropwizard.assets.AssetsBundle;
|
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.Bootstrap;
|
||||||
import io.dropwizard.setup.Environment;
|
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<ObservationsConfiguration> {
|
public class ObservationsApplication extends Application<ObservationsConfiguration> {
|
||||||
|
|
||||||
@ -19,6 +25,16 @@ public class ObservationsApplication extends Application<ObservationsConfigurati
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ObservationsConfiguration observationsConfiguration, Environment environment) throws Exception {
|
public void run(ObservationsConfiguration observationsConfiguration, Environment environment) throws Exception {
|
||||||
environment.jersey().setUrlPattern("/api/*");
|
final JerseyEnvironment jersey = environment.jersey();
|
||||||
|
final JdbiFactory factory = new JdbiFactory();
|
||||||
|
final Jdbi jdbi = factory.build(environment, observationsConfiguration.getDataSourceFactory(), "database");
|
||||||
|
TutorDao tutorDao = jdbi.onDemand(TutorDao.class);
|
||||||
|
SiteDao siteDao = jdbi.onDemand(SiteDao.class);
|
||||||
|
jersey.register(tutorDao);
|
||||||
|
jersey.register(siteDao);
|
||||||
|
siteDao.createSiteTable();
|
||||||
|
tutorDao.createTutorTable();
|
||||||
|
final TutorResource tutorResource = new TutorResource(tutorDao);
|
||||||
|
jersey.register(tutorResource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,24 @@
|
|||||||
package uk.co.neviyn.Observations;
|
package uk.co.neviyn.Observations;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import io.dropwizard.Configuration;
|
import io.dropwizard.Configuration;
|
||||||
|
import io.dropwizard.db.DataSourceFactory;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class ObservationsConfiguration extends Configuration {
|
public class ObservationsConfiguration extends Configuration {
|
||||||
|
@Valid
|
||||||
|
@NotNull
|
||||||
|
private DataSourceFactory database = new DataSourceFactory();
|
||||||
|
|
||||||
|
@JsonProperty("database")
|
||||||
|
public void setDataSourceFactory(DataSourceFactory factory) {
|
||||||
|
this.database = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("database")
|
||||||
|
public DataSourceFactory getDataSourceFactory() {
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package uk.co.neviyn.Observations.core;
|
||||||
|
|
||||||
|
public class Observation {
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
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 Site {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@JsonProperty
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@JsonProperty
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public static class Mapper implements RowMapper<Site> {
|
||||||
|
public Site map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
|
return new Site(rs.getInt("id"), rs.getString("name"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<Tutor>{
|
||||||
|
|
||||||
|
public Tutor map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
|
return new Tutor(rs.getInt("id"), rs.getString("name"), rs.getInt("site"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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<Site> allSites();
|
||||||
|
|
||||||
|
@SqlQuery("SELECT * FROM site WHERE id = :id")
|
||||||
|
Site getSiteById(@Bind("id") int id);
|
||||||
|
}
|
@ -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<Tutor> tutorsForSite(@Bind("siteId")int siteId);
|
||||||
|
|
||||||
|
@SqlQuery("SELECT * FROM tutor")
|
||||||
|
List<Tutor> allTutors();
|
||||||
|
|
||||||
|
}
|
@ -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<Tutor> allTutors(){
|
||||||
|
List<Tutor> tutors = dao.allTutors();
|
||||||
|
if(tutors != null && !tutors.isEmpty())
|
||||||
|
return tutors;
|
||||||
|
throw new WebApplicationException("No tutors found!", Response.Status.NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user