-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT #30] 기숙사 인증요청, 기숙사 인증요청 처리 로직 구현
- Loading branch information
1 parent
8305fd6
commit 6cab792
Showing
8 changed files
with
283 additions
and
6 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/duktown/domain/dormCert/controller/DormCertController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.duktown.domain.dormCert.controller; | ||
|
||
import com.duktown.domain.dormCert.dto.DormCertDto; | ||
import com.duktown.domain.dormCert.service.DormCertService; | ||
import com.duktown.global.security.service.CustomUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/dormCerts") | ||
public class DormCertController { | ||
private final DormCertService dormCertService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> createDormCert( | ||
@AuthenticationPrincipal CustomUserDetails customUserDetails, | ||
@Valid @RequestPart(value = "certRequest") DormCertDto.UserCertRequest request, | ||
@RequestPart (value = "certImg") MultipartFile certImg | ||
) { | ||
dormCertService.createDormCert(customUserDetails.getId(), request, certImg); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("/check/{dormCertId}") | ||
public ResponseEntity<DormCertDto.CertResponse> checkDormCert( | ||
@AuthenticationPrincipal CustomUserDetails customUserDetails, | ||
@PathVariable("dormCertId") Long dormCertId, | ||
@RequestParam("approve") Boolean approve | ||
) { | ||
return ResponseEntity.ok( | ||
dormCertService.checkDormCert(customUserDetails.getId(), dormCertId, approve) | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/duktown/domain/dormCert/dto/DormCertDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.duktown.domain.dormCert.dto; | ||
|
||
import com.duktown.domain.dormCert.entity.DormCert; | ||
import com.duktown.domain.user.entity.User; | ||
import com.duktown.global.type.CertRequestType; | ||
import com.duktown.global.type.HallName; | ||
import com.duktown.global.type.RoleType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
|
||
public class DormCertDto { | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class UserCertRequest{ | ||
// image는 requestParam MultipartFile로 입력받음 | ||
|
||
@NotNull(message = "인증요청 타입은 필수 값입니다.") | ||
@Min(value = 0, message = "인증요청 타입 값은 0부터 2 사이의 값이어야 합니다.") | ||
@Max(value = 2, message = "인증요청 타입 값은 0부터 2 사이의 값이어야 합니다.") | ||
private Integer certRequestType; | ||
|
||
@NotBlank(message = "학번은 필수 값입니다.") | ||
private String studentId; | ||
|
||
@NotNull(message = "기숙사 관명은 필수 값입니다.") | ||
private Integer hallName; | ||
|
||
@NotNull(message = "기숙사 층은 필수 값입니다.") | ||
private Integer floorNumber; | ||
|
||
@NotNull(message = "기숙사 동은 필수 값입니다.") | ||
private Integer buildingNumber; | ||
|
||
@NotNull(message = "기숙사 호수는 필수 값입니다.") | ||
private Integer roomNumber; | ||
|
||
public DormCert toEntity(User user, CertRequestType certRequestType, HallName hallName, String imgUrl) { | ||
// 최초 등록 시 certified는 null | ||
return DormCert.builder() | ||
.user(user) | ||
.certRequestType(certRequestType) | ||
.imgUrl(imgUrl) | ||
.studentId(this.studentId) | ||
.hallName(hallName) | ||
.floorNumber(this.floorNumber) | ||
.buildingNumber(this.buildingNumber) | ||
.roomNumber(this.roomNumber) | ||
.build(); | ||
} | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public static class CertResponse { | ||
private Boolean certified; | ||
private RoleType resultRoleType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/duktown/domain/dormCert/entity/DormCertRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.duktown.domain.dormCert.entity; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface DormCertRepository extends JpaRepository<DormCert, Long> { | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/com/duktown/domain/dormCert/service/DormCertService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.duktown.domain.dormCert.service; | ||
|
||
import com.duktown.domain.dormCert.dto.DormCertDto; | ||
import com.duktown.domain.dormCert.entity.DormCert; | ||
import com.duktown.domain.dormCert.entity.DormCertRepository; | ||
import com.duktown.domain.user.entity.User; | ||
import com.duktown.domain.user.entity.UserRepository; | ||
import com.duktown.global.exception.CustomException; | ||
import com.duktown.global.type.CertRequestType; | ||
import com.duktown.global.type.HallName; | ||
import com.duktown.global.type.RoleType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
|
||
import static com.duktown.global.exception.CustomErrorType.*; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class DormCertService { | ||
private final UserRepository userRepository; | ||
private final DormCertRepository dormCertRepository; | ||
|
||
public void createDormCert(Long userId, DormCertDto.UserCertRequest request, MultipartFile certImg){ | ||
User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
|
||
// TODO: AWS S3 Service 사용할 것 | ||
// 파일 업로드(로컬 폴더) | ||
String absolutePath = Paths.get("").toAbsolutePath() + "/certImages"; | ||
File localSaveFolder = new File(absolutePath); | ||
|
||
if (!localSaveFolder.exists()) { | ||
try { | ||
localSaveFolder.mkdir(); | ||
} catch (Exception e) { | ||
throw new CustomException(CERT_IMG_FOLDER_CREATION_ERROR); | ||
} | ||
} | ||
|
||
String filename = certImg.getOriginalFilename(); | ||
File destination = new File(absolutePath + "/" + filename); | ||
|
||
try{ | ||
certImg.transferTo(destination); | ||
} catch (IOException e){ | ||
throw new CustomException(CERT_IMG_UPLOAD_ERROR); | ||
} | ||
|
||
// 인증 요청 타입 찾기 | ||
CertRequestType certRequestType = Arrays.stream(CertRequestType.values()) | ||
.filter(type -> type.getValue() == request.getCertRequestType()) | ||
.findAny().orElseThrow(() -> new CustomException(INVALID_CERT_REQUEST_TYPE_VALUE)); | ||
|
||
// 기숙사 관명 찾기 | ||
HallName hallName = Arrays.stream(HallName.values()) | ||
.filter(hn -> hn.getValue() == request.getHallName()) | ||
.findAny().orElseThrow(() -> new CustomException(INVALID_HALL_NAME_VALUE)); | ||
|
||
DormCert dormCert = request.toEntity(user, certRequestType, hallName, destination.getPath()); | ||
dormCertRepository.save(dormCert); | ||
} | ||
|
||
public DormCertDto.CertResponse checkDormCert(Long userId, Long dormCertId, Boolean approve){ | ||
User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
|
||
// 자기 자신 인증요청 처리 불가 | ||
if (!user.getId().equals(userId)) { | ||
throw new CustomException(SELF_DORM_CERT_NOT_ALLOWED); | ||
} | ||
|
||
DormCert findDormCert = dormCertRepository.findById(dormCertId).orElseThrow(() -> new CustomException(DORM_CERT_NOT_FOUND)); | ||
|
||
// 이미 처리된 인증요청인 경우 | ||
if (findDormCert.getCertified() != null) { | ||
throw new CustomException(CERT_ALREADY_CHECKED); | ||
} | ||
|
||
// 인증요청 타입으로 사용자 권한 찾기 | ||
RoleType roleType = Arrays.stream(RoleType.values()) | ||
.filter(role -> role.getKey().equals(findDormCert.getCertRequestType().getRoleKey())) | ||
.findAny().orElseThrow(() -> new CustomException(INVALID_CERT_REQUEST_TYPE_VALUE)); | ||
|
||
RoleType resultRoleType; | ||
|
||
// 승인 시 | ||
if(approve){ | ||
user.updateRoleType(roleType); | ||
userRepository.save(user); | ||
|
||
findDormCert.update(true); | ||
dormCertRepository.save(findDormCert); | ||
|
||
resultRoleType = user.getRoleType(); | ||
} | ||
// 미승인 시 | ||
else { | ||
findDormCert.update(false); | ||
dormCertRepository.save(findDormCert); | ||
|
||
resultRoleType = user.getRoleType(); | ||
} | ||
|
||
return new DormCertDto.CertResponse(approve, resultRoleType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/duktown/global/type/CertRequestType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.duktown.global.type; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum CertRequestType { | ||
DORM_STUDENT(0, "ROLE_DORM_STUDENT", "기숙사생 인증"), | ||
DORM_COUNCIL(1, "ROLE_DORM_COUNCIL", "사생회 인증"), | ||
MANAGER(2, "ROLE_MANGER", "관리자 인증"); | ||
|
||
private final int value; | ||
private final String roleKey; | ||
private final String description; | ||
} |