Skip to content

Commit

Permalink
🔀 :: (#117) 그룹 컨트롤러 스웨거, 밸리데이션 보강
Browse files Browse the repository at this point in the history
🔀 :: (#117) 그룹 컨트롤러 스웨거, 밸리데이션 보강
  • Loading branch information
ImNM authored Dec 17, 2022
2 parents 323e91b + c530bd7 commit f36a981
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.github.depromeet.knockknockbackend.domain.group.presentation.dto.response.GroupResponse;
import io.github.depromeet.knockknockbackend.domain.group.service.CategoryService;
import io.github.depromeet.knockknockbackend.domain.group.service.GroupService;
import io.github.depromeet.knockknockbackend.global.annotation.DisableSecurity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand All @@ -38,11 +39,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/groups")
@RequiredArgsConstructor
@Tag(name = "그룹 관련 컨트롤러", description = "")
@Tag(name = "그룹 관련 컨트롤러",description = "")
@SecurityRequirement(name = "access-token")
public class GroupController {

Expand All @@ -54,32 +54,37 @@ public class GroupController {
private final AdmissionUsecase admissionUsecase;

@Operation(summary = "공개 그룹을 만듭니다")
@Tag(name = "그룹 생성")
@PostMapping("/open")
public GroupResponse createOpenGroup(@Valid @RequestBody CreateOpenGroupRequest createOpenGroupRequest){
return groupService.createOpenGroup(createOpenGroupRequest);
}

@Operation(summary = "친구 그룹을 만듭니다")
@Tag(name = "그룹 생성")
@PostMapping("/friend")
public GroupResponse createFriendGroup(@Valid @RequestBody CreateFriendGroupRequest createFriendGroupRequest){
return groupService.createFriendGroup(createFriendGroupRequest);
}

@Operation(summary = "방장 권한 그룹 설정")
@Tag(name = "방장 권한 그룹 관리")
@PutMapping("/{id}")
public GroupResponse putGroup(@PathVariable("id") Long groupId ,@Valid @RequestBody UpdateGroupRequest updateGroupRequest){

return groupService.updateGroup(groupId,updateGroupRequest);
}

@Operation(summary = "방장 권한 그룹 제거")
@Tag(name = "방장 권한 그룹 관리")
@DeleteMapping("/{id}")
public void deleteGroup(@PathVariable("id") Long groupId){
groupService.deleteGroup(groupId);
}


@GetMapping("/{id}")
@Tag(name = "그룹 조회")
public GroupResponse getGroupDetail(@PathVariable("id") Long groupId){
return groupService.getGroupDetailById(groupId);
}
Expand All @@ -88,6 +93,7 @@ public GroupResponse getGroupDetail(@PathVariable("id") Long groupId){
@Parameter(name = "type", description = "type", schema = @Schema(implementation = GroupInTypeRequest.class)
, in = ParameterIn.QUERY)
@Operation(summary = "참여중인 그룹 목록 전체 홀로외침 친구들 방 필터링")
@Tag(name = "그룹 조회")
@GetMapping("/joined")
public Slice<GroupBriefInfoDto> getParticipatingGroups(
@RequestParam("type") GroupInTypeRequest groupInTypeRequest,
Expand All @@ -104,6 +110,7 @@ public Slice<GroupBriefInfoDto> getParticipatingGroups(
@Parameter(name = "category", description = "category", schema = @Schema(implementation = Long.class)
, in = ParameterIn.QUERY)
@Operation(summary = "방 찾기")
@Tag(name = "그룹 조회")
@GetMapping("/open")
public Slice<GroupBriefInfoDto> getAllOpenGroups(
@RequestParam(value = "category" ) Long categoryId,
Expand All @@ -112,46 +119,51 @@ public Slice<GroupBriefInfoDto> getAllOpenGroups(
PageRequest pageRequest = PageRequest.of(page, size);
return groupService.findOpenGroupByCategory(categoryId ,pageRequest);
}

@Tag(name = "그룹 카테고리")
@GetMapping("/categories")
public CategoryListResponse getCategory(){
return categoryService.findAllCategory();
}

@Tag(name = "그룹 카테고리")
@GetMapping("/categories/famous")
public CategoryListResponse getFamousCategory(){
return categoryService.findFamousCategory();
}

//TODO : 관리자권한 필요
@PostMapping("/categories")
public CategoryDto createCategory(@RequestBody @Valid CreateCategoryRequest createCategoryRequest){
return categoryService.saveCategory(createCategoryRequest);
}
// @PostMapping("/categories")
// public CategoryDto createCategory(@RequestBody @Valid CreateCategoryRequest createCategoryRequest){
// return categoryService.saveCategory(createCategoryRequest);
// }



@Operation(summary = "그룹에 가입요청을 합니다.")
@Tag(name = "그룹 가입요청")
@PostMapping("/{id}/admissions")
public AdmissionInfoDto admissionToGroup(@PathVariable(value = "id") Long groupId){
return admissionUsecase.requestAdmission(groupId);
}

@Operation(summary = "그룹 가입 요청을 살펴봅니다.")
@Operation(summary = "그룹 가입 요청을 살펴봅니다. (방장 권한)")
@Tag(name = "그룹 가입요청")
@GetMapping("/{id}/admissions")
public AdmissionInfoListResponse getAdmissionRequest(@PathVariable(value = "id") Long groupId){
return admissionUsecase.getAdmissions(groupId);
}

@Operation(summary = "그룹 가입요청을 허락합니다.")
@Operation(summary = "그룹 가입요청을 허락합니다. (방장 권한)")
@Tag(name = "그룹 가입요청")
@PostMapping("/{id}/admissions/{admission_id}/allow")
public AdmissionInfoDto acceptAdmissionRequest(
@PathVariable(value = "id") Long groupId,
@PathVariable(value = "admission_id") Long admissionId){
return admissionUsecase.acceptAdmission(groupId,admissionId);
}

@Operation(summary = "그룹 가입요청을 거절합니다.")
@Operation(summary = "그룹 가입요청을 거절합니다. (방장 권한)")
@Tag(name = "그룹 가입요청")
@PostMapping("/{id}/admissions/{admission_id}/refuse")
public AdmissionInfoDto refuseAdmissionRequest(
@PathVariable(value = "id") Long groupId,
Expand All @@ -160,6 +172,7 @@ public AdmissionInfoDto refuseAdmissionRequest(
return admissionUsecase.refuseAdmission(groupId, admissionId);
}
@Operation(summary = "방 검색하기")
@Tag(name = "그룹 조회")
@GetMapping("/search/{searchString}")
public Slice<GroupBriefInfoDto> searchOpenGroups(
@PathVariable(value = "searchString") String searchString,
Expand All @@ -170,7 +183,8 @@ public Slice<GroupBriefInfoDto> searchOpenGroups(
return groupService.searchOpenGroups(searchString,pageRequest);
}

@Operation(summary = "방장 권한 멤버 추가")
@Operation(summary = "멤버 추가 내 친구 목록 초대가능!")
@Tag(name = "그룹 멤버")
@PostMapping("/{id}/members")
public GroupResponse addMembers(
@PathVariable(value = "id") Long groupId,
Expand All @@ -179,18 +193,21 @@ public GroupResponse addMembers(
}

@Operation(summary = "그룹에서 나가기 ( 방장은 못나갑니다 )")
@Tag(name = "그룹 멤버")
@DeleteMapping("/{id}/members/leave")
public GroupResponse leaveFromGroup(@PathVariable(value = "id") Long groupId){
return groupService.leaveFromGroup(groupId);
}

@Operation(summary = "멤버 내쫓기 (방장권한)")
@Tag(name = "방장 권한 그룹 관리")
@DeleteMapping("/{id}/members/{user_id}")
public GroupResponse deleteMemberFromGroup(@PathVariable(value = "id") Long groupId, @PathVariable(value = "user_id") Long userId){
return groupService.deleteMemberFromGroup(groupId , userId);
}

@Operation(summary = "그룹 초대 토큰 발급")
@Tag(name = "그룹 멤버")
@GetMapping("/{id}/members/invite")
public GroupInviteLinkResponse createGroupInviteLink(
@PathVariable(value = "id") Long groupId
Expand All @@ -199,6 +216,7 @@ public GroupInviteLinkResponse createGroupInviteLink(
}

@Operation(summary = "그룹 초대 토큰 검증 & 그룹 가입")
@Tag(name = "그룹 멤버")
@PostMapping("/{id}/members/invite/{code}")
public GroupResponse checkGroupInviteLink(
@PathVariable(value = "id") Long groupId,
Expand All @@ -208,6 +226,7 @@ public GroupResponse checkGroupInviteLink(
}

@Operation(summary = "요즘 뜨고있는 알림방")
@Tag(name = "그룹 조회")
@GetMapping("/famous")
public GroupBriefInfos getFamousGroup(){
return groupService.getFamousGroup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,6 +11,7 @@
@Getter
@NoArgsConstructor
public class AddFriendToGroupRequest {
private List<Long> memberIds = new ArrayList<>();
@NotNull
private List<Long> memberIds;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -16,9 +18,11 @@ public class CreateOpenGroupRequest {


@NotNull
@Size(min=1,max=18)
private String title;

@NotNull
@Size(min=1,max=80)
private String description;
@NotNull
private Boolean publicAccess;
Expand All @@ -29,7 +33,8 @@ public class CreateOpenGroupRequest {
private String backgroundImagePath;

@NotNull
@Schema(defaultValue = "1" , description = "건너뛰기일 경우 1로 설정")
@Positive
@Schema(defaultValue = "1" , description = "카테고리 디폴트 값은 1 입니다. 비어있는 카테고리 값")
private Long categoryId;

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.github.depromeet.knockknockbackend.domain.group.service.dto.UpdateGroupDto;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -11,9 +13,11 @@
@NoArgsConstructor
public class UpdateGroupRequest {
@NotNull
@Size(min=1,max=18)
private String title;

@NotNull
@Size(min=1,max=80)
private String description;
@NotNull
private Boolean publicAccess;
Expand All @@ -24,6 +28,8 @@ public class UpdateGroupRequest {
private String backgroundImagePath;

@Schema(defaultValue = "1")
@NotNull
@Positive
private Long categoryId;

public UpdateGroupDto toUpdateGroupDto(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -145,14 +146,14 @@ private Group makeOpenGroup(CreateOpenGroupRequest createOpenGroupRequest, User
GroupBuilder groupBuilder = Group.builder()
.publicAccess(createOpenGroupRequest.getPublicAccess())
.thumbnailPath(
createOpenGroupRequest.getThumbnailPath() == null ?
assetUtils.getRandomThumbnailUrl() :
createOpenGroupRequest.getThumbnailPath()
StringUtils.hasText(createOpenGroupRequest.getThumbnailPath()) ?
createOpenGroupRequest.getThumbnailPath() :
assetUtils.getRandomThumbnailUrl()
)
.backgroundImagePath(
createOpenGroupRequest.getBackgroundImagePath() == null ?
assetUtils.getRandomBackgroundImageUrl() :
createOpenGroupRequest.getBackgroundImagePath()
StringUtils.hasText(createOpenGroupRequest.getBackgroundImagePath()) ?
createOpenGroupRequest.getBackgroundImagePath() :
assetUtils.getRandomBackgroundImageUrl()
)
.description(createOpenGroupRequest.getDescription())
.title(createOpenGroupRequest.getTitle())
Expand All @@ -163,8 +164,7 @@ private Group makeOpenGroup(CreateOpenGroupRequest createOpenGroupRequest, User
createOpenGroupRequest.getCategoryId());
groupBuilder.category(category);

Group group = groupBuilder.build();
return group;
return groupBuilder.build();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.depromeet.knockknockbackend.global.annotation.DisableSecurity;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
Expand All @@ -12,6 +13,7 @@
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityScheme.In;
import io.swagger.v3.oas.models.security.SecurityScheme.Type;
import java.util.stream.Collectors;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -66,17 +68,32 @@ public OperationCustomizer customize() {
return (Operation operation, HandlerMethod handlerMethod) -> {
DisableSecurity methodAnnotation = handlerMethod.getMethodAnnotation(
DisableSecurity.class);
List<String> tags = getTags(handlerMethod);
// DisableSecurity 어노테이션있을시 스웨거 시큐리티 설정 삭제
if(methodAnnotation != null){
operation.setSecurity(Collections.emptyList());
}
//태그 중복 설정시 제일 구체적인 값만 태그로 설정
List<String> tags = operation.getTags();
if(tags != null && !tags.isEmpty() ){
if(!tags.isEmpty()){
operation.setTags(Collections.singletonList(tags.get(0)));
}
return operation;
};
}

private static List<String> getTags(HandlerMethod handlerMethod) {
List<String> tags = new ArrayList<>();

Tag[] methodTags = handlerMethod.getMethod().getAnnotationsByType(Tag.class);
List<String> methodTagStrings = Arrays.stream(methodTags).map(Tag::name)
.collect(Collectors.toList());

Tag[] classTags = handlerMethod.getClass().getAnnotationsByType(Tag.class);
List<String> classTagStrings = Arrays.stream(classTags).map(Tag::name)
.collect(Collectors.toList());
tags.addAll(methodTagStrings);
tags.addAll(classTagStrings);
return tags;
}

}

0 comments on commit f36a981

Please sign in to comment.