diff --git a/pom.xml b/pom.xml index 274f418..e2e29a0 100644 --- a/pom.xml +++ b/pom.xml @@ -29,15 +29,21 @@ org.springframework.boot spring-boot-starter-data-jpa + org.springframework.boot - spring-boot-starter-jdbc + spring-boot-starter-web + - org.springframework.boot - spring-boot-starter-web + org.hibernate + hibernate-core + + + javax.xml.bind + jaxb-api + 2.3.0 - com.h2database h2 diff --git a/src/main/java/io/zipcoder/persistenceapp/Home.java b/src/main/java/io/zipcoder/persistenceapp/Home.java new file mode 100644 index 0000000..811c2df --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Home.java @@ -0,0 +1,47 @@ +package io.zipcoder.persistenceapp; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Home { + + @Id + private Long home_id; + private String address; + private String homeNumber; + + public Home() { + } + + public Home(Long home_id, String address, String homeNumber) { + this.home_id = home_id; + this.address = address; + this.homeNumber = homeNumber; + } + + public Long getHome_id() { + return home_id; + } + + public void setHome_id(Long home_id) { + this.home_id = home_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/HomeRepo.java b/src/main/java/io/zipcoder/persistenceapp/HomeRepo.java new file mode 100644 index 0000000..b1807d4 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/HomeRepo.java @@ -0,0 +1,9 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface HomeRepo extends CrudRepository { + +} diff --git a/src/main/java/io/zipcoder/persistenceapp/HomeService.java b/src/main/java/io/zipcoder/persistenceapp/HomeService.java new file mode 100644 index 0000000..84b8e65 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/HomeService.java @@ -0,0 +1,84 @@ +package io.zipcoder.persistenceapp; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class HomeService { + + @Autowired + private HomeRepo repo; +// @Autowired +// private Person person; + + public Home create(Home home) { + return repo.save(home); + } + + public Home read(Long homeId) { + return repo.findOne(homeId); + } + + public List readAllHomes() { + Iterable homeIterable = repo.findAll(); + List homeList = new ArrayList<>(); + homeIterable.forEach(homeList::add); + return homeList; + } + + public List readByAddress(String address) { + List homeList = new ArrayList<>(); + readAllHomes().forEach(h -> { + if(h.getAddress().equals(address)) + homeList.add(h); + }); + return homeList; + } + + public List readByHomeNumber(String homeNumber) { + List homeList = new ArrayList<>(); + readAllHomes().forEach(h -> { + if(h.getAddress().equals(homeNumber)) + homeList.add(h); + }); + return homeList; + } + + // not too confident about this one +// public List findHomeByPerson(Long id) { +// List homeList = new ArrayList<>(); +// readAllHomes().forEach(h -> { +// if (person.getHome_id().equals(id)) +// homeList.add(h); +// }); +// return homeList; +// } + + public Home update(Long homeId, Home theNewHome) { + Home homeInDb = read(homeId); + homeInDb.setAddress(theNewHome.getAddress()); + homeInDb.setHomeNumber(theNewHome.getHomeNumber()); + homeInDb = repo.save(homeInDb); + return homeInDb; + } + + public Home delete(Long id) { + Home homeInDb = read(id); + repo.delete(homeInDb); + return homeInDb; + } + + public Home delete(Home home) { + return delete(home.getHome_id()); + } + + public List delete(List listToDelete) { + listToDelete.forEach(repo::delete); + return listToDelete; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/NotFoundException.java b/src/main/java/io/zipcoder/persistenceapp/NotFoundException.java new file mode 100644 index 0000000..45333e1 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/NotFoundException.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Person not found") +public class NotFoundException extends RuntimeException { +} diff --git a/src/main/java/io/zipcoder/persistenceapp/Person.java b/src/main/java/io/zipcoder/persistenceapp/Person.java new file mode 100644 index 0000000..f0d0cf4 --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/Person.java @@ -0,0 +1,86 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.context.annotation.Bean; + +import javax.persistence.*; +import java.util.Date; + +@Entity +public class Person { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private Long id; + @Column(name = "firstName") + private String firstName; + @Column(name = "lastName") + private String lastName; + @Column(name = "mobile") + private String mobile; +// @Temporal(TemporalType.DATE) +// @Column(name = "birthDate") +// private Date birthDate; + @Column(name = "home_id") + private Long home_id; + + public Person() { + } + + public Person(Long id, String firstName, String lastName, String mobile, Long home_id) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.mobile = mobile; +// this.birthDate = birthDate; + this.home_id = home_id; + } + + public Long getId() { + return id; + } + + public void setId(Long 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 getBirthDate() { +// return birthDate; +// } +// +// public void setBirthDate(Date birthDate) { +// this.birthDate = birthDate; +// } + + public Long getHome_id() { + return home_id; + } + + public void setHome_id(Long home_id) { + this.home_id = home_id; + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonConfig.java b/src/main/java/io/zipcoder/persistenceapp/PersonConfig.java new file mode 100644 index 0000000..b44ee0a --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonConfig.java @@ -0,0 +1,22 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; + +@Configuration +public class PersonConfig { + + @Autowired + private PersonService service; + + @PostConstruct + public void setup() { + service.create(new Person(5L, "Josh", "Wilkins", "12435124", 12L)); + service.create(new Person(6L, "Raf", "Wilkins", "12435124", 12L)); + service.create(new Person(7L, "Vac", "Wilkins", "12435124", 12L)); + service.create(new Person(8L, "Ewda", "Wilkins", "12435124", 12L)); + service.create(new Person(9L, "Palsd", "Wilkins", "12435124", 12L)); + } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonController.java b/src/main/java/io/zipcoder/persistenceapp/PersonController.java new file mode 100644 index 0000000..bb1b70b --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonController.java @@ -0,0 +1,71 @@ +package io.zipcoder.persistenceapp; + + +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.servlet.mvc.annotation.ResponseStatusExceptionResolver; + +import javax.net.ssl.HttpsURLConnection; +import java.util.List; + +@RestController +@RequestMapping (value = "/person-controller") +public class PersonController { + + @Autowired + private PersonService service; + + @RequestMapping(method = RequestMethod.POST, value = "/create") + public ResponseEntity create( + @RequestBody Person person) { + return new ResponseEntity<>(service.create(person), HttpStatus.CREATED); + } + + @RequestMapping(method = RequestMethod.PUT, value = "/people/u/{id}") + public ResponseEntity update( + @PathVariable Long id, + @RequestBody Person person) { + return new ResponseEntity<>(service.update(id, person), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.GET, value = "/people/r/{id}") + public ResponseEntity getPerson( + @PathVariable Long id) { + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/people/d/{id}") + public ResponseEntity delete( + @PathVariable Long id) { + return new ResponseEntity<>(service.delete(id), HttpStatus.ACCEPTED); + } + + @RequestMapping(method = RequestMethod.GET, value = "/read") + public ResponseEntity> readAll() { + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.GET, value = "/people/reverselookup/{mobileNumber}") + public ResponseEntity> findByMobile( + @PathVariable String phoneNumber) { + return new ResponseEntity<>(service.findByMobile(phoneNumber), HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.GET, value = "/people/surname/{lastName}") + public ResponseEntity> findByLastName( + @PathVariable String lastName) { + return new ResponseEntity<>(service.findAllByLastName(lastName), HttpStatus.OK); + } + +// @RequestMapping(method = RequestMethod.GET, value = "/people/surname/{firstName}") +// public ResponseEntity> findByFirstName(@PathVariable String firstName) { +// return new ResponseEntity<>(service.findAllByLastName(firstName), HttpStatus.OK); +// } + +// @RequestMapping(method = RequestMethod.GET, value = "/people/firstname/stats") +// public ResponseEntity> firstNameFrequency() { +// +// } +} diff --git a/src/main/java/io/zipcoder/persistenceapp/PersonRepo.java b/src/main/java/io/zipcoder/persistenceapp/PersonRepo.java new file mode 100644 index 0000000..2313b4b --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonRepo.java @@ -0,0 +1,8 @@ +package io.zipcoder.persistenceapp; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonRepo extends CrudRepository { +} 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..c754d2c --- /dev/null +++ b/src/main/java/io/zipcoder/persistenceapp/PersonService.java @@ -0,0 +1,84 @@ +package io.zipcoder.persistenceapp; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +@Service +public class PersonService { + + @Autowired + private PersonRepo repo; + + public Person create(Person person) { + return repo.save(person); + } + + public Person read(Long id) { + return repo.findOne(id); + } + + public List findAllByNames(String firstName) { + List personList = new ArrayList<>(); + readAll().forEach(person -> { + if (person.getFirstName().equals(firstName)) + personList.add(person); + }); + return personList; + } + + public List findAllByLastName(String lastName) { + List personList = new ArrayList<>(); + readAll().forEach(person -> { + if (person.getLastName().equals(lastName)) + personList.add(person); + }); + return personList; + } + + public List findByMobile(String mobile) { + List result = new ArrayList<>(); + readAll().forEach(person -> { + if(person.getMobile().equals(mobile)) + result.add(person); + }); + return result; + } + + public List readAll() { + Iterable personIterable = repo.findAll(); + List personList = new ArrayList<>(); + personIterable.forEach(personList::add); + return personList; + } + + public Person update(Long id, Person newPersonData) { + Person personInDb = read(id); + personInDb.setFirstName(newPersonData.getFirstName()); + personInDb.setLastName(newPersonData.getLastName()); +// personInDb.setBirthDate(newPersonData.getBirthDate()); + personInDb.setMobile(newPersonData.getMobile()); + personInDb.setHome_id(newPersonData.getHome_id()); + personInDb = repo.save(personInDb); + return personInDb; + } + + public Person delete(Long id) { + Person personInDb = read(id); + repo.delete(personInDb); + return personInDb; + } + + public Person delete(Person person) { + return delete(person.getId()); + } + + public List delete(List listToDelete) { + listToDelete.forEach(repo::delete); + return listToDelete; + } +} diff --git a/src/main/resources/application-h2.properties b/src/main/resources/application-h2.properties index 74765cc..fb5afeb 100644 --- a/src/main/resources/application-h2.properties +++ b/src/main/resources/application-h2.properties @@ -1,4 +1,7 @@ -spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle spring.datasource.platform=h2 spring.jpa.hibernate.ddl-auto=none -spring.datasource.continue-on-error=true \ No newline at end of file +spring.datasource.continue-on-error=true +server.port=8080 +spring.datasource.url=jdbc:h2:mem:testdb +spring.h2.console.enabled=true +spring.h2.console.view=/h2-console diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7d4dc6f..223693f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,2 @@ spring.profiles.active=h2 logging.level.org.springframework.boot.context.embedded=INFO -spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect \ No newline at end of file diff --git a/src/main/resources/postman_collection.json b/src/main/resources/postman_collection.json new file mode 100644 index 0000000..3c5f1fb --- /dev/null +++ b/src/main/resources/postman_collection.json @@ -0,0 +1,54 @@ +{ + "info": { + "_postman_id": "1fd82683-c8da-40e7-8a19-5823fb30a4c3", + "name": "my-first-postman-collection", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "create", + "request": { + "method": "POST", + "header": [], + "url": null + }, + "response": [] + }, + { + "name": "read", + "request": { + "method": "GET", + "header": [], + "url": null + }, + "response": [] + }, + { + "name": "readAll", + "request": { + "method": "GET", + "header": [], + "url": null + }, + "response": [] + }, + { + "name": "update", + "request": { + "method": "GET", + "header": [], + "url": null + }, + "response": [] + }, + { + "name": "delete", + "request": { + "method": "GET", + "header": [], + "url": null + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/script.sql b/src/main/resources/script.sql new file mode 100644 index 0000000..876d203 --- /dev/null +++ b/src/main/resources/script.sql @@ -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 ('Monster Inc.', 92, 'Animation', 8.1, 'G'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES ('Pineapple Express', 120, 'Documentary', 9.9, 'R'); +INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES ('Wallys World', 15, 'Horror', 3.0, 'PG-13'); + +SELECT * FROM movies; +SELECT * FROM movies WHERE genre = 'Sci-Fi'; +SELECT * FROM movies WHERE imdb_score >= 6.5; +SELECT * FROM movies WHERE runtime < 100 AND rating IN ('G', 'PG'); + +SELECT AVG(runtime) as 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 IN ('Documentary', 'Horror'); +SELECT genre, AVG(imdb_score) AS Average, MIN(imdb_score) AS Min, MAX(imdb_score) AS Max FROM movies GROUP BY genre; +SELECT COUNT(rating) FROM movies HAVING COUNT(rating) > 1; +DELETE * FROM movies WHERE rating = 'R'; + +