diff --git a/src/main/java/io/zipcoder/persistenceapp/PersistenceStarterApplication.java b/src/main/java/io/zipcoder/persistenceapp/PersistenceStarterApplication.java index 1f1e185..83c8b18 100644 --- a/src/main/java/io/zipcoder/persistenceapp/PersistenceStarterApplication.java +++ b/src/main/java/io/zipcoder/persistenceapp/PersistenceStarterApplication.java @@ -1,10 +1,12 @@ package io.zipcoder.persistenceapp; import org.h2.server.web.WebServlet; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; +import org.springframework.jdbc.core.JdbcTemplate; @SpringBootApplication public class PersistenceStarterApplication { @@ -19,4 +21,7 @@ ServletRegistrationBean h2servletRegistration(){ registrationBean.addUrlMappings("/console/*"); return registrationBean; } + + @Autowired + JdbcTemplate jdbcTemplate; } diff --git a/src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java b/src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java new file mode 100644 index 0000000..c2a1a44 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/controller/PersonController.java @@ -0,0 +1,38 @@ +package io.zipcoder.persistenceapp.controller; + +import io.zipcoder.persistenceapp.domain.Person; +import io.zipcoder.persistenceapp.service.PersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class PersonController { + + + @Autowired + private PersonService personService; + + @RequestMapping(value = "/people", method = RequestMethod.POST) + public ResponseEntity createPerson(@RequestBody Person person) { + personService.addPerson(person); + return new ResponseEntity<>(HttpStatus.OK); + } + +// @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT) +// public ResponseEntity updatePerson(@PathVariable Integer id, @RequestBody Person person) { +// Person person = +// } + + @RequestMapping(value = "/people/{id}", method = RequestMethod.GET) + public ResponseEntity lookUpId(@PathVariable int id) { + Person person = personService.findById(id); + return new ResponseEntity<>(person, HttpStatus.OK); + } + + + + + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/domain/Home.java b/src/main/java/io/zipcoder/persistenceapp/domain/Home.java new file mode 100644 index 0000000..197ae99 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Home.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp.domain; + +import javax.persistence.Entity; + +@Entity +public class Home { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/domain/Person.java b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java new file mode 100644 index 0000000..68ccbdb --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java @@ -0,0 +1,68 @@ +package io.zipcoder.persistenceapp.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import java.util.Date; + +@Entity +public class Person { + + @Id + @GeneratedValue + private int id; + private String firstName; + private String lastName; + private String mobile; + private Date birthday; + private int homeId; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMobile() { + return mobile; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public int getHomeId() { + return homeId; + } + + public void setHomeId(int homeId) { + this.homeId = homeId; + } + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/repository/HomeRepository.java b/src/main/java/io/zipcoder/persistenceapp/repository/HomeRepository.java new file mode 100644 index 0000000..6c14e64 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/repository/HomeRepository.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp.repository; + +import io.zipcoder.persistenceapp.domain.Home; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface HomeRepository extends JpaRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/repository/PersonRepository.java b/src/main/java/io/zipcoder/persistenceapp/repository/PersonRepository.java new file mode 100644 index 0000000..93e8e91 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/repository/PersonRepository.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp.repository; + +import io.zipcoder.persistenceapp.domain.Person; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PersonRepository extends JpaRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/service/HomeService.java b/src/main/java/io/zipcoder/persistenceapp/service/HomeService.java new file mode 100644 index 0000000..9c334a9 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/HomeService.java @@ -0,0 +1,18 @@ +package io.zipcoder.persistenceapp.service; + +import io.zipcoder.persistenceapp.domain.Person; + +import java.util.List; + +public interface HomeService { + + List getAllPeople(); + + void deletePerson(int id); + + void deletePeople(List cars); + + void addPerson(Person person); + + Person findById(int id); +} diff --git a/src/main/java/io/zipcoder/persistenceapp/service/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/service/PersonService.java new file mode 100644 index 0000000..9008167 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/PersonService.java @@ -0,0 +1,35 @@ +package io.zipcoder.persistenceapp.service; + +import io.zipcoder.persistenceapp.domain.Person; +import org.h2.engine.Database; +import org.springframework.stereotype.Service; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import java.util.List; + +public interface PersonService { + + List getAllPeople(); + + void deletePerson(int id); + + void deletePeople(List cars); + + void addPerson(Person person); + + Person findById(int id); + + + +// POST /people -- create a person +// PUT /people/{id} -- update person with id. 404 error if that person doesn't exist yet +// GET /people/{id} -- get the person with the specified ID +// DELETE /people/{id} -- Delete the person with the specified ID +// GET /people -- get all people in the database +// GET /people/reverselookup/{mobileNumber} -- find all people with the specified mobile number +// GET /people/surname/{lastName} -- Find all people with a particular last name +// GET /people/surname -- Get the result of the surname report above +// GET /people/firstname/stats -- Get the report of first name frequencies + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaHomeServiceImpl.java b/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaHomeServiceImpl.java new file mode 100644 index 0000000..1d3fe72 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaHomeServiceImpl.java @@ -0,0 +1,36 @@ +package io.zipcoder.persistenceapp.service.jpa; + +import io.zipcoder.persistenceapp.domain.Person; +import io.zipcoder.persistenceapp.service.HomeService; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class jpaHomeServiceImpl implements HomeService { + + @Override + public List getAllPeople() { + return null; + } + + @Override + public void deletePerson(int id) { + + } + + @Override + public void deletePeople(List cars) { + + } + + @Override + public void addPerson(Person person) { + + } + + @Override + public Person findById(int id) { + return null; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaPersonServiceImpl.java b/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaPersonServiceImpl.java new file mode 100644 index 0000000..2bc133c --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/jpa/jpaPersonServiceImpl.java @@ -0,0 +1,42 @@ +package io.zipcoder.persistenceapp.service.jpa; + +import io.zipcoder.persistenceapp.domain.Person; +import io.zipcoder.persistenceapp.repository.PersonRepository; +import io.zipcoder.persistenceapp.service.PersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class jpaPersonServiceImpl implements PersonService { + + @Autowired + private PersonRepository personRepository; + + @Override + public List getAllPeople() { + return null; + } + + @Override + public void deletePerson(int id) { + + } + + @Override + public void deletePeople(List cars) { + + } + + @Override + public void addPerson(Person person) { + + } + + @Override + public Person findById(int id) { + return null; + } + +} diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 39c2c27..8534d26 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -34,6 +34,16 @@ CREATE TABLE movies ( rating VARCHAR2(10) ); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Lavalantula', 83, 'Horror', 4.7, 'TV-14'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Waltz With Bashir', 90, 'Documentary', 8.0, 'R'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Spaceballs', 96, 'Comedy', 7.1, 'PG'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Monsters Inc.', 92, 'Animation', 8.1 'G'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Ready Player One', 120, 'Animation', 8.4, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('A Quiet Place', 105, 'Horror', 8.2, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Isle of Dogs', 95, 'Animation', 9.0, 'G'); + -- Tables for in-class example DROP TABLE IF EXISTS cars; diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..ed66f3a --- /dev/null +++ b/src/main/resources/script.sql @@ -0,0 +1,23 @@ +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Lavalantula', 83, 'Horror', 4.7, 'TV-14'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Waltz With Bashir', 90, 'Documentary', 8.0, 'R'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Spaceballs', 96, 'Comedy', 7.1, 'PG'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Monsters Inc.', 92, 'Animation', 8.1 'G'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Ready Player One', 120, 'Animation', 8.4, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('A Quiet Place', 105, 'Horror', 8.2, 'PG-13'); +INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Isle of Dogs', 95, 'Animation', 9.0, 'G'); + +SELECT * FROM Movies WHERE GENRE = 'Sci-Fi'; +SELECT * FROM Movies WHERE IMDB_SCORE > 6.4; +SELECT * FROM Movies WHERE RATING = 'G' AND RUNTIME < 100 OR RATING = 'PG' AND RUNTIME < 100; +SELECT AVG(RUNTIME) FROM Movies WHERE IMDB_SCORE < 7.5 GROUP BY GENRE; + +UPDATE Movies SET Rating = 'R' WHERE Title = 'Starship Troopers'; + +SELECT ID, Rating FROM Movies WHERE Genre = 'Horror' OR Genre = 'Documentary' +SELECT Rating, AVG(IMDB_Score), MIN(IMDB_SCORE), MAX(IMDB_SCORE) FROM Movies GROUP BY Rating; +SELECT Rating, AVG(IMDB_Score), MIN(IMDB_SCORE), MAX(IMDB_SCORE) FROM Movies GROUP BY Rating HAVING COUNT(RATING) > 1; + +DELETE FROM Movies WHERE Rating = 'R'; +