Skip to content

Commit

Permalink
create,update vaildation grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
mintaek22 committed Oct 17, 2023
1 parent 0267403 commit db07fa1
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class GuestController {

private final GuestService guestService;

//guest 정보 조희
@GetMapping("/guest/{id}")
public ResponseEntity<ResGuestDto> getGuestById(@PathVariable Long id) {

public ResponseEntity<ResGuestDto> getGuest(@PathVariable Long id) {
ResGuestDto resGuestDto = guestService.getGuestById(id);
return ResponseEntity.ok(resGuestDto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,50 @@
package com.Han2m.portLogistics.user.controller;

import com.Han2m.portLogistics.user.dto.req.CreateGroup;
import com.Han2m.portLogistics.user.dto.req.ReqWorkerDto;
import com.Han2m.portLogistics.user.dto.res.ResWorkerDto;
import com.Han2m.portLogistics.user.entity.Worker;
import com.Han2m.portLogistics.user.service.S3Service;
import com.Han2m.portLogistics.user.service.WorkerService;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class WorkerController {

private final WorkerService workerService;
private final S3Service s3Service;


// Worker 조회
@GetMapping("/worker/{id}")
public ResponseEntity<ResWorkerDto> getWorkerById(@PathVariable Long id) {
public ResponseEntity<ResWorkerDto> getWorker(@PathVariable Long id) {

ResWorkerDto resWorkerDto = workerService.getWorkerById(id);

return ResponseEntity.ok(resWorkerDto);
}

//TODO IOException 예외처리 확실하게
//Worker 등록
@PostMapping("/worker/register")
public ResponseEntity<ResWorkerDto> registerWorker(@RequestParam MultipartFile faceImg,
@RequestPart @Validated ReqWorkerDto reqWorkerDto) throws IOException {

Worker worker = workerService.registerWorker(reqWorkerDto);
@RequestPart @Validated(CreateGroup.class) ReqWorkerDto reqWorkerDto) throws IOException {

// 얼굴 이미지 s3에 저장
String faceUrl = s3Service.uploadFaceImg(faceImg,worker.getPersonId());

ResWorkerDto resWorkerDto = workerService.registerWorkerUrl(worker,faceUrl);
ResWorkerDto resWorkerDto = workerService.registerWorker(faceImg,reqWorkerDto);

return ResponseEntity.ok(resWorkerDto);
}



// Worker 수정
//Worker 수정
@PutMapping("/worker/edit/{id}")
public ResponseEntity<ResWorkerDto> updateWorker(@PathVariable Long id, @RequestParam MultipartFile faceImg,
@RequestPart @Validated ReqWorkerDto reqWorkerDto) throws IOException {

Worker worker = workerService.editWorkerInfo(id, reqWorkerDto);

// 얼굴 이미지 s3에 저장
String faceUrl = s3Service.uploadFaceImg(faceImg, worker.getPersonId());

ResWorkerDto resWorkerDto = workerService.registerWorkerUrl(worker, faceUrl);
ResWorkerDto resWorkerDto = workerService.editWorker(id,faceImg ,reqWorkerDto);

return ResponseEntity.ok(resWorkerDto);
}
Expand All @@ -74,20 +57,20 @@ public ResponseEntity<? extends HttpEntity> deleteWorkerById(@PathVariable Long
return ResponseEntity.noContent().build();
}


//직원들 조회
@GetMapping("/workers")
public ResponseEntity<Page<ResWorkerDto>> searchAllWorkers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
Page<ResWorkerDto> workers = workerService.getAllWorkers(pageable);
return ResponseEntity.ok(workers);
}

//직원 이름으로 조회
@GetMapping("/worker/search")
public ResponseEntity<List<ResWorkerDto>> searchPersonsByName(@RequestParam String name) {
List<ResWorkerDto> workers = workerService.searchWorkerByName(name);
return ResponseEntity.ok(workers);
}
//
// //직원들 조회
// @GetMapping("/workers")
// public ResponseEntity<Page<ResWorkerDto>> searchAllWorkers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
// Pageable pageable = PageRequest.of(page, size);
// Page<ResWorkerDto> workers = workerService.getAllWorkers(pageable);
// return ResponseEntity.ok(workers);
// }
//
// //직원 이름으로 조회
// @GetMapping("/worker/search")
// public ResponseEntity<List<ResWorkerDto>> searchPersonsByName(@RequestParam String name) {
// List<ResWorkerDto> workers = workerService.searchWorkerByName(name);
// return ResponseEntity.ok(workers);
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.Han2m.portLogistics.user.dto.req;

public interface CreateGroup {
}
34 changes: 24 additions & 10 deletions src/main/java/com/Han2m/portLogistics/user/dto/req/ReqGuestDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Han2m.portLogistics.user.dto.req;

import com.Han2m.portLogistics.user.entity.Guest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -15,27 +16,40 @@
@NoArgsConstructor
public class ReqGuestDto{

@NotBlank(message = "nationality은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "nationality은 필수값 입니다")
private String nationality;
@NotBlank(message = "name은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "name은 필수값 입니다")
private String name;
@NotNull(message = "sex은 필수값 입니다")
@NotNull(groups = {CreateGroup.class},message = "sex은 필수값 입니다")
private Boolean sex;
@NotBlank(message = "birth은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "birth은 필수값 입니다")
@Size(min = 11,max = 11,message = "생년월일은 8자리 입니다") // ex. 19990101
private String birth;
@NotBlank(message = "phone은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "phone은 필수값 입니다")
@Size(min = 14,max = 14,message = "핸드폰 번호는 11자리 입니다") // ex. 01012345678
private String phone;
@NotNull(message = "date는 필수값 입니다")
@NotNull(groups = {CreateGroup.class},message = "date는 필수값 입니다")
private LocalDate date;
@NotBlank(message = "reason은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "reason은 필수값 입니다")
private String reason;
@NotBlank(message = "goal은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "goal은 필수값 입니다")
private String goal;
@NotNull(message = "workerId는 필수값 입니다")
@NotNull(groups = {CreateGroup.class},message = "workerId는 필수값 입니다")
private Long workerId;
@NotNull(message = "wharfs은 필수값 입니다")
@NotNull(groups = {CreateGroup.class},message = "wharfs은 필수값 입니다")
private List<String> wharfs;

public Guest toEntity(){
return Guest.builder().
nationality(nationality).
name(name).
birth(birth).
sex(sex).
phone(phone).
reason(reason).
goal(goal).
date(date).
build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Han2m.portLogistics.user.dto.req;

import com.Han2m.portLogistics.user.entity.Worker;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -12,20 +13,34 @@
@Setter
public class ReqWorkerDto {

@NotBlank(message = "nationality은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class}, message = "nationality은 필수값 입니다")
private String nationality;
@NotBlank(message = "name은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "name은 필수값 입니다")
private String name;
@NotNull(message = "sex은 필수값 입니다")
@NotNull(groups = {CreateGroup.class},message = "sex은 필수값 입니다")
private Boolean sex;
@NotBlank(message = "birth은 필수값 입니다")
@Size(min = 10,max = 10,message = "생년월일은 8자리 입니다")
@NotBlank(groups = {CreateGroup.class},message = "birth은 필수값 입니다")
@Size(groups = {CreateGroup.class},min = 10,max = 10,message = "생년월일은 8자리 입니다")
private String birth;
@NotBlank(message = "phone은 필수값 입니다")
@Size(min =13,max = 13,message = "핸드폰 번호는 11자리 입니다")
@NotBlank(groups = {CreateGroup.class},message = "phone은 필수값 입니다")
@Size(groups = {CreateGroup.class},min =13,max = 13,message = "핸드폰 번호는 11자리 입니다")
private String phone;
@NotBlank(message = "position은 필수값 입니다")
@NotBlank(groups = {CreateGroup.class},message = "position은 필수값 입니다")
private String position;
@NotNull(message = "wharfs은 필수값 입니다")

//wharfs name이 들어온다
@NotNull(groups = {CreateGroup.class},message = "wharfs은 필수값 입니다")
private List<String> wharfs;

public Worker toEntity(){
return Worker.builder().
nationality(nationality).
name(name).
position(position).
birth(birth).
sex(sex).
phone(phone).
build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.Han2m.portLogistics.user.dto.req;

public interface UpdateGroup {
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.Han2m.portLogistics.user.dto.res;

import com.Han2m.portLogistics.user.entity.Guest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ResGuestDto{

private String nationality;
Expand All @@ -25,18 +26,6 @@ public class ResGuestDto{
private Long workerId;
private List<String> wharfs;

public ResGuestDto(Guest guest) {
this.nationality = guest.getNationality();
this.name = guest.getName();
this.sex = guest.getSex();
this.birth = guest.getBirth();
this.phone = guest.getPhone();
this.personId = guest.getPersonId();
this.date = guest.getDate();
this.reason = guest.getReason();
this.goal = guest.getGoal();
this.workerId = guest.getWorker().getPersonId();
this.wharfs = guest.getWharfPlaces();
}


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.Han2m.portLogistics.user.dto.res;

import com.Han2m.portLogistics.user.entity.Worker;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@Builder
public class ResWorkerDto{

private String nationality;
Expand All @@ -20,15 +23,4 @@ public class ResWorkerDto{
private String faceUrl;
private List<String> wharfs;

public ResWorkerDto(Worker worker) {
nationality = worker.getNationality();
name = worker.getName();
sex = worker.getSex();
birth = worker.getBirth();
phone = worker.getPhone();
personId = worker.getPersonId();
position = worker.getPosition();
faceUrl = worker.getFaceUrl();
wharfs = worker.getWharfPlaces();
}
}
46 changes: 44 additions & 2 deletions src/main/java/com/Han2m/portLogistics/user/entity/Guest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.Han2m.portLogistics.user.entity;


import com.Han2m.portLogistics.user.dto.req.ReqGuestDto;
import com.Han2m.portLogistics.user.dto.res.ResGuestDto;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Entity
@Getter
@Setter
@NoArgsConstructor
@DiscriminatorValue("guest")
public class Guest extends Person{
Expand All @@ -32,5 +35,44 @@ public class Guest extends Person{
//담당자
private Worker worker;

public void setWorker(Worker worker) {
this.worker = worker;
}

@Builder
public Guest(String nationality, String name, Boolean sex, String birth, String phone, List<Wharf> wharfList, String reason, String goal, LocalDate date, Worker worker) {
super(nationality, name, sex, birth, phone);
this.reason = reason;
this.goal = goal;
this.date = date;
this.worker = worker;
}

public void updateGuest(ReqGuestDto reqGuestDto){
Optional<ReqGuestDto> optionalReqGuestDto = Optional.ofNullable(reqGuestDto);

optionalReqGuestDto.map(ReqGuestDto::getName).ifPresent(this::setName);
optionalReqGuestDto.map(ReqGuestDto::getPhone).ifPresent(this::setPhone);
optionalReqGuestDto.map(ReqGuestDto::getSex).ifPresent(this::setSex);
optionalReqGuestDto.map(ReqGuestDto::getNationality).ifPresent(this::setNationality);
optionalReqGuestDto.map(ReqGuestDto::getBirth).ifPresent(this::setBirth);
optionalReqGuestDto.map(ReqGuestDto::getGoal).ifPresent((goal -> this.goal = goal));
optionalReqGuestDto.map(ReqGuestDto::getReason).ifPresent((reason -> this.reason = reason));
optionalReqGuestDto.map(ReqGuestDto::getDate).ifPresent(date -> this.date = date);
}

public ResGuestDto toResGuestDto(){
return ResGuestDto.builder().
personId(getPersonId()).
birth(getBirth()).
sex(getSex()).
name(getName()).
nationality(getNationality()).
phone(getPhone()).
goal(getGoal()).
reason(getReason()).
date(getDate()).
wharfs(getWharfList().stream().map(Wharf::getName).toList()).build();
}

}
Loading

0 comments on commit db07fa1

Please sign in to comment.