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
13 changes: 13 additions & 0 deletions data-h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('S
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 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'),
('Spaceballs', 96, 'Comedy', 7.1,'PG'),
('Monsters Inc.', 92, 'Animation', 8.1, 'G');

INSERT INTO movies (title, runtime, genre, imdb_score, rating) VALUES
('Zardoz', 105, 'Sci-Fi', 5.8, 'R'),
('The Room', 99, 'Comedy', 3.6, 'R'),
('Mad Max: Fury Road', 120, 'Action', 8.1, 'R');


5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.zipcoder.persistenceapp.cache;

import io.zipcoder.persistenceapp.domain.Person;
import org.springframework.context.annotation.Bean;

import java.util.List;


public interface PersonSurnameSearchCache {

public List<Person> getCache();
public void setCache(List<Person> cache);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.zipcoder.persistenceapp.cache;

import io.zipcoder.persistenceapp.domain.Person;
import org.hibernate.annotations.Cache;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;


public class PersonSurnameSearchCacheImp implements PersonSurnameSearchCache {
private List<Person> cache = new ArrayList<>();

public List<Person> getCache() {
return cache;
}

public void setCache(List<Person> cache) {
this.cache = cache;
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zipcoder.persistenceapp.config;

import io.zipcoder.persistenceapp.cache.PersonSurnameSearchCache;
import io.zipcoder.persistenceapp.cache.PersonSurnameSearchCacheImp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public PersonSurnameSearchCache personSurnameSearchCache(){
return new PersonSurnameSearchCacheImp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.zipcoder.persistenceapp.controller;


import io.zipcoder.persistenceapp.domain.Home;
import io.zipcoder.persistenceapp.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

@Autowired
HomeService service;

@RequestMapping(value = "/home/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getHomeById(@PathVariable int id){
try {
Home home = service.getHomeById(id);
return new ResponseEntity<>(home, HttpStatus.OK);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("Home Not Found In Database",HttpStatus.NOT_FOUND);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class PersonController {

@Autowired
PersonService service;

@RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getPersonById(@PathVariable int id){
try {
Person person = service.getPersonById(id);
return new ResponseEntity<>(person, HttpStatus.OK);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("Person Not Found In Database",HttpStatus.NOT_FOUND);
}
}

@RequestMapping(value = "/person", method = RequestMethod.GET)
public ResponseEntity<?> getAllPersons () {
try{
List<Person> people = service.getAllPerson();
return new ResponseEntity<>(people, HttpStatus.OK);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("No People Currently In Database", HttpStatus.NOT_FOUND);
}
}

@RequestMapping(value = "/person/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deletePerson(@PathVariable int id) {
try {
service.deletePerson(id);
return new ResponseEntity<>(HttpStatus.OK);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("Person Not Found In Database",HttpStatus.NOT_FOUND);
}
}

@RequestMapping(value = "/person/reverselookup/{mobile}", method = RequestMethod.GET)
public ResponseEntity<?> getPersonByMobile(@PathVariable String mobile){
try {
Person person = service.getPersonByMobile(mobile);
return new ResponseEntity<>(person, HttpStatus.OK);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("Person Not Found In Database",HttpStatus.NOT_FOUND);
}
}

@RequestMapping(value = "/person/surname/{lastName}", method = RequestMethod.GET)
public ResponseEntity<?> getAllPersonsWithSurname (@PathVariable String lastName) {

List<Person> people = service.getAllPersonWithSurname(lastName);
if (people.size() == 0){
return new ResponseEntity<>("No People With That Surname In Database", HttpStatus.NOT_FOUND);
}
else {
return new ResponseEntity<>(people, HttpStatus.OK);
}
}

@RequestMapping(value = "/person/surname", method = RequestMethod.GET)
public ResponseEntity<?> getSurnameCache(){
List<Person> people = service.getSurnameCache();
if (people.size() == 0){
return new ResponseEntity<>("No Previous Search To Reference", HttpStatus.NOT_FOUND);
}
else {
return new ResponseEntity<>(people, HttpStatus.OK);
}
}

@RequestMapping(value = "/person/firstname/stats", method = RequestMethod.GET)
public ResponseEntity<?> getFirstNameStats () {
Map<String,Number> results = service.getFirstNameStats();
if(results.keySet().size() == 0){
return new ResponseEntity<>("No People In Database", HttpStatus.NOT_FOUND);
}
else{
return new ResponseEntity<>(results, HttpStatus.OK);
}
}

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

@RequestMapping(value = "/person/{id}", method = RequestMethod.PUT)
public ResponseEntity<?> updatePerson(@PathVariable int id, @RequestBody Person person){
try {
service.getPersonById(id);
}
catch(EmptyResultDataAccessException e){
return new ResponseEntity<>("Person Not Found In Database",HttpStatus.NOT_FOUND);
}
service.updatePerson(person, id);
return new ResponseEntity<>(HttpStatus.OK);
}

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

public class Home {
private int id;
private String address;
private String homeNumber;

public int getId() {
return id;
}

public void setId(int 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;
}
}
65 changes: 65 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,65 @@
package io.zipcoder.persistenceapp.domain;

import javax.persistence.Entity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Person {
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,19 @@
package io.zipcoder.persistenceapp.rowmapper;

import io.zipcoder.persistenceapp.domain.Home;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class HomeRowMapper implements RowMapper {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Home home = new Home();
home.setId(rs.getInt("ID"));
home.setAddress(rs.getString("ADDRESS"));
home.setHomeNumber(rs.getString("HOMENUMBER"));

return home;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.zipcoder.persistenceapp.rowmapper;


import io.zipcoder.persistenceapp.domain.Person;
import org.springframework.jdbc.core.RowMapper;

import javax.swing.tree.TreePath;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PersonRowMapper implements RowMapper {

@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Person person = new Person();
person.setId(rs.getInt("ID"));
person.setFirstName(rs.getString("FIRST_NAME"));
person.setLastName(rs.getString("LAST_NAME"));
person.setMobile(rs.getString("MOBILE"));
person.setBirthday(rs.getDate("BIRTHDAY"));
person.setHomeId(rs.getInt("HOME_ID"));

return person;
}
}
Loading