Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.zipcoder.persistenceapp;

public class PersonRepository {
}
46 changes: 46 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/domain/Home.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
81 changes: 81 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/domain/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
57 changes: 57 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/service/HomeService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
111 changes: 111 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/service/PersonService.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions src/main/resources/schema-h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 22 additions & 0 deletions src/main/resources/script.sql
Original file line number Diff line number Diff line change
@@ -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';