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 f6f18b5..76661ea 100644 --- a/data-h2.sql +++ b/data-h2.sql @@ -1,15 +1,27 @@ +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); + + -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); diff --git a/pom.xml b/pom.xml index 274f418..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 @@ -48,7 +51,7 @@ spring-boot-starter-test test - + 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..7b1e72b --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Controller/PersonController.java @@ -0,0 +1,54 @@ +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.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class PersonController { + + private PersonService personService; + + @Autowired + public PersonController(PersonService personService){ + this.personService = personService; + } + + @RequestMapping(value = "/people/{id}", method = RequestMethod.GET) + public ResponseEntity getPerson(@PathVariable Long id) { + return personService.findById(id); + } + @RequestMapping(value = "/people", method = RequestMethod.GET) + public ResponseEntity> getAllPeople() { + return personService.getAllPeople(); + } + @RequestMapping(value = "/people", method = RequestMethod.POST) + public ResponseEntity createPerson(@RequestBody Person person) { + return personService.addPerson(person); + } + @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT) + public ResponseEntity updatePerson(@RequestBody Person person, @PathVariable Long id) { + return personService.updatePerson(person); + } + @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE) + public ResponseEntity deletePerson(@PathVariable Long id) { + return personService.removePerson(id); + } + +// @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/Service/JDBCPersonService.java b/src/main/java/io/zipcoder/persistenceapp/Service/JDBCPersonService.java new file mode 100644 index 0000000..b2f7b07 --- /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.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.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..79679aa --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/JPAPersonServiceImpl.java @@ -0,0 +1,66 @@ +package io.zipcoder.persistenceapp.Service; + +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; + +@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 new file mode 100644 index 0000000..8830209 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Service/PersonService.java @@ -0,0 +1,27 @@ +package io.zipcoder.persistenceapp.Service; + +import io.zipcoder.persistenceapp.domain.Person; +import org.springframework.http.ResponseEntity; + +public interface PersonService { + + ResponseEntity findById(Long id); + + ResponseEntity> getAllPeople(); + + ResponseEntity addPerson(Person person); + + ResponseEntity updatePerson(Person person); + + ResponseEntity removePerson(Long personId); + + ResponseEntity> findByLastName(String lastname); + + ResponseEntity> reverseLookup(String mobile); + + ResponseEntity> findByBirthday(String birthday); + +// ResponseEntity>> getSurname(String surname); +// +// ResponseEntity> getFirstNameWithStats(); +} 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..e389a74 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java @@ -0,0 +1,89 @@ +package io.zipcoder.persistenceapp.domain; + +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/domain/PersonRepository.java b/src/main/java/io/zipcoder/persistenceapp/domain/PersonRepository.java new file mode 100644 index 0000000..16b3c69 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/PersonRepository.java @@ -0,0 +1,6 @@ +package io.zipcoder.persistenceapp.domain; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PersonRepository extends JpaRepository { + +} diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..b8c77ed --- /dev/null +++ b/src/main/resources/script.sql @@ -0,0 +1,37 @@ +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'; + +