Skip to content

Commit

Permalink
Merge pull request #12 from 7th-UMC-Hackathon-TeamV/feat/groupcode
Browse files Browse the repository at this point in the history
feat: 그룹코드 생성 API
  • Loading branch information
dokyung-kang authored Jan 11, 2025
2 parents e6bfd7f + 4883ae6 commit c4f9bad
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Group {
public class TeamGroup {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
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);
}
40 changes: 40 additions & 0 deletions src/main/java/banban/springboot/service/GroupService.java
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();
}
}
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());
}
}

0 comments on commit c4f9bad

Please sign in to comment.