From 534a1bdc9af5c3d27cf23221bb7884352d42862f Mon Sep 17 00:00:00 2001 From: AnataArisa <92080339+AnataAria@users.noreply.github.com> Date: Fri, 17 Nov 2023 01:35:19 +0700 Subject: [PATCH] vinh/remove column email on salary, add new column in instructor, adding new method for updating paypal email --- .../controller/InstructorController.java | 8 +++++++- .../drawingcouseselling/model/dto/InstructorDto.java | 5 +++-- .../drawingcouseselling/model/entity/Instructor.java | 12 +++++++++++- .../drawingcouseselling/model/entity/Salary.java | 10 ---------- .../service/InstructorService.java | 1 + .../service/impl/InstructorServiceImpl.java | 11 ++++++++++- .../service/impl/SalaryServiceImpl.java | 2 -- .../com/group1/drawingcouseselling/util/Tool.java | 7 +++++++ 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/InstructorController.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/InstructorController.java index 5bb5a3e4..0e6b9d55 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/InstructorController.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/controller/InstructorController.java @@ -4,7 +4,6 @@ import com.group1.drawingcouseselling.service.InstructorService; import com.group1.drawingcouseselling.service.JwtService; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -23,4 +22,11 @@ public ResponseEntity getInstructorInfo( String email = jwtService.extractUserEmail(instructorToken.substring(7)); return ResponseEntity.ok(instructorService.findInstructorDtoByInstructorEmail(email)); } + + @PostMapping("/instructor/paypal") + public ResponseEntity addPayPalEmail(@RequestHeader(value = "Authorization") String instructorToken, + @RequestParam(value = "paypalEmail") String paypalEmail){ + String email = jwtService.extractUserEmail(instructorToken.substring(7)); + return ResponseEntity.ok(instructorService.addInstructorPayPalEmail(email,paypalEmail)); + } } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/dto/InstructorDto.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/dto/InstructorDto.java index fa855e03..c6087dab 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/dto/InstructorDto.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/dto/InstructorDto.java @@ -5,12 +5,13 @@ import java.math.BigDecimal; @Builder -public record InstructorDto(BigDecimal id, String fullName, String specialization, String phone,String avatar) { - public InstructorDto(BigDecimal id, String fullName, String specialization, String phone, String avatar) { +public record InstructorDto(BigDecimal id, String fullName, String specialization, String phone,String avatar, String paypalEmail) { + public InstructorDto(BigDecimal id, String fullName, String specialization, String phone, String avatar, String paypalEmail) { this.id = id; this.fullName = fullName; this.specialization = specialization; this.phone = phone; this.avatar = avatar; + this.paypalEmail = paypalEmail; } } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Instructor.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Instructor.java index 50326391..5caeace8 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Instructor.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Instructor.java @@ -5,7 +5,6 @@ import com.group1.drawingcouseselling.util.ObjectMapper; import jakarta.persistence.*; -import java.io.Serializable; import java.math.BigDecimal; @Entity(name = "instructor") @@ -20,6 +19,8 @@ public class Instructor implements ObjectMapper { private String specialization; @Column(name ="phone_number") private String phone; + @Column(name = "paypal_email") + private String paypalEmail; @OneToOne(targetEntity = Account.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = false) @JoinColumn(name = "account_id") private Account account; @@ -79,6 +80,14 @@ public Instructor covertDtoToEntity(InstructorDto data) { return null; } + public String getPaypalEmail() { + return paypalEmail; + } + + public void setPaypalEmail(String paypalEmail) { + this.paypalEmail = paypalEmail; + } + @Override public InstructorDto convertEntityToDto(Instructor data) { return InstructorDto.builder() @@ -87,6 +96,7 @@ public InstructorDto convertEntityToDto(Instructor data) { .specialization(data.getSpecialization()) .phone(data.getPhone()) .avatar(data.getAvatar()) + .paypalEmail(data.getPaypalEmail()) .build(); } } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Salary.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Salary.java index e8847731..643208d4 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Salary.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/model/entity/Salary.java @@ -15,8 +15,6 @@ public class Salary { @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Instructor.class) @JoinColumn(name = "instructor_id", updatable = true, nullable = false) private Instructor instructor; - @Column(name = "email", nullable = false) - private String email; @Column(name = "amount", columnDefinition = "decimal(20,5)", nullable = false) private BigDecimal amount; @Column(name = "currency", nullable = false) @@ -46,14 +44,6 @@ public void setInstructor(Instructor instructor) { this.instructor = instructor; } - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - public BigDecimal getAmount() { return amount; } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/InstructorService.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/InstructorService.java index 96efb11c..9ad71e28 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/InstructorService.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/InstructorService.java @@ -12,4 +12,5 @@ public interface InstructorService { public Instructor findInstructorByInstructorEmail(String instructorEmail); public InstructorDto findInstructorDtoByInstructorEmail(String instructorEmail); public Page getInstructorOnPaging(Integer page, Integer maxPage); + public InstructorDto addInstructorPayPalEmail(String instructorEmail, String paypalEmail); } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/InstructorServiceImpl.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/InstructorServiceImpl.java index 42ab4f7c..6d9a0044 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/InstructorServiceImpl.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/InstructorServiceImpl.java @@ -7,6 +7,7 @@ import com.group1.drawingcouseselling.model.entity.Instructor; import com.group1.drawingcouseselling.repository.InstructorRepository; import com.group1.drawingcouseselling.service.InstructorService; +import com.group1.drawingcouseselling.util.Tool; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -44,5 +45,13 @@ public InstructorDto findInstructorDtoByInstructorEmail(String instructorEmail){ public Page getInstructorOnPaging(Integer page, Integer maxPage){ return instructorRepository.getInstructorsOnPaging(PageRequest.of(page,maxPage)); } - + @Override + public InstructorDto addInstructorPayPalEmail(String instructorEmail, String paypalEmail){ + var instructorInfo = instructorRepository.findInstructorByEmail(instructorEmail).orElseThrow( + () -> new UserNotFoundException("Not found this instructor in system") + ); + if(!Tool.isValidEmailAddress(paypalEmail)) throw new ValueIsInvalidException("Email is not valid"); + instructorInfo.setPaypalEmail(paypalEmail); + return new Instructor().convertEntityToDto(instructorRepository.save(instructorInfo)); + } } diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java index 3903478a..b6af2b00 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/service/impl/SalaryServiceImpl.java @@ -33,11 +33,9 @@ public void createMonthlySalaryForAllInstructor(){ do{ instructorPage.stream().forEach(instructor -> { var monthSalary = new Salary(); - var instructorAcc = instructor.getAccount(); monthSalary.setInstructor(instructor); monthSalary.setAmount(BigDecimal.ZERO); monthSalary.setCurrency("USD"); - monthSalary.setEmail(instructorAcc.getEmail()); monthSalary.setRecipientWallet("PayPal"); salaryRepository.save(monthSalary); }); diff --git a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/util/Tool.java b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/util/Tool.java index 32ab9eb9..38fac18c 100644 --- a/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/util/Tool.java +++ b/api/drawingcouseselling/src/main/java/com/group1/drawingcouseselling/util/Tool.java @@ -32,4 +32,11 @@ public static MultipartFile changeMultipartFileName(String name, MultipartFile c throw new SomethingWentWrongExceptions("Something wrong in processing of change name of file"); } } + + public static boolean isValidEmailAddress(String email) { + String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; + java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern); + java.util.regex.Matcher m = p.matcher(email); + return m.matches(); + } }