Skip to content

Commit

Permalink
Merge pull request #49 from Team-UMC/feature/#19/branch
Browse files Browse the repository at this point in the history
[FEAT] 지부 api 구현
  • Loading branch information
junseokkim authored Feb 8, 2024
2 parents 07038b7 + aa8609a commit bf5905c
Show file tree
Hide file tree
Showing 22 changed files with 1,024 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public static BranchInfo getBranchInfo(String name) {
return Arrays.stream(BranchInfo.values())
.filter(branchInfo -> branchInfo.getName().equals(name))
.findFirst()
.orElseThrow(() -> new RestApiException(BranchErrorCode.EMPTY_BRANCH));
.orElseThrow(() -> new RestApiException(BranchErrorCode.BRANCH_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ public boolean isEnabled() {
return member.getDeletedAt() == null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.umc.networkingService.domain.branch.controller;

import com.umc.networkingService.config.security.auth.CurrentMember;
import com.umc.networkingService.domain.branch.dto.request.BranchRequest;
import com.umc.networkingService.domain.branch.dto.response.BranchResponse;
import com.umc.networkingService.domain.branch.service.BranchServiceImpl;
import com.umc.networkingService.domain.branch.service.BranchUniversityServiceImpl;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.branch.validation.annotation.ExistBranch;
import com.umc.networkingService.global.common.base.BaseResponse;
import com.umc.networkingService.global.common.enums.Semester;
import io.swagger.v3.oas.annotations.Operation;
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 lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "지부 API", description = "지부 관련 API")

@RestController
@Validated
@RequestMapping("/branch")
@RequiredArgsConstructor
public class BranchController {

private final BranchServiceImpl branchService;

@Operation(summary = "지부 리스트 정보 조회 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "SEMESTER001", description = "잘못된 기수 값이 입력된 경우")
})
@GetMapping("")
public BaseResponse<BranchResponse.JoinBranchs> joinBranchList(
@RequestParam("semester") Semester semester //기수별로 조회
){
return BaseResponse.onSuccess(branchService.joinBranchList(semester));
}

@Operation(summary = "지부 세부 정보 조회 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "BRANCH001", description = "지부를 찾을 수 없는 경우")
})
@GetMapping("/detail")
public BaseResponse<BranchResponse.JoinBranchDetails> joinBranchDetail(
@RequestParam("branchId") @Validated @ExistBranch UUID branchId
){
return BaseResponse.onSuccess(branchService.joinBranchDetail(branchId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.umc.networkingService.domain.branch.controller;


import com.umc.networkingService.config.security.auth.CurrentMember;
import com.umc.networkingService.domain.branch.dto.request.BranchRequest;
import com.umc.networkingService.domain.branch.dto.response.BranchResponse;
import com.umc.networkingService.domain.branch.service.BranchServiceImpl;
import com.umc.networkingService.domain.branch.service.BranchUniversityServiceImpl;
import com.umc.networkingService.domain.branch.validation.annotation.ExistBranch;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.global.common.base.BaseResponse;
import com.umc.networkingService.global.common.enums.Semester;
import io.swagger.v3.oas.annotations.Operation;
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 lombok.RequiredArgsConstructor;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Tag(name = "운영진용 지부 API", description = "운영진용 지부 관련 API")

@RestController
@Validated
@RequestMapping("/staff/branch")
@RequiredArgsConstructor
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("")
public BaseResponse<BranchResponse.BranchId> postBranch(
@CurrentMember Member member,
@RequestBody BranchRequest.BranchInfoDTO request
){
return BaseResponse.onSuccess(branchService.postBranch(request));
}

@Operation(summary = "지부 수정 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "BRANCH001", description = "수정할 지부를 찾을 수 없는 경우"),
@ApiResponse(responseCode = "BRANCH002", description = "지부 이름이 비어있는 경우"),
@ApiResponse(responseCode = "BRANCH003", description = "지부 설명이 비어있는 경우")
})
@PatchMapping("/{branchId}")
public BaseResponse<BranchResponse.BranchId> patchBranch(
@CurrentMember Member member,
@PathVariable("branchId") @ExistBranch UUID branchId,
@RequestBody BranchRequest.BranchInfoDTO request
){
return BaseResponse.onSuccess(branchService.patchBranch(request, branchId));
}

@Operation(summary = "지부 삭제 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공")
})
@DeleteMapping("/{branchId}")
public BaseResponse<BranchResponse.BranchId> patchBranch(
@CurrentMember Member member,
@Validated @ExistBranch @PathVariable("branchId") UUID branchId
){
return BaseResponse.onSuccess(branchService.deleteBranch(branchId));
}

@Operation(summary = "지부 대학교 연결 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "UNIVERSITY001", description = "대학교를 찾을 수 없는 경우"),
@ApiResponse(responseCode = "BRANCH001", description = "지부를 찾을 수 없는 경우"),
@ApiResponse(responseCode = "BRANCH_UNIVERSITY002", description = "이미 연결된 지부와 대학교가 있는 경우")
})
@PostMapping("/connection")
public BaseResponse<BranchResponse.ConnectBranch> connectBranch(
@CurrentMember Member member,
@RequestParam("branchId") @ExistBranch UUID branchId,
@RequestBody BranchRequest.ConnectBranchDTO request
){
return BaseResponse.onSuccess(branchUniversityService.connectBranchUniversity(branchId,request.getUniversityIds()));
}

@Operation(summary = "지부 대학교 연결 해제 API")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "UNIVERSITY001", description = "대학교를 찾을 수 없는 경우"),
@ApiResponse(responseCode = "BRANCH001", description = "지부를 찾을 수 없는 경우"),
@ApiResponse(responseCode = "BRANCH_UNIVERSITY001", description = "연결되지 않은 지부와 대학교가 있는 경우")
})
@DeleteMapping("/connection")
public BaseResponse<BranchResponse.ConnectBranch> disconnectBranch(
@CurrentMember Member member,
@RequestParam("branchId") @ExistBranch UUID branchId,
@RequestBody BranchRequest.ConnectBranchDTO request
){
return BaseResponse.onSuccess(branchUniversityService.disconnectBranchUniversity(branchId, request.getUniversityIds()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.umc.networkingService.domain.branch.dto.request;

import com.umc.networkingService.domain.branch.validation.annotation.ExistBranch;
import com.umc.networkingService.global.common.enums.Semester;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

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

public class BranchRequest {


@Getter
@AllArgsConstructor
@Builder
public static class BranchInfoDTO{
@NotNull
private String name; //todo: 글자수 제한 .. 중복 검증도?
@NotNull
private String description; //todo: 글자수 제한
private MultipartFile image;
@NotNull
private Semester semester;

}

@Getter
public static class ConnectBranchDTO{

@NotNull
private List<UUID> universityIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.umc.networkingService.domain.branch.dto.response;

import com.umc.networkingService.domain.branch.entity.Branch;
import com.umc.networkingService.global.common.enums.Semester;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

public class BranchResponse {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinBranchs{
List<JoinBranch> branchs;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinBranch{
UUID branchId;
String name;
String description;
String image;
Semester semester;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinBranchDetails{
//todo: 뷰애 맞춰 고치기
List<JoinBranchDetail> universities;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinBranchDetail{
UUID universityId;
String name;
}

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class BranchId{
UUID branchId;
}

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class ConnectBranch{
UUID branchId;
List<UUID> universityIds;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.networkingService.domain.branch.entity;

import com.umc.networkingService.domain.branch.dto.request.BranchRequest;
import com.umc.networkingService.global.common.base.BaseEntity;
import com.umc.networkingService.global.common.enums.Semester;
import jakarta.persistence.*;
Expand Down Expand Up @@ -35,4 +36,10 @@ public class Branch extends BaseEntity {
@Column(nullable = false)
private Semester semester;

public void updateBranch(BranchRequest.BranchInfoDTO dto, String image) {
this.name = dto.getName();
this.description = dto.getDescription();
this.semester = dto.getSemester();
this.image = image;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.umc.networkingService.domain.branch.mapper;

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;
import com.umc.networkingService.domain.university.entity.University;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class BranchMapper {

public static Branch toBranch(
BranchRequest.BranchInfoDTO request
,String imagePath)
{
return Branch.builder()
.name(request.getName())
.description(request.getDescription())
.image(imagePath)
.semester(request.getSemester())
.build();
}

public static BranchResponse.JoinBranchs toJoinBranchListDTO(
List<Branch> branchList
){
return BranchResponse.JoinBranchs.builder()
.branchs(branchList.stream()
.map(branch -> BranchResponse.JoinBranch.builder()
.branchId(branch.getId())
.name(branch.getName())
.description(branch.getDescription())
.image(branch.getImage())
.semester(branch.getSemester())
.build())
.collect(Collectors.toList()))
.build();
}

public static BranchResponse.JoinBranchDetails toJoinBranchDetailDTO(
List<University> universityList
){
return BranchResponse.JoinBranchDetails.builder()
.universities(universityList.stream()
.map(university -> BranchResponse.JoinBranchDetail.builder()
.universityId(university.getId())
.name(university.getName())
.build())
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.umc.networkingService.domain.branch.repository;

import com.umc.networkingService.domain.branch.entity.Branch;
import com.umc.networkingService.global.common.enums.Semester;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

public interface BranchRepository extends JpaRepository<Branch, UUID> {
@Repository
public interface BranchRepository extends JpaRepository<Branch, UUID>{
//기수 별로 지부 조회
List<Branch> findAllBySemester(Semester semester);

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

0 comments on commit bf5905c

Please sign in to comment.