-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] ์ด๋ ์ฝ๋ ๊ด๋ จ API ๊ตฌํ
- Loading branch information
Showing
27 changed files
with
568 additions
and
36 deletions.
There are no files selected for viewing
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
61 changes: 61 additions & 0 deletions
61
src/main/java/com/umc/networkingService/domain/invite/controller/InviteController.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,61 @@ | ||
package com.umc.networkingService.domain.invite.controller; | ||
|
||
import com.umc.networkingService.config.security.auth.CurrentMember; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteAuthenticateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteCreateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteInquiryMineResponse; | ||
import com.umc.networkingService.domain.invite.service.InviteService; | ||
import com.umc.networkingService.domain.member.entity.Member; | ||
import com.umc.networkingService.global.common.base.BaseResponse; | ||
import com.umc.networkingService.global.common.enums.Role; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
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.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "์ด๋ API", description = "์ด๋ ๊ด๋ จ API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class InviteController { | ||
|
||
private final InviteService inviteService; | ||
|
||
@Operation(summary = "์ด๋ ์ฝ๋ ๋ฐ๊ธ API", description = "์ผ๋ฐ ๋ถ์์ฉ ๋๋ ์ด์์ง์ฉ ์ด๋ ์ฝ๋ ๋ฐ๊ธ API์ ๋๋ค.") | ||
@ApiResponses( value = { | ||
@ApiResponse(responseCode = "COMMON200", description = "์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "INVITE002", description = "์์์ ์ญํ ์ ๋ํด์ ์ด๋ ์ฝ๋๋ฅผ ๋ฐ๊ธํ ๊ฒฝ์ฐ ๋ฐ์") | ||
}) | ||
@Parameter(name = "role", description = "์ด๋ ์ฝ๋์ ๋ถ์ฌํ ์ญํ ์ ์ง์ ํฉ๋๋ค.(ex. MEMBER, STAFF, CAMPUS_STAFF ...)") | ||
@PostMapping("/staff/invites") | ||
public BaseResponse<InviteCreateResponse> createInviteCode(@CurrentMember Member member, | ||
@RequestParam Role role) { | ||
return BaseResponse.onSuccess(inviteService.createInviteCode(member, role)); | ||
} | ||
|
||
@Operation(summary = "๋์ ์ด๋ ์ฝ๋ ์กฐํ API", description = "๋ณธ์ธ์ด ์์ฑํ ์ด๋ ์ฝ๋ ๋ชฉ๋ก ์กฐํ API์ ๋๋ค.") | ||
@ApiResponses( value = { | ||
@ApiResponse(responseCode = "COMMON200", description = "์ฑ๊ณต") | ||
}) | ||
@GetMapping("/staff/invites") | ||
public BaseResponse<List<InviteInquiryMineResponse>> inquiryMyInviteCode(@CurrentMember Member member) { | ||
return BaseResponse.onSuccess(inviteService.inquiryMyInviteCode(member)); | ||
} | ||
|
||
@Operation(summary = "์ด๋ ์ฝ๋ ํ์ธ API", description = "๋ฐ๊ธ ๋ฐ์ ์ด๋ ์ฝ๋๋ฅผ ๊ฒ์ฆํ๋ API์ ๋๋ค.") | ||
@ApiResponses( value = { | ||
@ApiResponse(responseCode = "COMMON200", description = "์ฑ๊ณต"), | ||
@ApiResponse(responseCode = "INVITE001", description = "๋ง๋ฃ๋ ์ด๋ ์ฝ๋์ผ ๊ฒฝ์ฐ ๋ฐ์") | ||
}) | ||
@Parameter(name = "inviteCode", in = ParameterIn.PATH, description = "๋ฐ๊ธ ๋ฐ์ ์ด๋ ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ ํ๋ผ๋ฏธํฐ์ ๋๋ค.") | ||
@PostMapping("/invites/{inviteCode}") | ||
public BaseResponse<InviteAuthenticateResponse> authenticationInviteCode(@CurrentMember Member member, | ||
@PathVariable String inviteCode) { | ||
return BaseResponse.onSuccess(inviteService.authenticateInviteCode(member, inviteCode)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...java/com/umc/networkingService/domain/invite/dto/response/InviteAuthenticateResponse.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,11 @@ | ||
package com.umc.networkingService.domain.invite.dto.response; | ||
|
||
import com.umc.networkingService.global.common.enums.Role; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class InviteAuthenticateResponse { | ||
private Role role; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/umc/networkingService/domain/invite/dto/response/InviteCreateResponse.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,12 @@ | ||
package com.umc.networkingService.domain.invite.dto.response; | ||
|
||
import com.umc.networkingService.global.common.enums.Role; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class InviteCreateResponse { | ||
private String inviteCode; | ||
private Role role; | ||
} |
15 changes: 15 additions & 0 deletions
15
.../java/com/umc/networkingService/domain/invite/dto/response/InviteInquiryMineResponse.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,15 @@ | ||
package com.umc.networkingService.domain.invite.dto.response; | ||
|
||
import com.umc.networkingService.global.common.enums.Role; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class InviteInquiryMineResponse { | ||
private String inviteCode; | ||
private Role role; | ||
private LocalDateTime createdAt; | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/umc/networkingService/domain/invite/mapper/InviteMapper.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,13 @@ | ||
package com.umc.networkingService.domain.invite.mapper; | ||
|
||
import com.umc.networkingService.domain.invite.dto.response.InviteInquiryMineResponse; | ||
import com.umc.networkingService.domain.invite.entity.Invite; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class InviteMapper { | ||
|
||
public InviteInquiryMineResponse toInquiryMineResponse(Invite invite) { | ||
return new InviteInquiryMineResponse(invite.getCode(), invite.getRole(), invite.getCreatedAt()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/umc/networkingService/domain/invite/repository/InviteRepository.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,19 @@ | ||
package com.umc.networkingService.domain.invite.repository; | ||
|
||
import com.umc.networkingService.domain.invite.entity.Invite; | ||
import com.umc.networkingService.domain.member.entity.Member; | ||
import com.umc.networkingService.global.common.enums.Role; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface InviteRepository extends JpaRepository<Invite, UUID> { | ||
|
||
Optional<Invite> findByMemberAndRole(Member member, Role role); | ||
Optional<Invite> findByCode(String code); | ||
|
||
List<Invite> findAllByMember(Member member); | ||
boolean existsByCode(String code); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/umc/networkingService/domain/invite/service/InviteService.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,15 @@ | ||
package com.umc.networkingService.domain.invite.service; | ||
|
||
import com.umc.networkingService.domain.invite.dto.response.InviteAuthenticateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteCreateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteInquiryMineResponse; | ||
import com.umc.networkingService.domain.member.entity.Member; | ||
import com.umc.networkingService.global.common.enums.Role; | ||
|
||
import java.util.List; | ||
|
||
public interface InviteService { | ||
InviteCreateResponse createInviteCode(Member member, Role role); | ||
InviteAuthenticateResponse authenticateInviteCode(Member member, String inviteCode); | ||
List<InviteInquiryMineResponse> inquiryMyInviteCode(Member member); | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/com/umc/networkingService/domain/invite/service/InviteServiceImpl.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,107 @@ | ||
package com.umc.networkingService.domain.invite.service; | ||
|
||
import com.umc.networkingService.domain.invite.dto.response.InviteAuthenticateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteCreateResponse; | ||
import com.umc.networkingService.domain.invite.dto.response.InviteInquiryMineResponse; | ||
import com.umc.networkingService.domain.invite.entity.Invite; | ||
import com.umc.networkingService.domain.invite.mapper.InviteMapper; | ||
import com.umc.networkingService.domain.invite.repository.InviteRepository; | ||
import com.umc.networkingService.domain.member.entity.Member; | ||
import com.umc.networkingService.domain.member.service.MemberService; | ||
import com.umc.networkingService.global.common.base.BaseEntity; | ||
import com.umc.networkingService.global.common.enums.Role; | ||
import com.umc.networkingService.global.common.exception.ErrorCode; | ||
import com.umc.networkingService.global.common.exception.RestApiException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class InviteServiceImpl implements InviteService { | ||
|
||
private final InviteMapper inviteMapper; | ||
|
||
private final InviteRepository inviteRepository; | ||
private final MemberService memberService; | ||
|
||
// ์ด๋ ์ฝ๋ ์์ฑ ํจ์ | ||
@Override | ||
public InviteCreateResponse createInviteCode(Member member, Role role) { | ||
checkRolePriority(member, role); | ||
deleteExistingInvite(member, role); | ||
String inviteCode = generateUniqueInviteCode(); | ||
Invite savedInvite = saveInvite(member, role, inviteCode); | ||
return new InviteCreateResponse(savedInvite.getCode(), savedInvite.getRole()); | ||
} | ||
|
||
// ์ด๋ ์ฝ๋ ์ธ์ฆ ํจ์ | ||
@Override | ||
@Transactional | ||
public InviteAuthenticateResponse authenticateInviteCode(Member member, String inviteCode) { | ||
// ์กด์ฌํ์ง ์๋ ์ด๋ ์ฝ๋์ธ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ | ||
Invite savedInvite = inviteRepository.findByCode(inviteCode) | ||
.orElseThrow(() -> new RestApiException(ErrorCode.EXPIRED_INVITE_CODE)); | ||
|
||
// ๋ง๋ฃ๋ ์ด๋ ์ฝ๋์ธ ๊ฒฝ์ฐ ์ญ์ ํ ์์ธ ์ฒ๋ฆฌ | ||
if (savedInvite.isExpired()) { | ||
savedInvite.delete(); | ||
throw new RestApiException(ErrorCode.EXPIRED_INVITE_CODE); | ||
} | ||
|
||
// ์ด๋ ์ฝ๋์ ๋ถ์ฌ๋ ์ญํ ๋ถ์ฌ | ||
member.updateRole(savedInvite.getRole()); | ||
Member savedMember = memberService.saveEntity(member); | ||
|
||
return new InviteAuthenticateResponse(savedMember.getRole()); | ||
} | ||
|
||
// ๋์ ์ด๋ ์ฝ๋ ์กฐํ ํจ์ | ||
@Override | ||
public List<InviteInquiryMineResponse> inquiryMyInviteCode(Member member) { | ||
// ๋ณธ์ธ์ด ์์ฑํ ์ด๋ ์ฝ๋ ์กฐํ | ||
List<Invite> savedInvites = inviteRepository.findAllByMember(member); | ||
|
||
return savedInvites.stream() | ||
.map(inviteMapper::toInquiryMineResponse) | ||
.toList(); | ||
} | ||
|
||
// ๋ณธ์ธ์ ์ญํ ์ด์์ ์ญํ ๋ถ์ฌ๋ฅผ ํ์ธํ๋ ํจ์ | ||
private void checkRolePriority(Member member, Role role) { | ||
if (member.getRole().getPriority() >= role.getPriority()) { | ||
throw new RestApiException(ErrorCode.UNAUTHORIZED_CREATE_INVITE); | ||
} | ||
} | ||
|
||
// ์กด์ฌํ๋ ์ด๋ ์ฝ๋ ์ญ์ ํจ์ | ||
private void deleteExistingInvite(Member member, Role role) { | ||
Optional<Invite> oldInvite = inviteRepository.findByMemberAndRole(member, role); | ||
oldInvite.ifPresent(BaseEntity::delete); | ||
} | ||
|
||
// ์ค๋ณต๋์ง ์๋ ์ด๋ ์ฝ๋ ์์ฑ ํจ์ | ||
private String generateUniqueInviteCode() { | ||
String inviteCode; | ||
do { | ||
inviteCode = UUID.randomUUID().toString().replace("-", "").substring(0, 10); | ||
} while (inviteRepository.existsByCode(inviteCode)); | ||
return inviteCode; | ||
} | ||
|
||
// ์ด๋ ์ฝ๋ ์ ์ฅ ํจ์ | ||
private Invite saveInvite(Member member, Role role, String inviteCode) { | ||
return inviteRepository.save( | ||
Invite.builder() | ||
.member(member) | ||
.code(inviteCode) | ||
.role(role) | ||
.build() | ||
); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.