diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonRepository.java b/src/main/java/io/zipcoder/persistenceapp/PersonRepository.java new file mode 100644 index 0000000..8e03008 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonRepository.java @@ -0,0 +1,4 @@ +package io.zipcoder.persistenceapp; + +public class PersonRepository { +} 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..73df866 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Home.java @@ -0,0 +1,46 @@ +package io.zipcoder.persistenceapp.domain; + +public class Home{ + private Integer id; + private String address; + private String homenumber; + + public Home() { + } + + public Home(String address, String homenumber) { + + this.address = address; + this.homenumber = homenumber; + } + + public Home(Integer id, String address, String homenumber) { + this.id = id; + this.address = address; + this.homenumber = homenumber; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getHomenumber() { + return homenumber; + } + + public void setHomenumber(String homenumber) { + this.homenumber = homenumber; + } +} 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..c12f699 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/domain/Person.java @@ -0,0 +1,81 @@ +package io.zipcoder.persistenceapp.domain; + +import org.springframework.jdbc.core.JdbcTemplate; + +public class Person extends JdbcTemplate{ + + private Integer id; + private String first_name; + private String last_name; + private String mobile; + private String birthday; + private Integer home_id; + + public Person() { + } + + public Person(Integer id, String first_name, String last_name, String mobile, String birthday, Integer home_id) { + this.id = id; + this.first_name = first_name; + this.last_name = last_name; + this.mobile = mobile; + this.birthday = birthday; + this.home_id = home_id; + } + + public Person(String first_name, String last_name, String mobile, String birthday, Integer home_id) { + this.first_name = first_name; + this.last_name = last_name; + this.mobile = mobile; + this.birthday = birthday; + this.home_id = home_id; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + 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 String getBirthday() { + return birthday; + } + + public void setBirthday(String birthday) { + this.birthday = birthday; + } + + public Integer getHome_id() { + return home_id; + } + + public void setHome_id(Integer home_id) { + this.home_id = home_id; + } +} 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..eb9c53f --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/HomeService.java @@ -0,0 +1,57 @@ +package io.zipcoder.persistenceapp.service; + +import io.zipcoder.persistenceapp.domain.Home; +import org.springframework.http.HttpRequest; +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 org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Service +@RequestMapping +public class HomeService { + private JdbcTemplate template; + + public HomeService(JdbcTemplate jdbcTemplate) { + this.template = jdbcTemplate; + } + + @RequestMapping(value = "/homes", method = RequestMethod.GET) + public ResponseEntity getAllHomes(){ + String sql = "SELECT * FROM home"; + Iterable homes = template.queryForList(sql); + return new ResponseEntity(homes, HttpStatus.OK); + } + + @RequestMapping(value = "/homes", method = RequestMethod.POST) + public ResponseEntity createHome(@RequestBody Home home){ + String sql = "INSERT INTO home (address, homenumber) VALUES('" + home.getAddress() +"','" + home.getHomenumber() + "')"; + template.execute(sql); + return new ResponseEntity(HttpStatus.CREATED); + } + + @RequestMapping(value = "/homes/{id}", method = RequestMethod.PUT) + public ResponseEntity updateHome(@PathVariable Integer id, @RequestBody Home home){ + String sql = "UPDATE home SET address = '" + home.getAddress() + "', homenumber = '" + home.getHomenumber() + "' WHERE id = " + id; + template.execute(sql); + return new ResponseEntity(HttpStatus.OK); + } + + @RequestMapping(value = "/homes/{id}", method = RequestMethod.DELETE) + public ResponseEntity deleteHome(@PathVariable Integer id){ + String sql = "DELETE FROM home WHERE id = " + id; + template.execute(sql); + return new ResponseEntity(HttpStatus.OK); + } + + @RequestMapping(value = "/homes/{id}", method = RequestMethod.GET) + public ResponseEntity findHomeById(@PathVariable Integer id){ + String sql = "SELECT * FROM home WHERE id = " + id; + Iterable home = template.queryForList(sql); + return new ResponseEntity(home, HttpStatus.OK); + } +} 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..4dbd3b8 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/service/PersonService.java @@ -0,0 +1,111 @@ +package io.zipcoder.persistenceapp.service; + +import io.zipcoder.persistenceapp.domain.Person; +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 org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Service +@RequestMapping +public class PersonService { + + private JdbcTemplate template; + + public PersonService(JdbcTemplate template) { + this.template = template; + } + + @RequestMapping(value = "/people", method = RequestMethod.POST) + public ResponseEntity createPerson(@RequestBody Person person){ +// Person person = new Person(first_name, last_name, mobile, birthday, home_id); + String sql = createInsertStatement(person); + template.execute(sql); + return new ResponseEntity(HttpStatus.CREATED); + } + + @RequestMapping(value = "people/{id}", method = RequestMethod.PUT) + public ResponseEntity updatePerson(@PathVariable Integer id, @RequestBody Person person){ + String sql = "UPDATE person SET first_name = '" + person.getFirst_name() + "', last_name = '" + person.getLast_name() + "', mobile = '" + + person.getMobile() + "', birthday = '" + person.getBirthday() + "', home_id = '" + person.getHome_id() + "' WHERE id = " + id; + template.execute(sql); + return new ResponseEntity(person, HttpStatus.OK); + } + + @RequestMapping(value = "/people", method = RequestMethod.GET) + public ResponseEntity getAllPeople(){ + String sql = "SELECT * FROM person"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/{id}", method = RequestMethod.GET) + public ResponseEntity getPersonById(@PathVariable Integer id){ + String sql = "SELECT * FROM person WHERE id = " + id; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/{id}", method = RequestMethod.DELETE) + public ResponseEntity deletePersonById(@PathVariable Integer id){ + String sql = "DELETE FROM person WHERE id = " + id; + template.execute(sql); + return new ResponseEntity(HttpStatus.OK); + } + + //need to come back to this one + @RequestMapping(value = "/people", method = RequestMethod.DELETE) + public ResponseEntity deleteGroup(@RequestBody Integer[] ids){ + for (Integer id:ids) { + String sql = "DELETE FROM person WHERE id = " + id; + template.execute(sql); + } + return new ResponseEntity(HttpStatus.OK); + } + + @RequestMapping(value = "/people/first_name/{first_name}", method = RequestMethod.GET) + public ResponseEntity findGroupByFirstName(@PathVariable String first_name){ + String sql = "SELECT * FROM person WHERE first_name = '" + first_name + "'"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/last_name/{last_name}", method = RequestMethod.GET) + public ResponseEntity findGroupByLastName(@PathVariable String last_name){ + String sql = "SELECT * FROM person WHERE last_name = '" + last_name + "'"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/birthday/{birthday}", method = RequestMethod.GET) + public ResponseEntity findGroupByBirthday(@PathVariable String birthday){ + String sql = "SELECT * FROM person WHERE birthday = '" + birthday + "'"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/last_name", method = RequestMethod.GET) + public ResponseEntity lastNameOccurences(){ + String sql = "SELECT last_name, COUNT(last_name) FROM person GROUP BY last_name"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + @RequestMapping(value = "/people/first_name", method = RequestMethod.GET) + public ResponseEntity firstNameOccurences(){ + String sql = "SELECT first_name, COUNT(first_name) FROM person GROUP BY first_name"; + Iterable people = template.queryForList(sql); + return new ResponseEntity(people, HttpStatus.OK); + } + + private String createInsertStatement(Person person){ + String sql = "INSERT INTO PERSON(first_name, last_name, mobile, birthday, home_id) VALUES( '" + + person.getFirst_name() + "','" + person.getLast_name() + "','" + person.getMobile() + "','" + person.getBirthday() + "','" + person.getHome_id() + + "')"; + return sql; + } +} diff --git a/src/main/resources/schema-h2.sql b/src/main/resources/schema-h2.sql index 39c2c27..2624a0c 100644 --- a/src/main/resources/schema-h2.sql +++ b/src/main/resources/schema-h2.sql @@ -61,6 +61,22 @@ CREATE TABLE auto_prices ( ); +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); + + + + DROP SEQUENCE hibernate_sequence; CREATE SEQUENCE hibernate_sequence; diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..478cc85 --- /dev/null +++ b/src/main/resources/script.sql @@ -0,0 +1,22 @@ +INSERT INTO movies (title, runtime, genre, imdb_score, rating) +VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG'),('Lavalantula', 83, 'Horror', 4.7, 'TV-14'), +('Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13'), ('Waltz With Bashir', 90, 'Documentary', 8.0, 'R'), ('Monsters Inc.', 92, 'Animation', 8.1, 'G'), +('Spaceballs', 96, 'Comedy', 7.1, 'PG'),('Tombstone', 102, 'Western', 7.3, 'R'), ('Black Panther', 135, 'Superhero', 9.5, 'PG-13'); + +SELECT * FROM movies WHERE genre='Sci-Fi'; + +SELECT * FROM movies WHERE imdb_score > 6.5; + +SELECT * FROM movies WHERE rating = 'G' OR rating = 'PG' AND runtime < 100; + +SELECT AVG(runtime), genre 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 genre, AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY genre; + +SELECT genre, AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY genre HAVING COUNT(*) > 1; + +DELETE FROM movies WHERE rating='R'; \ No newline at end of file