-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from 7th-UMC-Hackathon-TeamV/feat/groupcode
feat: 그룹코드 생성 API
- Loading branch information
Showing
4 changed files
with
82 additions
and
1 deletion.
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
8 changes: 8 additions & 0 deletions
8
src/main/java/banban/springboot/repository/GroupRepository.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 banban.springboot.repository; | ||
|
||
import banban.springboot.domain.entity.TeamGroup; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface GroupRepository extends JpaRepository<TeamGroup, Long> { | ||
boolean existsByGroupKey(String groupKey); | ||
} |
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 banban.springboot.service; | ||
|
||
import banban.springboot.domain.entity.TeamGroup; | ||
import banban.springboot.repository.GroupRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.security.SecureRandom; | ||
|
||
@Service | ||
public class GroupService { | ||
@Autowired | ||
private GroupRepository groupRepository; | ||
|
||
// 그룹코드 생성 | ||
public TeamGroup createGroupCode() { | ||
String groupCode = generateUniqueGroupCode(); | ||
TeamGroup teamGroup = new TeamGroup(); | ||
teamGroup.setGroupKey(groupCode); | ||
|
||
return groupRepository.save(teamGroup); | ||
} | ||
|
||
// 중복되지 않는 6글자 그룹코드 생성 | ||
private String generateUniqueGroupCode() { | ||
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
SecureRandom random = new SecureRandom(); | ||
StringBuilder code; | ||
|
||
do { | ||
code = new StringBuilder(); | ||
for (int i = 0; i < 6; i++) { | ||
int index = random.nextInt(characters.length()); | ||
code.append(characters.charAt(index)); | ||
} | ||
} while (groupRepository.existsByGroupKey(code.toString())); | ||
|
||
return code.toString(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/banban/springboot/web/controller/GroupController.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,33 @@ | ||
package banban.springboot.web.controller; | ||
|
||
import banban.springboot.apiPayload.ApiResponse; | ||
import banban.springboot.domain.entity.TeamGroup; | ||
import banban.springboot.service.GroupService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Validated | ||
@RequestMapping("/api") | ||
public class GroupController { | ||
|
||
private final GroupService groupService; | ||
|
||
@PostMapping("/random-group") | ||
@Operation(summary = "그룹 코드 생성 API",description = "그룹 코드 생성하는 API입니다.") | ||
@ApiResponses({ | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"), | ||
}) | ||
public ApiResponse<String> updatePromotionProject(){ | ||
|
||
TeamGroup newGroup = groupService.createGroupCode(); | ||
|
||
return ApiResponse.onSuccess(newGroup.getGroupKey()); | ||
} | ||
} |