Skip to content

Commit

Permalink
vinh/remove column email on salary, add new column in instructor, add…
Browse files Browse the repository at this point in the history
…ing new method for updating paypal email
  • Loading branch information
AnataAria committed Nov 16, 2023
1 parent 20b517c commit 534a1bd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -23,4 +22,11 @@ public ResponseEntity<InstructorDto> getInstructorInfo(
String email = jwtService.extractUserEmail(instructorToken.substring(7));
return ResponseEntity.ok(instructorService.findInstructorDtoByInstructorEmail(email));
}

@PostMapping("/instructor/paypal")
public ResponseEntity<InstructorDto> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.group1.drawingcouseselling.util.ObjectMapper;
import jakarta.persistence.*;

import java.io.Serializable;
import java.math.BigDecimal;

@Entity(name = "instructor")
Expand All @@ -20,6 +19,8 @@ public class Instructor implements ObjectMapper<Instructor, InstructorDto> {
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;
Expand Down Expand Up @@ -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()
Expand All @@ -87,6 +96,7 @@ public InstructorDto convertEntityToDto(Instructor data) {
.specialization(data.getSpecialization())
.phone(data.getPhone())
.avatar(data.getAvatar())
.paypalEmail(data.getPaypalEmail())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface InstructorService {
public Instructor findInstructorByInstructorEmail(String instructorEmail);
public InstructorDto findInstructorDtoByInstructorEmail(String instructorEmail);
public Page<Instructor> getInstructorOnPaging(Integer page, Integer maxPage);
public InstructorDto addInstructorPayPalEmail(String instructorEmail, String paypalEmail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,5 +45,13 @@ public InstructorDto findInstructorDtoByInstructorEmail(String instructorEmail){
public Page<Instructor> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 534a1bd

Please sign in to comment.