function complete

This commit is contained in:
Juyounglee95
2020-04-22 19:23:59 +09:00
parent f66bc35409
commit 4377352510
20 changed files with 165 additions and 39 deletions

View File

@@ -8,40 +8,60 @@ import java.util.List;
@Table(name="BookRentalSystem_table")
public class BookRentalSystem {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String bookName;
private String bookStatus;
@PostPersist
public void onPostPersist(){
Reserved reserved = new Reserved();
BeanUtils.copyProperties(this, reserved);
reserved.publish();
//Following code causes dependency to external APIs
// it is NOT A GOOD PRACTICE. instead, Event-Policy mapping is recommended.
library.external.PointSystem pointSystem = new library.external.PointSystem();
// mappings goes here
Application.applicationContext.getBean(library.external.PointSystemService.class)
.usePoints(pointSystem);
ReservationCanceled reservationCanceled = new ReservationCanceled();
BeanUtils.copyProperties(this, reservationCanceled);
reservationCanceled.publish();
}
//책을 예약함
// @PostPersist
// public void bookReserved(){
// Reserved reserved = new Reserved();
// BeanUtils.copyProperties(this, reserved);
// reserved.publish();
//
// //Following code causes dependency to external APIs
// // it is NOT A GOOD PRACTICE. instead, Event-Policy mapping is recommended.
//
// library.external.PointSystem pointSystem = new library.external.PointSystem();
// // mappings goes here
// Application.applicationContext.getBean(library.external.PointSystemService.class)
// .usePoints(pointSystem);
//
//
//
//
//
// }
@PostUpdate
public void onPostUpdate(){
Returned returned = new Returned();
BeanUtils.copyProperties(this, returned);
returned.publish();
public void bookStatusUpdate(){
if(this.getBookStatus().equals("Returned")){
Returned returned = new Returned(this);
BeanUtils.copyProperties(this, returned);
returned.publish();
}else if(this.getBookStatus().equals("Canceled")){
ReservationCanceled reservationCanceled = new ReservationCanceled(this);
BeanUtils.copyProperties(this, reservationCanceled);
reservationCanceled.publish();
}else if(this.getBookStatus().equals("Reserved Complete")){
Reserved reserved = new Reserved(this);
BeanUtils.copyProperties(this, reserved);
reserved.publish();
}
}
// @PostUpdate
// public void onPostUpdate(){
// Returned returned = new Returned();
// BeanUtils.copyProperties(this, returned);
// returned.publish();
//
//
// }
public Long getId() {

View File

@@ -1,17 +1,49 @@
package library;
import library.external.PointSystem;
import library.external.PointSystemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@RestController
public class BookRentalSystemController {
@Autowired
private BookRentalSystemRepository bookRentalSystemRepository;
@Autowired
private PointSystemService pointSystemService;
@PutMapping("/bookRentalSystems/returned/{id}")
public void bookReturned(@PathVariable(value = "id") Long id){
BookRentalSystem bookRentalSystem = bookRentalSystemRepository.findById(id).get();
bookRentalSystem.setBookStatus("Returned");
bookRentalSystemRepository.save(bookRentalSystem);
}
@PostMapping("/bookRentalSystems/reserveCanceled/{id}")
public void bookReserveCanceled(@PathVariable(value = "id")Long id){
BookRentalSystem bookRentalSystem = bookRentalSystemRepository.findById(id).get();
bookRentalSystem.setBookStatus("Canceled");
bookRentalSystemRepository.save(bookRentalSystem);
}
@PostMapping("/bookRentalSystems/reserve/{id}")
public void bookReserve(@PathVariable(value="id")Long id){
PointSystem pointSystem = new PointSystem();
pointSystem.setBookId(id);
PointSystemService pointSystemService = Application.applicationContext.
getBean(library.external.PointSystemService.class);
pointSystemService.usePoints(pointSystem);
}
}

View File

@@ -4,12 +4,21 @@ package library;
public class PointUsed extends AbstractEvent {
private Long id;
private Long bookId;
private Long pointQty;
public Long getId() {
return id;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public void setId(Long id) {
this.id = id;
}
@@ -20,4 +29,4 @@ public class PointUsed extends AbstractEvent {
public void setPointQty(Long pointQty) {
this.pointQty = pointQty;
}
}
}

View File

@@ -8,22 +8,43 @@ import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
import java.awt.print.Book;
@Service
public class PolicyHandler{
@StreamListener(KafkaProcessor.INPUT)
public void wheneverPointUsed_ChangeStatus(@Payload PointUsed pointUsed){
if(pointUsed.isMe()){
System.out.println("##### listener ChangeStatus : " + pointUsed.toJson());
@Autowired
BookRentalSystemRepository bookRentalSystemRepository;
@StreamListener(KafkaProcessor.INPUT) //포인트 결제 완료시
public void wheneverPointUsed_ChangeStatus(@Payload PointUsed pointUsed){
try {
if (pointUsed.isMe()) {
System.out.println("##### point use completed : " + pointUsed.toJson());
BookRentalSystem bookRentalSystem = bookRentalSystemRepository.findById(pointUsed.getBookId()).get();
bookRentalSystem.setBookStatus("Reserved Complete");
bookRentalSystemRepository.save(bookRentalSystem);
}
}catch (Exception e){
e.printStackTrace();
}
}
@StreamListener(KafkaProcessor.INPUT)
@StreamListener(KafkaProcessor.INPUT) //관리자가 도서 신규 등록 시
public void wheneverRegistered_ChangeStatus(@Payload Registered registered){
try{
if(registered.isMe()){
System.out.print("####book registered: " + registered.toJson());
BookRentalSystem bookRentalSystem = new BookRentalSystem();
bookRentalSystem.setId(registered.getId());
bookRentalSystem.setBookName(registered.getBookName());
bookRentalSystem.setBookStatus(Registered.class.getSimpleName());
bookRentalSystemRepository.save(bookRentalSystem);
if(registered.isMe()){
System.out.println("##### listener ChangeStatus : " + registered.toJson());
}
}catch (Exception e){
e.printStackTrace();
}
}
}

View File

@@ -6,6 +6,13 @@ public class ReservationCanceled extends AbstractEvent {
private String bookName;
private String bookStatus;
public ReservationCanceled(BookRentalSystem bookRentalSystem){
this();
this.setId(bookRentalSystem.getId());
this.setBookName(bookRentalSystem.getBookName());
this.setBookStatus(bookRentalSystem.getBookStatus());
}
public ReservationCanceled(){
super();
}

View File

@@ -6,6 +6,13 @@ public class Reserved extends AbstractEvent {
private String bookName;
private String bookStatus;
public Reserved(BookRentalSystem bookRentalSystem){
this();
this.setId(bookRentalSystem.getId());
this.setBookName(bookRentalSystem.getBookName());
this.setBookStatus(bookRentalSystem.getBookStatus());
}
public Reserved(){
super();
}

View File

@@ -10,6 +10,13 @@ public class Returned extends AbstractEvent {
super();
}
public Returned(BookRentalSystem bookRentalSystem){
this();
this.setId(bookRentalSystem.getId());
this.setBookStatus(bookRentalSystem.getBookStatus());
this.setBookName(bookRentalSystem.getBookName());
}
public Long getId() {
return id;
}

View File

@@ -3,12 +3,21 @@ package library.external;
public class PointSystem {
private Long id;
private Long bookId;
public Long getId() {
return id;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public void setId(Long id) {
this.id = id;
}

View File

@@ -12,10 +12,10 @@ import java.util.Date;
* Created by uengine on 2018. 11. 21..
*/
@FeignClient(name="point", url="http://point:8080")
@FeignClient(name="point", url="http://localhost:8084")
public interface PointSystemService {
@RequestMapping(method= RequestMethod.POST, path="/pointSystems")
@RequestMapping(method= RequestMethod.POST, path="/pointSystems", consumes = "application/json")
public void usePoints(@RequestBody PointSystem pointSystem);
}

Binary file not shown.

View File

@@ -0,0 +1,14 @@
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\BookRentalSystemRepository.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\ReservationCanceled.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\Application.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\config\kafka\KafkaProcessor.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\AbstractEvent.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\external\PointSystem.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\BookRentalSystem.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\external\PointSystemService.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\PolicyHandler.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\Reserved.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\BookRentalSystemController.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\Returned.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\PointUsed.java
C:\Users\SKCC\Desktop\library\bookRental\src\main\java\library\Registered.java