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
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
@Table(name = "HOME")
public class Home {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String address;
private int homeNumber;

public Home() {
}

public Home(Long id, String address, int homeNumber) {
this.id = id;
this.address = address;
this.homeNumber = homeNumber;
}

public Long getId() {
return id;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getHomeNumber() {
return homeNumber;
}

public void setHomeNumber(int homeNumber) {
this.homeNumber = homeNumber;
}
}
62 changes: 62 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 java.util.List;

@RestController
public class HomeController {


private HomeService homeService;

@Autowired
public HomeController(HomeService homeService) {
this.homeService = homeService;
}

@RequestMapping(value = "/homes", method = RequestMethod.POST)
public ResponseEntity<Home> createPerson(@RequestBody Home home) {
return new ResponseEntity<>(this.homeService.insertHome(home), HttpStatus.CREATED);
}

@RequestMapping(value = "/homeUp", method = RequestMethod.PUT)
public ResponseEntity<Home> updateHome(@RequestBody Home homeUpdated) {
return new ResponseEntity<>(this.homeService.updateHome(homeUpdated), HttpStatus.OK);
}

@RequestMapping(value = "/home/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Home> deletePerson(@PathVariable Long id) {
this.homeService.removeHome(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@RequestMapping(value = "/home/delete-list", method = RequestMethod.DELETE)
public ResponseEntity<List<Home>> deleteManyHomes(@RequestBody List<Home> homes) {
this.homeService.removeHomes(homes);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@RequestMapping(value = "/home/id/{id}", method = RequestMethod.GET)
public ResponseEntity<Home> findHomeById(@PathVariable Long id) {
return new ResponseEntity<>(this.homeService.findHomeById(id), HttpStatus.OK);
}

@RequestMapping(value = "/home", method = RequestMethod.GET)
public ResponseEntity<List<Home>> findAllHomes() {
return new ResponseEntity<>(this.homeService.findAllHomes(), HttpStatus.OK);
}

@RequestMapping(value = "/home/home-number/{homeNumber}", method = RequestMethod.GET)
public ResponseEntity<Home> findHomeByHomeNumber(@PathVariable String homeNumber) {
return new ResponseEntity<>(this.homeService.findHomeByHomeNumber(homeNumber), HttpStatus.OK);
}

@RequestMapping(value = "/home/address/{address}", method = RequestMethod.GET)
public ResponseEntity<Home> findHomeByAddress(@PathVariable String address) {
return new ResponseEntity<>(this.homeService.findHomeByAddress(address), HttpStatus.OK);
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/HomeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zipcoder.persistenceapp;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface HomeRepository extends JpaRepository<Home, Long> {

Home findById(Long Id);
Home findByHomeNumber(String homeNumber);
Home findByAddress(String address);



}
85 changes: 85 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/HomeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.zipcoder.persistenceapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeService {

@Autowired
private HomeRepository homeRepository;


public HomeService(HomeRepository homeRepository) {
this.homeRepository = homeRepository;
}

public List<Home> findAllHomes() {
return this.homeRepository.findAll();
}

public Home insertHome(Home home) {
return this.homeRepository.save(home);
}

public void removeHome(Long id) {
this.homeRepository.delete(id);
}

public void removeHomes(List<Home> homes) {
this.homeRepository.delete(homes);
}

public Home updateHome(Home home) {
Home toUpdate = this.homeRepository.getOne(home.getId());// findById(home.getId());
toUpdate.setAddress(home.getAddress());
toUpdate.setHomeNumber(home.getHomeNumber());
return this.homeRepository.save(toUpdate);
}

public Home findHomeById(Long id) {
return this.homeRepository.findById(id);
}

public Home findHomeByHomeNumber(String homeNumber) {
return this.homeRepository.findByHomeNumber(homeNumber);
}

public Home findHomeByAddress(String address) {
return this.homeRepository.findByAddress(address);
}

}

// public Person create(Person person) {
// return repository.save(person);
// }
//
// public Person findById(Long id) {
// return repository.findOne(id);
// }
//
// public List<Person> readAll() {
// Iterable<Person> personIterable = repository.findAll();
// List<Person> result = new ArrayList<>();
// personIterable.forEach(result::add);
// return result;
// }
//
// public Person update(Long id, Person newPersonData) {
// Person personInDatabase = findById(id);
// newPersonData.setId(personInDatabase.getId());
// return repository.save(newPersonData);
// }
//
// public Person delete(Person person) {
// repository.delete(person);
// return person;
// }
//
// public Person delete(Long id) {
// return delete(findById(id));
// }

80 changes: 80 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.zipcoder.persistenceapp;


import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "PERSON")
public class Person {

@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Long id;
private Long homeId;
private String firstName;
private String lastName;
private Date birthday;
private String mobile;

public Person() {
}

public Person(Long id, Long homeId, String firstName, String lastName, Date birthday, String mobile) {
this.id = id;
this.homeId = homeId;
this.firstName = firstName;
this.lastName = lastName;
this.birthday = birthday;
this.mobile = mobile;
}

public Long getId() {
return id;
}

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

public Long getHomeId() {
return homeId;
}

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

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 Date getBirthday() {
return birthday;
}

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

public String getMobile() {
return mobile;
}

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

}
Loading