From 79d67037b444ba78e4b59216a27aed691b91a9b0 Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Fri, 6 Apr 2018 14:03:49 -0400 Subject: [PATCH 1/5] part1 --- .../persistenceapp/PersonService.java | 14 +++++ src/main/resources/script.sql | 56 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/io/zipcoder/persistenceapp/PersonService.java create mode 100644 src/main/resources/script.sql diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/PersonService.java new file mode 100644 index 0000000..3c17527 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonService.java @@ -0,0 +1,14 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.SimpleDriverDataSource; +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + + SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); + + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + +} diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..1873cb6 --- /dev/null +++ b/src/main/resources/script.sql @@ -0,0 +1,56 @@ +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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) +VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) +VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) +VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); + +SELECT * FROM movies; + +SELECT title FROM movies +WHERE genre = 'Sci-Fi'; + +SELECT title FROM movies +WHERE imdb_score >= 6.5; + +SELECT title FROM movies +WHERE rating = 'G' OR rating = 'PG' +AND runtime < 100; + +SELECT AVG(runtime) AS AverageRunTime +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) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating; + +SELECT rating, AVG(imdb_score) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating +HAVING COUNT(*) > 1; + +DELETE FROM movies +WHERE rating = 'R'; \ No newline at end of file From 6e207dabdc7592c7f478056cf9c3be215625f1ce Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Sat, 7 Apr 2018 13:13:43 -0400 Subject: [PATCH 2/5] able to GET person --- data-h2.sql | 51 ++++++++++++- .../Controller/PersonController.java | 29 ++++++++ .../persistenceapp/Domain/Person.java | 74 +++++++++++++++++++ .../persistenceapp/PersonService.java | 14 ---- .../Service/PersonRowMapper.java | 24 ++++++ .../persistenceapp/Service/PersonService.java | 46 ++++++++++++ src/main/resources/schema-h2.sql | 60 +++++++++++++++ src/main/resources/script.sql | 46 ++++++------ 8 files changed, 307 insertions(+), 37 deletions(-) create mode 100644 src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Domain/Person.java delete mode 100644 src/main/java/io/zipcoder/persistenceapp/PersonService.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java diff --git a/data-h2.sql b/data-h2.sql index f6f18b5..ddaf31a 100644 --- a/data-h2.sql +++ b/data-h2.sql @@ -1,15 +1,62 @@ +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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); - -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); +SELECT * FROM movies; + +SELECT title FROM movies +WHERE genre = 'Sci-Fi'; + +SELECT title FROM movies +WHERE imdb_score >= 6.5; + +SELECT title FROM movies +WHERE rating = 'G' OR rating = 'PG' +AND runtime < 100; + +SELECT AVG(runtime) AS AverageRunTime +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) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating; + +SELECT rating, AVG(imdb_score) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating +HAVING COUNT(*) > 1; + +DELETE FROM movies +WHERE rating = 'R'; + + + 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..2d329fb --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java @@ -0,0 +1,29 @@ +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.web.bind.annotation.*; + +import java.util.Collection; + +@RestController +public class PersonController { + + @Autowired + PersonService personService; + + @RequestMapping(value = "/person/{id}", method = RequestMethod.GET) + @ResponseStatus(HttpStatus.OK) + public Person getPersonById(@PathVariable int id){ + return personService.getSinglePersonById(id); + } + + @RequestMapping(value = "/person", method = RequestMethod.GET) + @ResponseStatus(HttpStatus.OK) + public Collection getAllPerson(){ + return null; + } + +} 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..c0baa5a --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java @@ -0,0 +1,74 @@ +package io.zipcoder.persistenceapp.Domain; + +import java.util.Date; + +public class Person { + private String firstName; + private String lastName; + private String mobileNumber; + private Date birthDate; + private int homeId; + private int id; + + public Person(){ + } + public Person(String firstName, String lastName, String mobileNumber, Date birthDate, int homeId, int id){ + this.firstName = firstName; + this.lastName = lastName; + this.mobileNumber = mobileNumber; + this.birthDate = birthDate; + this.homeId = homeId; + 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 getMobileNumber() { + return mobileNumber; + } + + public void setMobileNumber(String mobileNumber) { + this.mobileNumber = mobileNumber; + } + + public Date getBirthDate() { + return birthDate; + } + + public void setBirthDate(Date birthDate) { + this.birthDate = birthDate; + } + + public int getHomeId() { + return homeId; + } + + public void setHomeId(int homeId) { + this.homeId = homeId; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String toString(){ + return String.format("[%s, %s, %s, %s, %d]", firstName, lastName, mobileNumber, birthDate, homeId); + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/PersonService.java deleted file mode 100644 index 3c17527..0000000 --- a/src/main/java/io/zipcoder/persistenceapp/PersonService.java +++ /dev/null @@ -1,14 +0,0 @@ -package io.zipcoder.persistenceapp; - -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.SimpleDriverDataSource; -import org.springframework.stereotype.Service; - -@Service -public class PersonService { - - SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); - - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - -} diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java b/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java new file mode 100644 index 0000000..066166a --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java @@ -0,0 +1,24 @@ +package io.zipcoder.persistenceapp.Service; + +import io.zipcoder.persistenceapp.Domain.Person; + +import javax.swing.tree.RowMapper; +import javax.swing.tree.TreePath; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class PersonRowMapper implements org.springframework.jdbc.core.RowMapper { + + @Override + public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException { + Person person = new Person(); + person.setId(resultSet.getInt("ID")); + person.setFirstName(resultSet.getString("FIRST_NAME")); + person.setLastName(resultSet.getString("LAST_NAME")); + person.setMobileNumber(resultSet.getString("MOBILE")); + person.setBirthDate(resultSet.getDate("BIRTHDAY")); + person.setHomeId(resultSet.getInt("HOME_ID")); + + return person; + } +} 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..0f3413e --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java @@ -0,0 +1,46 @@ +package io.zipcoder.persistenceapp.Service; + +import io.zipcoder.persistenceapp.Domain.Person; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.SimpleDriverDataSource; +import org.springframework.stereotype.Service; + +@Service +public class PersonService { + +// private SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); + /** + * SQL methods needed: + * Add + * Update + * Remove + * Remove a list of people + * Find single person by ID + * Generate a map of lastnames to list people with that lastname + * Generate a map of firstnames to the number of occurrences. + */ + private static final String addPerson = "INSERT INTO PERSON " + + "(firstName, lastName, mobileNumber, birthDate, homeId, id) VALUES (?, ?, ?, ?, ?, ?)"; + private static final String updatePerson = "UPDATE PERSON SET "; + private static final String getAllPersons = "SELECT * FROM PERSON"; + private static final String getSingleId = "SELECT * FROM PERSON WHERE ID = ?"; + private static final String getAllIds = "SELECT ID FROM PERSON"; + private static final String deletePerson = "DELETE * FROM PERSON WHERE ID = ?"; + private static final String deleteListPerson = ""; + private static final String getMapLastName = ""; + private static final String getMapFirstName = ""; + + @Autowired + private JdbcTemplate jdbcTemplate; + + public Person getSinglePersonById(int id){ + Person person = (Person)jdbcTemplate.queryForObject(getSingleId, new Object[] {id}, new PersonRowMapper()); + return person; + } +// public void addPerson(Person person){ +// jdbcTemplate.update(addPerson, new Object[] +// {person.getFirstName(), person.getLastName(), person.getMobileNumber(), person.getBirthDate(), person.getHomeId(), person.getId()}); +// } + +} diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 39c2c27..998ad1b 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -64,3 +64,63 @@ CREATE TABLE auto_prices ( DROP SEQUENCE hibernate_sequence; CREATE SEQUENCE hibernate_sequence; + + +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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); + +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); + +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); + +SELECT * FROM movies; + +SELECT title FROM movies +WHERE genre = 'Sci-Fi'; + +SELECT title FROM movies +WHERE imdb_score >= 6.5; + +SELECT title FROM movies +WHERE rating = 'G' OR rating = 'PG' +AND runtime < 100; + +SELECT AVG(runtime) AS AverageRunTime +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) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating; + +SELECT rating, AVG(imdb_score) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB +FROM movies +GROUP BY rating +HAVING COUNT(*) > 1; + +DELETE FROM movies +WHERE rating = 'R'; \ No newline at end of file diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql index 1873cb6..6e32d87 100644 --- a/src/main/resources/script.sql +++ b/src/main/resources/script.sql @@ -1,23 +1,25 @@ -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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) -VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) -VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) -VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); +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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); + +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); +INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); + +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); +INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); SELECT * FROM movies; @@ -53,4 +55,6 @@ GROUP BY rating HAVING COUNT(*) > 1; DELETE FROM movies -WHERE rating = 'R'; \ No newline at end of file +WHERE rating = 'R'; + + From 5eee7cb01e44bc47a31233acec3cbadf62fe9da6 Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Sun, 8 Apr 2018 16:19:24 -0400 Subject: [PATCH 3/5] part 2 person methods bare minimum --- .../Controller/PersonController.java | 40 ++++++++--- .../persistenceapp/Domain/Person.java | 21 +++--- .../Service/PersonRowMapper.java | 24 ------- .../persistenceapp/Service/PersonService.java | 69 +++++++++++++------ 4 files changed, 87 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java diff --git a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java index 2d329fb..6e744f4 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java @@ -4,10 +4,10 @@ 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.*; -import java.util.Collection; - +//where the mapping goes @RestController public class PersonController { @@ -15,15 +15,35 @@ public class PersonController { PersonService personService; @RequestMapping(value = "/person/{id}", method = RequestMethod.GET) - @ResponseStatus(HttpStatus.OK) - public Person getPersonById(@PathVariable int id){ - return personService.getSinglePersonById(id); + public ResponseEntity getSinglePersonById(@PathVariable Long id){ + personService.getSinglePersonById(id); + return new ResponseEntity<>(HttpStatus.OK); } - - @RequestMapping(value = "/person", method = RequestMethod.GET) - @ResponseStatus(HttpStatus.OK) - public Collection getAllPerson(){ - return null; + @RequestMapping(value = "/persons", method = RequestMethod.GET) + public ResponseEntity getAllPersons(){ + personService.getAllPersons(); + return new ResponseEntity<>(HttpStatus.OK); + } + @RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE) + public ResponseEntity deletePersonById(@PathVariable Long id){ + personService.deletePersonById(id); + return new ResponseEntity<>(HttpStatus.OK); } + @RequestMapping(value = "/person/{last_name}", method = RequestMethod.GET) + public ResponseEntity findPersonByLastName(@PathVariable String last_name){ + personService.findPersonByLastName(last_name); + return new ResponseEntity<>(HttpStatus.OK); + } + @RequestMapping(value = "/person", method = RequestMethod.POST) + public ResponseEntity addPerson(@RequestBody Person person){ + personService.addPerson(person); + return new ResponseEntity<>(HttpStatus.CREATED); + } + @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT) + public ResponseEntity updatePerson(@PathVariable Long id, @RequestBody Person person){ + personService.getSinglePersonById(id); + personService.updatePerson(person, id); + return new ResponseEntity<>(HttpStatus.OK); + } } diff --git a/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java b/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java index c0baa5a..336d185 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java +++ b/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java @@ -7,18 +7,17 @@ public class Person { private String lastName; private String mobileNumber; private Date birthDate; - private int homeId; - private int id; + private Integer homeId; + Long Id; public Person(){ } - public Person(String firstName, String lastName, String mobileNumber, Date birthDate, int homeId, int id){ + public Person(Long Id, String firstName, String lastName, String mobileNumber){ + this.Id = Id; this.firstName = firstName; this.lastName = lastName; this.mobileNumber = mobileNumber; - this.birthDate = birthDate; - this.homeId = homeId; - this.id = id; + } public String getFirstName() { return firstName; @@ -56,16 +55,16 @@ public int getHomeId() { return homeId; } - public void setHomeId(int homeId) { + public void setHomeId(Integer homeId) { this.homeId = homeId; } - public int getId() { - return id; + public Long getId() { + return Id; } - public void setId(int id) { - this.id = id; + public void setId(Long id) { + this.Id = id; } public String toString(){ diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java b/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java deleted file mode 100644 index 066166a..0000000 --- a/src/main/java/io/zipcoder/persistenceapp/Service/PersonRowMapper.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.zipcoder.persistenceapp.Service; - -import io.zipcoder.persistenceapp.Domain.Person; - -import javax.swing.tree.RowMapper; -import javax.swing.tree.TreePath; -import java.sql.ResultSet; -import java.sql.SQLException; - -public class PersonRowMapper implements org.springframework.jdbc.core.RowMapper { - - @Override - public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException { - Person person = new Person(); - person.setId(resultSet.getInt("ID")); - person.setFirstName(resultSet.getString("FIRST_NAME")); - person.setLastName(resultSet.getString("LAST_NAME")); - person.setMobileNumber(resultSet.getString("MOBILE")); - person.setBirthDate(resultSet.getDate("BIRTHDAY")); - person.setHomeId(resultSet.getInt("HOME_ID")); - - return person; - } -} diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java index 0f3413e..7476585 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java @@ -2,45 +2,70 @@ import io.zipcoder.persistenceapp.Domain.Person; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + + +//where the methods go @Service public class PersonService { // private SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); /** * SQL methods needed: - * Add - * Update - * Remove - * Remove a list of people - * Find single person by ID - * Generate a map of lastnames to list people with that lastname - * Generate a map of firstnames to the number of occurrences. + * Add - should work + * Update - should work + * Remove - works + * Remove a list of people - in progress + * Find single person by ID - works + * Generate a map of lastnames to list people with that lastname - in progress + * Generate a map of firstnames to the number of occurrences. - in progress */ - private static final String addPerson = "INSERT INTO PERSON " + - "(firstName, lastName, mobileNumber, birthDate, homeId, id) VALUES (?, ?, ?, ?, ?, ?)"; - private static final String updatePerson = "UPDATE PERSON SET "; + private static final String getAllPersons = "SELECT * FROM PERSON"; private static final String getSingleId = "SELECT * FROM PERSON WHERE ID = ?"; - private static final String getAllIds = "SELECT ID FROM PERSON"; private static final String deletePerson = "DELETE * FROM PERSON WHERE ID = ?"; - private static final String deleteListPerson = ""; - private static final String getMapLastName = ""; - private static final String getMapFirstName = ""; + private static final String findByLastName = "SELECT * FROM PERSON WHERE LAST_NAME = ?"; +// private static final String getMapLastName = ""; +// private static final String getMapFirstName = ""; @Autowired private JdbcTemplate jdbcTemplate; - public Person getSinglePersonById(int id){ - Person person = (Person)jdbcTemplate.queryForObject(getSingleId, new Object[] {id}, new PersonRowMapper()); - return person; + public void getSinglePersonById(Long id){ + this.jdbcTemplate.execute(getSingleId); } -// public void addPerson(Person person){ -// jdbcTemplate.update(addPerson, new Object[] -// {person.getFirstName(), person.getLastName(), person.getMobileNumber(), person.getBirthDate(), person.getHomeId(), person.getId()}); -// } + + public void getAllPersons(){ + this.jdbcTemplate.queryForList(getAllPersons); + } + + public void deletePersonById(Long id){ + this.jdbcTemplate.execute(deletePerson); + } + public void findPersonByLastName(String last_name){ + this.jdbcTemplate.execute(findByLastName); + } + public void addPerson(Person person) { + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + String sql = "INSERT INTO person ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('" + person.getLastName() + + "','" + person.getFirstName() + "','" + person.getMobileNumber() + "','" + format.format(person.getBirthDate()) + "','" + person.getHomeId() + "')"; + jdbcTemplate.execute(sql); + } + public void updatePerson(Person person, Long id) { + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + String sql = "UPDATE person SET FIRST_NAME = '" + person.getFirstName() +"', LAST_NAME = '" + person.getLastName() + + "', MOBILE = '" + person.getMobileNumber() + "', BIRTHDAY = '" + format.format(person.getBirthDate()) + "', HOME_ID = '" + + person.getHomeId() + "' WHERE ID = " + id; + jdbcTemplate.execute(sql); + } + } From 48fb623bb9727f681e780ca2d8c34ed6cb2198d7 Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Sun, 8 Apr 2018 21:10:34 -0400 Subject: [PATCH 4/5] something wrong with sql statements --- README.md | 4 +- data-h2.sql | 47 ++-------- pom.xml | 2 +- .../Controller/JDBCPersonController.java | 52 +++++++++++ .../Controller/PersonController.java | 66 +++++++------- .../persistenceapp/Domain/Person.java | 73 --------------- .../persistenceapp/Entity/Person.java | 89 +++++++++++++++++++ .../Entity/PersonRepository.java | 6 ++ .../Service/JDBCPersonService.java | 74 +++++++++++++++ .../Service/JPAPersonServiceImpl.java | 71 +++++++++++++++ .../persistenceapp/Service/PersonService.java | 72 ++++----------- src/main/resources/schema-h2.sql | 60 ------------- src/main/resources/script.sql | 23 ----- 13 files changed, 353 insertions(+), 286 deletions(-) create mode 100644 src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java delete mode 100644 src/main/java/io/zipcoder/persistenceapp/Domain/Person.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Entity/Person.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java create mode 100644 src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java diff --git a/README.md b/README.md index c4ef5c4..cf56cd1 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,8 @@ Create the following REST endpoints to interact with the application. You can us - `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/reverselookup/{MOBILE}` -- find all people with the specified mobile number + - `GET` `/people/surname/{LAST_NAME}` -- 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/data-h2.sql b/data-h2.sql index ddaf31a..76661ea 100644 --- a/data-h2.sql +++ b/data-h2.sql @@ -14,48 +14,13 @@ INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 1173 INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); +INSERT INTO PERSON (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); -SELECT * FROM movies; - -SELECT title FROM movies -WHERE genre = 'Sci-Fi'; - -SELECT title FROM movies -WHERE imdb_score >= 6.5; - -SELECT title FROM movies -WHERE rating = 'G' OR rating = 'PG' -AND runtime < 100; - -SELECT AVG(runtime) AS AverageRunTime -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) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB -FROM movies -GROUP BY rating; - -SELECT rating, AVG(imdb_score) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB -FROM movies -GROUP BY rating -HAVING COUNT(*) > 1; - -DELETE FROM movies -WHERE rating = 'R'; diff --git a/pom.xml b/pom.xml index 274f418..ad12423 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ spring-boot-starter-test test - + diff --git a/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java b/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java new file mode 100644 index 0000000..60538a5 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java @@ -0,0 +1,52 @@ +//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.*; +// +/** + * defunct class from part 2 + */ +////where the mapping goes +//@RestController +//public class PersonController { +// +// @Autowired +// PersonService personService; +// +// @RequestMapping(value = "/person/{id}", method = RequestMethod.GET) +// public ResponseEntity getSinglePersonById(@PathVariable Long id){ +// personService.getSinglePersonById(id); +// return new ResponseEntity<>(HttpStatus.OK); +// } +// @RequestMapping(value = "/persons", method = RequestMethod.GET) +// public ResponseEntity getAllPersons(){ +// personService.getAllPersons(); +// return new ResponseEntity<>(HttpStatus.OK); +// } +// @RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE) +// public ResponseEntity deletePersonById(@PathVariable Long id){ +// personService.deletePersonById(id); +// return new ResponseEntity<>(HttpStatus.OK); +// } +// @RequestMapping(value = "/person/{last_name}", method = RequestMethod.GET) +// public ResponseEntity findPersonByLastName(@PathVariable String last_name){ +// personService.findPersonByLastName(last_name); +// return new ResponseEntity<>(HttpStatus.OK); +// } +// @RequestMapping(value = "/person", method = RequestMethod.POST) +// public ResponseEntity addPerson(@RequestBody Person person){ +// personService.addPerson(person); +// return new ResponseEntity<>(HttpStatus.CREATED); +// } +// @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT) +// public ResponseEntity updatePerson(@PathVariable Long id, @RequestBody Person person){ +// personService.getSinglePersonById(id); +// personService.updatePerson(person, id); +// return new ResponseEntity<>(HttpStatus.OK); +// +// } +//} diff --git a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java index 6e744f4..ff7b5c5 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java @@ -1,49 +1,55 @@ package io.zipcoder.persistenceapp.Controller; -import io.zipcoder.persistenceapp.Domain.Person; +import io.zipcoder.persistenceapp.Entity.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.*; +import org.springframework.web.bind.annotation.RequestMethod.*; -//where the mapping goes @RestController public class PersonController { + private PersonService personService; + @Autowired - PersonService personService; + public PersonController(PersonService personService){ + this.personService = personService; + } - @RequestMapping(value = "/person/{id}", method = RequestMethod.GET) - public ResponseEntity getSinglePersonById(@PathVariable Long id){ - personService.getSinglePersonById(id); - return new ResponseEntity<>(HttpStatus.OK); + @RequestMapping(value = "/people/{id}", method = RequestMethod.GET) + public ResponseEntity getPerson(@PathVariable Long id) { + return personService.findById(id); } - @RequestMapping(value = "/persons", method = RequestMethod.GET) - public ResponseEntity getAllPersons(){ - personService.getAllPersons(); - return new ResponseEntity<>(HttpStatus.OK); + @RequestMapping(value = "/people", method = RequestMethod.GET) + public ResponseEntity> getAllPeople() { + return personService.getAllPeople(); } - @RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE) - public ResponseEntity deletePersonById(@PathVariable Long id){ - personService.deletePersonById(id); - return new ResponseEntity<>(HttpStatus.OK); + @RequestMapping(value = "/people", method = RequestMethod.POST) + public ResponseEntity createPerson(@RequestBody Person person) { + return personService.addPerson(person); } - @RequestMapping(value = "/person/{last_name}", method = RequestMethod.GET) - public ResponseEntity findPersonByLastName(@PathVariable String last_name){ - personService.findPersonByLastName(last_name); - return new ResponseEntity<>(HttpStatus.OK); + @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT) + public ResponseEntity updatePerson(@RequestBody Person person, @PathVariable Long id) { + return personService.updatePerson(person); } - @RequestMapping(value = "/person", method = RequestMethod.POST) - public ResponseEntity addPerson(@RequestBody Person person){ - personService.addPerson(person); - return new ResponseEntity<>(HttpStatus.CREATED); + @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE) + public ResponseEntity deletePerson(@PathVariable Long id) { + return personService.removePerson(id); } - @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT) - public ResponseEntity updatePerson(@PathVariable Long id, @RequestBody Person person){ - personService.getSinglePersonById(id); - personService.updatePerson(person, id); - return new ResponseEntity<>(HttpStatus.OK); - } +// @RequestMapping(value = "/people/reverselookup/{mobile}", method = RequestMethod.GET) +// public ResponseEntity> reverseLookup(@PathVariable String mobile) { +// return personService.reverseLookup(mobile); +// } +// +// @RequestMapping(value = "/people/surname/{lastName}", method = RequestMethod.GET) +// public ResponseEntity> surnameLookup(@PathVariable String lastName) { +// return personService.findByLastName(lastName); +// } + +// @RequestMapping(value = "/people/firstname/stats", method = RequestMethod.GET) +// public ResponseEntity> firstNameWithStats() { +// return personService.getFirstNameStats(); +// } } diff --git a/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java b/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java deleted file mode 100644 index 336d185..0000000 --- a/src/main/java/io/zipcoder/persistenceapp/Domain/Person.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.zipcoder.persistenceapp.Domain; - -import java.util.Date; - -public class Person { - private String firstName; - private String lastName; - private String mobileNumber; - private Date birthDate; - private Integer homeId; - Long Id; - - public Person(){ - } - public Person(Long Id, String firstName, String lastName, String mobileNumber){ - this.Id = Id; - this.firstName = firstName; - this.lastName = lastName; - this.mobileNumber = mobileNumber; - - } - 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 getMobileNumber() { - return mobileNumber; - } - - public void setMobileNumber(String mobileNumber) { - this.mobileNumber = mobileNumber; - } - - public Date getBirthDate() { - return birthDate; - } - - public void setBirthDate(Date birthDate) { - this.birthDate = birthDate; - } - - public int getHomeId() { - return homeId; - } - - public void setHomeId(Integer homeId) { - this.homeId = homeId; - } - - public Long getId() { - return Id; - } - - public void setId(Long id) { - this.Id = id; - } - - public String toString(){ - return String.format("[%s, %s, %s, %s, %d]", firstName, lastName, mobileNumber, birthDate, homeId); - } -} diff --git a/src/main/java/io/zipcoder/persistenceapp/Entity/Person.java b/src/main/java/io/zipcoder/persistenceapp/Entity/Person.java new file mode 100644 index 0000000..5f961a0 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Entity/Person.java @@ -0,0 +1,89 @@ +package io.zipcoder.persistenceapp.Entity; + +import javax.persistence.*; + +@Entity +public class Person { + + @Id + @GeneratedValue + @Column(name = "PERSON_ID") + private Long Id; + + @Column(name = "LAST_NAME") + private String LAST_NAME; + + @Column(name = "FIRST_NAME") + private String FIRST_NAME; + + @Column(name = "MOBILE") + private String MOBILE; + + @Column(name = "BIRTHDAY") + private Long BIRTHDAY; + + @Column(name = "HOME_ID") + private Integer HOME_ID; + + public Person(){ + + } + public Person(Long Id, String FIRST_NAME, String LAST_NAME, String MOBILE){ + this.Id = Id; + this.FIRST_NAME = FIRST_NAME; + this.LAST_NAME = LAST_NAME; + this.MOBILE = MOBILE; + + } + public String getFIRST_NAME() { + return FIRST_NAME; + } + + public void setFIRST_NAME(String FIRST_NAME) { + this.FIRST_NAME = FIRST_NAME; + } + + public String getLAST_NAME() { + return LAST_NAME; + } + + public void setLAST_NAME(String LAST_NAME) { + this.LAST_NAME = LAST_NAME; + } + + public String getMOBILE() { + return MOBILE; + } + + public void setMOBILE(String MOBILE) { + this.MOBILE = MOBILE; + } + + public Long getBIRTHDAY() { + return BIRTHDAY; + } + + public void setBIRTHDAY(Long BIRTHDAY) { + this.BIRTHDAY = BIRTHDAY; + } + + public int getHOME_ID() { + return HOME_ID; + } + + public void setHOME_ID(Integer HOME_ID) { + this.HOME_ID = HOME_ID; + } + + public Long getId() { + return Id; + } + + public void setId(Long id) { + this.Id = id; + } + +// public String toString(){ +// return String.format("[%s, %s, %s, %s, %d]", FIRST_NAME, LAST_NAME, MOBILE, BIRTHDAY, HOME_ID); +// } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java b/src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java new file mode 100644 index 0000000..898428c --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java @@ -0,0 +1,6 @@ +package io.zipcoder.persistenceapp.Entity; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PersonRepository extends JpaRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java new file mode 100644 index 0000000..260dc16 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java @@ -0,0 +1,74 @@ +//package io.zipcoder.persistenceapp.Service; +// +//import io.zipcoder.persistenceapp.Entity.Person; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.ResponseEntity; +//import org.springframework.jdbc.core.JdbcTemplate; +//import org.springframework.stereotype.Service; +//import org.springframework.web.bind.annotation.PathVariable; +//import org.springframework.web.bind.annotation.RequestBody; +// +//import java.text.DateFormat; +//import java.text.SimpleDateFormat; +// +/** + * defunct class from part 2 + */ +// +////where the methods go +//@Service +//public class PersonService { +// +//// private SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); +// /** +// * SQL methods needed: +// * Add - should work +// * Update - should work +// * Remove - works +// * Remove a list of people - in progress +// * Find single person by ID - works +// * Generate a map of lastnames to list people with that lastname - in progress +// * Generate a map of firstnames to the number of occurrences. - in progress +// */ +// +// private static final String getAllPersons = "SELECT * FROM PERSON"; +// private static final String getSingleId = "SELECT * FROM PERSON WHERE ID = ?"; +// private static final String deletePerson = "DELETE * FROM PERSON WHERE ID = ?"; +// private static final String findByLastName = "SELECT * FROM PERSON WHERE LAST_NAME = ?"; +//// private static final String getMapLastName = ""; +//// private static final String getMapFirstName = ""; +// +// @Autowired +// private JdbcTemplate jdbcTemplate; +// +// public void getSinglePersonById(Long id){ +// this.jdbcTemplate.execute(getSingleId); +// } +// +// public void getAllPersons(){ +// this.jdbcTemplate.queryForList(getAllPersons); +// } +// +// public void deletePersonById(Long id){ +// this.jdbcTemplate.execute(deletePerson); +// } +// public void findPersonByLastName(String last_name){ +// this.jdbcTemplate.execute(findByLastName); +// } +// public void addPerson(Person person) { +// DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); +// String sql = "INSERT INTO person ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('" + person.getLAST_NAME() + +// "','" + person.getFIRST_NAME() + "','" + person.getMOBILE() + "','" + format.format(person.getBIRTHDAY()) + "','" + person.getHOME_ID() + "')"; +// jdbcTemplate.execute(sql); +// } +// public void updatePerson(Person person, Long id) { +// DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); +// String sql = "UPDATE person SET FIRST_NAME = '" + person.getFIRST_NAME() +"', LAST_NAME = '" + person.getLAST_NAME() + +// "', MOBILE = '" + person.getMOBILE() + "', BIRTHDAY = '" + format.format(person.getBIRTHDAY()) + "', HOME_ID = '" + +// person.getHOME_ID() + "' WHERE ID = " + id; +// jdbcTemplate.execute(sql); +// } +// +// +//} diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java b/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java new file mode 100644 index 0000000..aa4c9a5 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java @@ -0,0 +1,71 @@ +package io.zipcoder.persistenceapp.Service; + +import io.zipcoder.persistenceapp.Entity.Person; +import io.zipcoder.persistenceapp.Entity.PersonRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +@Primary +@Service +public class JPAPersonServiceImpl implements PersonService{ + + private PersonRepository personRepository; + + /** + * To-Do: findAll LastNames, FirstNames, Birthdays,Mobile. + * Done with: basic methods. + * @param personRepository + */ + @Autowired + public JPAPersonServiceImpl(PersonRepository personRepository){ + this.personRepository = personRepository; + } +// @Override + public ResponseEntity findById(Long id) { + return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK); + } +// @Override + public ResponseEntity> getAllPeople() { + return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK); + } +// @Override + public ResponseEntity addPerson(Person person) { + return new ResponseEntity<>(personRepository.save(person), HttpStatus.OK); + } +// @Override + public ResponseEntity updatePerson(Person person) { + return new ResponseEntity<>(personRepository.save(person), HttpStatus.OK); + } +// @Override + public ResponseEntity removePerson(Long id) { + personRepository.delete(id); + return new ResponseEntity<>(HttpStatus.OK); + } +// @Override + public ResponseEntity> findByLastName(String lastname) { + return null; + } +// @Override + public ResponseEntity> reverseLookup(String mobile) { + return null; + } +// @Override + public ResponseEntity> findByBirthday(String birthday) { + return null; + } +// @Override +// public ResponseEntity>> getSurname(String surname) { +// return null; +// } +// @Override +// public ResponseEntity> getFirstNameWithStats() { +// return null; +// } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java index 7476585..ad81e97 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java @@ -1,71 +1,31 @@ package io.zipcoder.persistenceapp.Service; -import io.zipcoder.persistenceapp.Domain.Person; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; +import io.zipcoder.persistenceapp.Entity.Person; import org.springframework.http.ResponseEntity; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.List; +import java.util.Map; +public interface PersonService { -//where the methods go -@Service -public class PersonService { + ResponseEntity findById(Long id); -// private SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); - /** - * SQL methods needed: - * Add - should work - * Update - should work - * Remove - works - * Remove a list of people - in progress - * Find single person by ID - works - * Generate a map of lastnames to list people with that lastname - in progress - * Generate a map of firstnames to the number of occurrences. - in progress - */ + ResponseEntity> getAllPeople(); - private static final String getAllPersons = "SELECT * FROM PERSON"; - private static final String getSingleId = "SELECT * FROM PERSON WHERE ID = ?"; - private static final String deletePerson = "DELETE * FROM PERSON WHERE ID = ?"; - private static final String findByLastName = "SELECT * FROM PERSON WHERE LAST_NAME = ?"; -// private static final String getMapLastName = ""; -// private static final String getMapFirstName = ""; + ResponseEntity addPerson(Person person); - @Autowired - private JdbcTemplate jdbcTemplate; + ResponseEntity updatePerson(Person person); - public void getSinglePersonById(Long id){ - this.jdbcTemplate.execute(getSingleId); - } + ResponseEntity removePerson(Long personId); - public void getAllPersons(){ - this.jdbcTemplate.queryForList(getAllPersons); - } + ResponseEntity> findByLastName(String lastname); - public void deletePersonById(Long id){ - this.jdbcTemplate.execute(deletePerson); - } - public void findPersonByLastName(String last_name){ - this.jdbcTemplate.execute(findByLastName); - } - public void addPerson(Person person) { - DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - String sql = "INSERT INTO person ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('" + person.getLastName() + - "','" + person.getFirstName() + "','" + person.getMobileNumber() + "','" + format.format(person.getBirthDate()) + "','" + person.getHomeId() + "')"; - jdbcTemplate.execute(sql); - } - public void updatePerson(Person person, Long id) { - DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - String sql = "UPDATE person SET FIRST_NAME = '" + person.getFirstName() +"', LAST_NAME = '" + person.getLastName() + - "', MOBILE = '" + person.getMobileNumber() + "', BIRTHDAY = '" + format.format(person.getBirthDate()) + "', HOME_ID = '" + - person.getHomeId() + "' WHERE ID = " + id; - jdbcTemplate.execute(sql); - } + ResponseEntity> reverseLookup(String mobile); + ResponseEntity> findByBirthday(String birthday); +// ResponseEntity>> getSurname(String surname); +// +// ResponseEntity> getFirstNameWithStats(); } diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 998ad1b..39c2c27 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -64,63 +64,3 @@ CREATE TABLE auto_prices ( DROP SEQUENCE hibernate_sequence; CREATE SEQUENCE hibernate_sequence; - - -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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); - -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); - -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); - -SELECT * FROM movies; - -SELECT title FROM movies -WHERE genre = 'Sci-Fi'; - -SELECT title FROM movies -WHERE imdb_score >= 6.5; - -SELECT title FROM movies -WHERE rating = 'G' OR rating = 'PG' -AND runtime < 100; - -SELECT AVG(runtime) AS AverageRunTime -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) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB -FROM movies -GROUP BY rating; - -SELECT rating, AVG(imdb_score) as avgIMDB, MAX(imdb_score) as maxIMDB, MIN(imdb_score) as minIMDB -FROM movies -GROUP BY rating -HAVING COUNT(*) > 1; - -DELETE FROM movies -WHERE rating = 'R'; \ No newline at end of file diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql index 6e32d87..b8c77ed 100644 --- a/src/main/resources/script.sql +++ b/src/main/resources/script.sql @@ -1,26 +1,3 @@ -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('2001: A Space Odyssey', 149, 'Sci-Fi', 8.3, 'G'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Blade Runner 2049', 164, 'Sci-Fi', 8.2, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Mad Max: Fury Road', 120, 'Action', 8.1, 'R'); -INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES('Titanic', 194, 'Romance', 7.8, 'PG-13'); - -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('920 Arlington Street Clifton, NJ 07011', '985-4515'); -INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES ('234 High Street, PA 19159 ', '267-3940'); - -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID ) VALUES ('Carbral', 'Sheeri', '230-4233', '1970-02-23', 2); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES ( 'Sharam', 'Raj', '186-5223', '1980-08-31', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Durand', 'Noelle', '395-6161', '1960-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Thomas', '395-6181', '1987-07-06', 1); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Smith', 'Jane', '393-6181', '1987-12-06', 3); -INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3); - SELECT * FROM movies; SELECT title FROM movies From 0a8b650b64471a4b4160ec9679eff6a8595e203a Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Mon, 9 Apr 2018 08:53:34 -0400 Subject: [PATCH 5/5] jpa in progress --- pom.xml | 5 +- .../Controller/JDBCPersonController.java | 52 ------------------- .../Controller/PersonController.java | 3 +- .../Service/JDBCPersonService.java | 2 +- .../Service/JPAPersonServiceImpl.java | 25 ++++----- .../persistenceapp/Service/PersonService.java | 6 +-- .../{Entity => domain}/Person.java | 2 +- .../{Entity => domain}/PersonRepository.java | 2 +- 8 files changed, 19 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java rename src/main/java/io/zipcoder/persistenceapp/{Entity => domain}/Person.java (97%) rename src/main/java/io/zipcoder/persistenceapp/{Entity => domain}/PersonRepository.java (76%) diff --git a/pom.xml b/pom.xml index ad12423..827b847 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-thymeleaf + com.h2database h2 diff --git a/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java b/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java deleted file mode 100644 index 60538a5..0000000 --- a/src/main/java/io/zipcoder/persistenceapp/Controller/JDBCPersonController.java +++ /dev/null @@ -1,52 +0,0 @@ -//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.*; -// -/** - * defunct class from part 2 - */ -////where the mapping goes -//@RestController -//public class PersonController { -// -// @Autowired -// PersonService personService; -// -// @RequestMapping(value = "/person/{id}", method = RequestMethod.GET) -// public ResponseEntity getSinglePersonById(@PathVariable Long id){ -// personService.getSinglePersonById(id); -// return new ResponseEntity<>(HttpStatus.OK); -// } -// @RequestMapping(value = "/persons", method = RequestMethod.GET) -// public ResponseEntity getAllPersons(){ -// personService.getAllPersons(); -// return new ResponseEntity<>(HttpStatus.OK); -// } -// @RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE) -// public ResponseEntity deletePersonById(@PathVariable Long id){ -// personService.deletePersonById(id); -// return new ResponseEntity<>(HttpStatus.OK); -// } -// @RequestMapping(value = "/person/{last_name}", method = RequestMethod.GET) -// public ResponseEntity findPersonByLastName(@PathVariable String last_name){ -// personService.findPersonByLastName(last_name); -// return new ResponseEntity<>(HttpStatus.OK); -// } -// @RequestMapping(value = "/person", method = RequestMethod.POST) -// public ResponseEntity addPerson(@RequestBody Person person){ -// personService.addPerson(person); -// return new ResponseEntity<>(HttpStatus.CREATED); -// } -// @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT) -// public ResponseEntity updatePerson(@PathVariable Long id, @RequestBody Person person){ -// personService.getSinglePersonById(id); -// personService.updatePerson(person, id); -// return new ResponseEntity<>(HttpStatus.OK); -// -// } -//} diff --git a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java index ff7b5c5..7b1e72b 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java @@ -1,11 +1,10 @@ package io.zipcoder.persistenceapp.Controller; -import io.zipcoder.persistenceapp.Entity.Person; +import io.zipcoder.persistenceapp.domain.Person; import io.zipcoder.persistenceapp.Service.PersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import org.springframework.web.bind.annotation.RequestMethod.*; @RestController public class PersonController { diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java index 260dc16..b2f7b07 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java +++ b/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java @@ -1,6 +1,6 @@ //package io.zipcoder.persistenceapp.Service; // -//import io.zipcoder.persistenceapp.Entity.Person; +//import io.zipcoder.persistenceapp.domain.Person; //import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.http.HttpStatus; //import org.springframework.http.ResponseEntity; diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java b/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java index aa4c9a5..79679aa 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java +++ b/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java @@ -1,23 +1,18 @@ package io.zipcoder.persistenceapp.Service; -import io.zipcoder.persistenceapp.Entity.Person; -import io.zipcoder.persistenceapp.Entity.PersonRepository; +import io.zipcoder.persistenceapp.domain.Person; +import io.zipcoder.persistenceapp.domain.PersonRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import java.util.Collection; -import java.util.List; -import java.util.Map; - @Primary @Service public class JPAPersonServiceImpl implements PersonService{ private PersonRepository personRepository; - /** * To-Do: findAll LastNames, FirstNames, Birthdays,Mobile. * Done with: basic methods. @@ -27,36 +22,36 @@ public class JPAPersonServiceImpl implements PersonService{ public JPAPersonServiceImpl(PersonRepository personRepository){ this.personRepository = personRepository; } -// @Override + @Override public ResponseEntity findById(Long id) { return new ResponseEntity<>(personRepository.findOne(id), HttpStatus.OK); } -// @Override + @Override public ResponseEntity> getAllPeople() { return new ResponseEntity<>(personRepository.findAll(), HttpStatus.OK); } -// @Override + @Override public ResponseEntity addPerson(Person person) { return new ResponseEntity<>(personRepository.save(person), HttpStatus.OK); } -// @Override + @Override public ResponseEntity updatePerson(Person person) { return new ResponseEntity<>(personRepository.save(person), HttpStatus.OK); } -// @Override + @Override public ResponseEntity removePerson(Long id) { personRepository.delete(id); return new ResponseEntity<>(HttpStatus.OK); } -// @Override + @Override public ResponseEntity> findByLastName(String lastname) { return null; } -// @Override + @Override public ResponseEntity> reverseLookup(String mobile) { return null; } -// @Override + @Override public ResponseEntity> findByBirthday(String birthday) { return null; } diff --git a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java index ad81e97..8830209 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java @@ -1,12 +1,8 @@ package io.zipcoder.persistenceapp.Service; -import io.zipcoder.persistenceapp.Entity.Person; +import io.zipcoder.persistenceapp.domain.Person; import org.springframework.http.ResponseEntity; -import java.util.Collection; -import java.util.List; -import java.util.Map; - public interface PersonService { ResponseEntity findById(Long id); diff --git a/src/main/java/io/zipcoder/persistenceapp/Entity/Person.java b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java similarity index 97% rename from src/main/java/io/zipcoder/persistenceapp/Entity/Person.java rename to src/main/java/io/zipcoder/persistenceapp/domain/Person.java index 5f961a0..e389a74 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Entity/Person.java +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java @@ -1,4 +1,4 @@ -package io.zipcoder.persistenceapp.Entity; +package io.zipcoder.persistenceapp.domain; import javax.persistence.*; diff --git a/src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java b/src/main/java/io/zipcoder/persistenceapp/domain/PersonRepository.java similarity index 76% rename from src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java rename to src/main/java/io/zipcoder/persistenceapp/domain/PersonRepository.java index 898428c..16b3c69 100644 --- a/src/main/java/io/zipcoder/persistenceapp/Entity/PersonRepository.java +++ b/src/main/java/io/zipcoder/persistenceapp/domain/PersonRepository.java @@ -1,4 +1,4 @@ -package io.zipcoder.persistenceapp.Entity; +package io.zipcoder.persistenceapp.domain; import org.springframework.data.jpa.repository.JpaRepository; public interface PersonRepository extends JpaRepository {