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
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/Home.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/HomeRepo.java
Original file line number Diff line number Diff line change
@@ -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<Home, Long> {

}
84 changes: 84 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/HomeService.java
Original file line number Diff line number Diff line change
@@ -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<Home> readAllHomes() {
Iterable<Home> homeIterable = repo.findAll();
List<Home> homeList = new ArrayList<>();
homeIterable.forEach(homeList::add);
return homeList;
}

public List<Home> readByAddress(String address) {
List<Home> homeList = new ArrayList<>();
readAllHomes().forEach(h -> {
if(h.getAddress().equals(address))
homeList.add(h);
});
return homeList;
}

public List<Home> readByHomeNumber(String homeNumber) {
List<Home> homeList = new ArrayList<>();
readAllHomes().forEach(h -> {
if(h.getAddress().equals(homeNumber))
homeList.add(h);
});
return homeList;
}

// not too confident about this one
// public List<Home> findHomeByPerson(Long id) {
// List<Home> 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<Home> delete(List<Home> listToDelete) {
listToDelete.forEach(repo::delete);
return listToDelete;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
86 changes: 86 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/PersonConfig.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
71 changes: 71 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/PersonController.java
Original file line number Diff line number Diff line change
@@ -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<Person> create(
@RequestBody Person person) {
return new ResponseEntity<>(service.create(person), HttpStatus.CREATED);
}

@RequestMapping(method = RequestMethod.PUT, value = "/people/u/{id}")
public ResponseEntity<Person> 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<Person> getPerson(
@PathVariable Long id) {
return new ResponseEntity<>(service.read(id), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/people/d/{id}")
public ResponseEntity<Person> delete(
@PathVariable Long id) {
return new ResponseEntity<>(service.delete(id), HttpStatus.ACCEPTED);
}

@RequestMapping(method = RequestMethod.GET, value = "/read")
public ResponseEntity<List<Person>> readAll() {
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, value = "/people/reverselookup/{mobileNumber}")
public ResponseEntity<List<Person>> findByMobile(
@PathVariable String phoneNumber) {
return new ResponseEntity<>(service.findByMobile(phoneNumber), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, value = "/people/surname/{lastName}")
public ResponseEntity<List<Person>> findByLastName(
@PathVariable String lastName) {
return new ResponseEntity<>(service.findAllByLastName(lastName), HttpStatus.OK);
}

// @RequestMapping(method = RequestMethod.GET, value = "/people/surname/{firstName}")
// public ResponseEntity<List<Person>> findByFirstName(@PathVariable String firstName) {
// return new ResponseEntity<>(service.findAllByLastName(firstName), HttpStatus.OK);
// }

// @RequestMapping(method = RequestMethod.GET, value = "/people/firstname/stats")
// public ResponseEntity<List<Person>> firstNameFrequency() {
//
// }
}
8 changes: 8 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/PersonRepo.java
Original file line number Diff line number Diff line change
@@ -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<Person, Long> {
}
Loading