Skip to content

Commit

Permalink
Merge pull request #103 from Team-UMC/refactor/#96/branch
Browse files Browse the repository at this point in the history
[REFACTOR] Branch API 리펙토링
  • Loading branch information
ShimFFF committed May 9, 2024
2 parents 1ecc85f + a56429a commit a2cba16
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Tag(name = "운영진용 지부 API", description = "운영진용 지부 관련 API")
Expand All @@ -30,21 +34,19 @@ public class StaffBranchController {
private final BranchServiceImpl branchService;
private final BranchUniversityServiceImpl branchUniversityService;

//todo: 다음 기수 생성하기?, 현재 진행중인 기수 바꾸기? api 추가하기 (현재 기수 isActive false로 만들기)

@Operation(summary = "지부 생성 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "BRANCH005", description = "지부 저장에 실패"),
@ApiResponse(responseCode = "BRANCH002", description = "지부 이름이 비어있는 경우"),
@ApiResponse(responseCode = "BRANCH003", description = "지부 설명이 비어있는 경우")
})
@PostMapping("")
@PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public BaseResponse<BranchResponse.BranchId> postBranch(
@CurrentMember Member member,
@RequestBody BranchRequest.BranchInfoDTO request
@Valid @RequestPart("request") BranchRequest.BranchInfoDTO request,
@RequestPart(name = "file", required = false) MultipartFile file
){
return BaseResponse.onSuccess(branchService.postBranch(request));
return BaseResponse.onSuccess(branchService.postBranch(request, file));
}

@Operation(summary = "지부 수정 API")
Expand All @@ -54,13 +56,13 @@ public BaseResponse<BranchResponse.BranchId> postBranch(
@ApiResponse(responseCode = "BRANCH002", description = "지부 이름이 비어있는 경우"),
@ApiResponse(responseCode = "BRANCH003", description = "지부 설명이 비어있는 경우")
})
@PatchMapping("/{branchId}")
@PatchMapping(value = "/{branchId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public BaseResponse<BranchResponse.BranchId> patchBranch(
@CurrentMember Member member,
@PathVariable("branchId") @ExistBranch UUID branchId,
@RequestBody BranchRequest.BranchInfoDTO request
@Valid @RequestPart("request") BranchRequest.BranchInfoDTO request,
@RequestPart(name = "file", required = false) MultipartFile file
){
return BaseResponse.onSuccess(branchService.patchBranch(request, branchId));
return BaseResponse.onSuccess(branchService.patchBranch(request, branchId, file));
}

@Operation(summary = "지부 삭제 API")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public static class BranchInfoDTO{
private String name; //todo: 글자수 제한 .. 중복 검증도?
@NotNull
private String description; //todo: 글자수 제한
private MultipartFile image;
@NotNull
private Semester semester;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ public class Branch extends BaseEntity {
@Column(nullable = false)
private Semester semester;

public void updateBranch(BranchRequest.BranchInfoDTO dto, String image) {
public void updateBranchInfo(BranchRequest.BranchInfoDTO dto) {
this.name = dto.getName();
this.description = dto.getDescription();
this.semester = dto.getSemester();
}

public void setImage(String image) {
this.image = image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface BranchRepository extends JpaRepository<Branch, UUID>{

boolean existsByName(String name);
Optional<Branch> findByName(String name);

boolean existsBySemester(Semester semester);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.networkingService.domain.branch.service;

import com.umc.networkingService.config.initial.DataLoader;
import com.umc.networkingService.domain.branch.dto.request.BranchRequest;
import com.umc.networkingService.domain.branch.dto.response.BranchResponse;
import com.umc.networkingService.domain.branch.entity.Branch;
Expand Down Expand Up @@ -29,17 +30,23 @@ public class BranchServiceImpl implements BranchService {
private final BranchUniversityRepository branchUniversityRepository;
private final S3FileComponent s3FileComponent;

private final DataLoader dataLoader;

private static final String BRANCH_CATEGORY = "branch";

@Transactional //지부 생성
public BranchResponse.BranchId postBranch(BranchRequest.BranchInfoDTO request) {
public BranchResponse.BranchId postBranch(BranchRequest.BranchInfoDTO request, MultipartFile image) {

validateBranchNameAndDescription(request.getName(), request.getDescription());

Branch savedBranch = branchRepository.save(
BranchMapper.toBranch(request,uploadImageS3(BRANCH_CATEGORY,request.getImage()))
BranchMapper.toBranch(request,uploadImageS3(BRANCH_CATEGORY,image))
);

if(!branchRepository.existsBySemester(request.getSemester())){
dataLoader.updateBranchUniversities(); // 새로운 기수인 경우 기존 BranchUniv 상태 false로 변경
}

if (savedBranch.getId() == null) {
throw new RestApiException(BranchErrorCode.BRANCH_SAVE_FAIL);
}
Expand All @@ -48,7 +55,7 @@ public BranchResponse.BranchId postBranch(BranchRequest.BranchInfoDTO request) {
}

@Transactional //지부 수정
public BranchResponse.BranchId patchBranch(BranchRequest.BranchInfoDTO request, UUID branchId) {
public BranchResponse.BranchId patchBranch(BranchRequest.BranchInfoDTO request, UUID branchId, MultipartFile image) {

Optional<Branch> optionalBranch = branchRepository.findById(branchId);
if(optionalBranch.isEmpty()){
Expand All @@ -58,7 +65,11 @@ public BranchResponse.BranchId patchBranch(BranchRequest.BranchInfoDTO request,
validateBranchNameAndDescription(request.getName(), request.getDescription());

Branch branch = optionalBranch.get();
branch.updateBranch(request, uploadImageS3(BRANCH_CATEGORY, request.getImage()));
if(image != null && !image.isEmpty()){
s3FileComponent.deleteFile(branch.getImage()); // 기존 이미지 삭제
branch.setImage(uploadImageS3(BRANCH_CATEGORY, image)); // 새로운 이미지 업로드
}
branch.updateBranchInfo(request); // 이름, 설명, 기수 수정
return BranchResponse.BranchId.builder()
.branchId(branch.getId()).build();

Expand Down

0 comments on commit a2cba16

Please sign in to comment.