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
@@ -1,10 +1,12 @@
package io.zipcoder.persistenceapp;

import org.h2.server.web.WebServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication
public class PersistenceStarterApplication {
Expand All @@ -19,4 +21,7 @@ ServletRegistrationBean h2servletRegistration(){
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}

@Autowired
JdbcTemplate jdbcTemplate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.zipcoder.persistenceapp.controller;

import io.zipcoder.persistenceapp.domain.Person;
import io.zipcoder.persistenceapp.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class PersonController {


@Autowired
private PersonService personService;

@RequestMapping(value = "/people", method = RequestMethod.POST)
public ResponseEntity<?> createPerson(@RequestBody Person person) {
personService.addPerson(person);
return new ResponseEntity<>(HttpStatus.OK);
}

// @RequestMapping(value = "/people/{id}", method = RequestMethod.PUT)
// public ResponseEntity<?> updatePerson(@PathVariable Integer id, @RequestBody Person person) {
// Person person =
// }

@RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
public ResponseEntity<?> lookUpId(@PathVariable int id) {
Person person = personService.findById(id);
return new ResponseEntity<>(person, HttpStatus.OK);
}





}
8 changes: 8 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,8 @@
package io.zipcoder.persistenceapp.domain;

import javax.persistence.Entity;

@Entity
public class Home {

}
68 changes: 68 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,68 @@
package io.zipcoder.persistenceapp.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;

@Entity
public class Person {

@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
private String mobile;
private Date birthday;
private int homeId;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public int getHomeId() {
return homeId;
}

public void setHomeId(int homeId) {
this.homeId = homeId;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.persistenceapp.repository;

import io.zipcoder.persistenceapp.domain.Home;
import org.springframework.data.jpa.repository.JpaRepository;

public interface HomeRepository extends JpaRepository<Home, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.zipcoder.persistenceapp.repository;

import io.zipcoder.persistenceapp.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<Person, Long> {

}
18 changes: 18 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,18 @@
package io.zipcoder.persistenceapp.service;

import io.zipcoder.persistenceapp.domain.Person;

import java.util.List;

public interface HomeService {

List<Person> getAllPeople();

void deletePerson(int id);

void deletePeople(List<Person> cars);

void addPerson(Person person);

Person findById(int id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.zipcoder.persistenceapp.service;

import io.zipcoder.persistenceapp.domain.Person;
import org.h2.engine.Database;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.List;

public interface PersonService {

List<Person> getAllPeople();

void deletePerson(int id);

void deletePeople(List<Person> cars);

void addPerson(Person person);

Person findById(int id);



// POST /people -- create a person
// PUT /people/{id} -- update person with id. 404 error if that person doesn't exist yet
// GET /people/{id} -- get the person with the specified ID
// DELETE /people/{id} -- Delete the person with the specified ID
// GET /people -- get all people in the database
// GET /people/reverselookup/{mobileNumber} -- find all people with the specified mobile number
// GET /people/surname/{lastName} -- Find all people with a particular last name
// GET /people/surname -- Get the result of the surname report above
// GET /people/firstname/stats -- Get the report of first name frequencies

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.zipcoder.persistenceapp.service.jpa;

import io.zipcoder.persistenceapp.domain.Person;
import io.zipcoder.persistenceapp.service.HomeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class jpaHomeServiceImpl implements HomeService {

@Override
public List<Person> getAllPeople() {
return null;
}

@Override
public void deletePerson(int id) {

}

@Override
public void deletePeople(List<Person> cars) {

}

@Override
public void addPerson(Person person) {

}

@Override
public Person findById(int id) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.zipcoder.persistenceapp.service.jpa;

import io.zipcoder.persistenceapp.domain.Person;
import io.zipcoder.persistenceapp.repository.PersonRepository;
import io.zipcoder.persistenceapp.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class jpaPersonServiceImpl implements PersonService {

@Autowired
private PersonRepository personRepository;

@Override
public List<Person> getAllPeople() {
return null;
}

@Override
public void deletePerson(int id) {

}

@Override
public void deletePeople(List<Person> cars) {

}

@Override
public void addPerson(Person person) {

}

@Override
public Person findById(int id) {
return null;
}

}
10 changes: 10 additions & 0 deletions src/main/resources/schema-h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ CREATE TABLE movies (
rating VARCHAR2(10)
);

INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Lavalantula', 83, 'Horror', 4.7, 'TV-14');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Waltz With Bashir', 90, 'Documentary', 8.0, 'R');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Spaceballs', 96, 'Comedy', 7.1, 'PG');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Monsters Inc.', 92, 'Animation', 8.1 'G');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Ready Player One', 120, 'Animation', 8.4, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('A Quiet Place', 105, 'Horror', 8.2, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Isle of Dogs', 95, 'Animation', 9.0, 'G');

-- Tables for in-class example

DROP TABLE IF EXISTS cars;
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Lavalantula', 83, 'Horror', 4.7, 'TV-14');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Waltz With Bashir', 90, 'Documentary', 8.0, 'R');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Spaceballs', 96, 'Comedy', 7.1, 'PG');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Monsters Inc.', 92, 'Animation', 8.1 'G');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Ready Player One', 120, 'Animation', 8.4, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('A Quiet Place', 105, 'Horror', 8.2, 'PG-13');
INSERT INTO Movies (TITLE, RUNTIME, GENRE, IMDB_SCORE, RATING) VALUES ('Isle of Dogs', 95, 'Animation', 9.0, 'G');

SELECT * FROM Movies WHERE GENRE = 'Sci-Fi';
SELECT * FROM Movies WHERE IMDB_SCORE > 6.4;
SELECT * FROM Movies WHERE RATING = 'G' AND RUNTIME < 100 OR RATING = 'PG' AND RUNTIME < 100;
SELECT AVG(RUNTIME) FROM Movies WHERE IMDB_SCORE < 7.5 GROUP BY GENRE;

UPDATE Movies SET Rating = 'R' WHERE Title = 'Starship Troopers';

SELECT ID, Rating FROM Movies WHERE Genre = 'Horror' OR Genre = 'Documentary'
SELECT Rating, AVG(IMDB_Score), MIN(IMDB_SCORE), MAX(IMDB_SCORE) FROM Movies GROUP BY Rating;
SELECT Rating, AVG(IMDB_Score), MIN(IMDB_SCORE), MAX(IMDB_SCORE) FROM Movies GROUP BY Rating HAVING COUNT(RATING) > 1;

DELETE FROM Movies WHERE Rating = 'R';