Skip to content

Commit

Permalink
Merge pull request #158 from hellokitty-coding-club/feature/#152-miss…
Browse files Browse the repository at this point in the history
…ion-suggestion

미션 제안하기
  • Loading branch information
ray-yhc authored Jan 9, 2024
2 parents 9a86893 + 3612c10 commit 0d84266
Show file tree
Hide file tree
Showing 14 changed files with 865 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public enum ResponseCode {
INVALID_PROCESS_STATUS(10307, HttpStatus.BAD_REQUEST, "현재 진행상태에서 수행할 수 없는 작업입니다."),


// 104xx : 검색
// 104xx : 미션 제안하기
NOT_EXIST_SUGGESTION(10400, HttpStatus.BAD_REQUEST, "존재하지 않는 게시글입니다."),
NOT_MY_SUGGESTION(10401, HttpStatus.BAD_REQUEST, "수정 권한이 없는 게시글입니다."),

// 105xx :

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import swm.hkcc.LGTM.app.global.dto.ApiDataResponse;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.member.domain.custom.CustomUserDetails;
import swm.hkcc.LGTM.app.modules.suggestion.dto.CreateSuggestionRequest;
import swm.hkcc.LGTM.app.modules.suggestion.dto.CreateSuggestionResponse;
import swm.hkcc.LGTM.app.modules.suggestion.dto.*;
import swm.hkcc.LGTM.app.modules.suggestion.service.SuggestionService;

@RestController
Expand All @@ -25,4 +24,34 @@ public ApiDataResponse<CreateSuggestionResponse> createSuggestion(
Member member = customUserDetails.getMember();
return ApiDataResponse.of(suggestionService.createSuggestion(requestBody, member));
}

@GetMapping
public ApiDataResponse<SuggestionListDto> getSuggestionList(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Member member = customUserDetails.getMember();
return ApiDataResponse.of(SuggestionListDto.from(suggestionService.getSuggestionList(member)));
}

@GetMapping("/{suggestionId}")
public ApiDataResponse<SuggestionDto> getSuggestion(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@PathVariable Long suggestionId
) {
Member member = customUserDetails.getMember();
return ApiDataResponse.of(suggestionService.getSuggestion(suggestionId, member));
}

@DeleteMapping("/{suggestionId}")
public ApiDataResponse<DeleteSuggestionResponse> deleteSuggestion(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@PathVariable Long suggestionId
) {
Member member = customUserDetails.getMember();
suggestionService.deleteSuggestion(suggestionId, member);
return ApiDataResponse.of(DeleteSuggestionResponse.builder()
.suggestionId(suggestionId)
.success(true)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public class Suggestion extends BaseEntity implements Serializable {
@Column(nullable = false, length = 1000)
private String description;

@ColumnDefault("0")
private Integer likeNum;

public static Suggestion from(CreateSuggestionRequest requestBody, Member writer) {
return Suggestion.builder()
.title(requestBody.getTitle().trim())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package swm.hkcc.LGTM.app.modules.suggestion.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class DeleteSuggestionResponse {
private Long suggestionId;
private Boolean success;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package swm.hkcc.LGTM.app.modules.suggestion.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import swm.hkcc.LGTM.app.modules.suggestion.domain.Suggestion;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
public class SuggestionDto {
private static final String LIKE_BIG_NUMBER = "999+";
@NotNull
private Long suggestionId;
@NotNull
private String title;
@NotNull
private String description;
@NotNull
private LocalDateTime date;
@NotNull
private String likeNum;
@NotNull
private Boolean isLiked;
@NotNull
private Boolean isMyPost;

public static SuggestionDto from(Suggestion suggestion, Integer likeNum, Boolean isLiked, Boolean isMyPost) {
return SuggestionDto.builder()
.suggestionId(suggestion.getSuggestionId())
.title(suggestion.getTitle())
.description(suggestion.getDescription())
.date(suggestion.getCreatedAt())
.likeNum(likeNum < 1000 ? likeNum.toString() : LIKE_BIG_NUMBER)
.isLiked(isLiked)
.isMyPost(isMyPost)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package swm.hkcc.LGTM.app.modules.suggestion.dto;

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
public class SuggestionListDto {
private final String infoTitle = "\uD83D\uDC4B 어떤 미션이 필요한지 알려주세요!";
private final String infoDescription = "여러분을 위한 미션이 무엇인지 의견을 남겨주세요.\n리뷰어들이 미션을 제작하는데 큰 도움이 됩니다.";
private List<SuggestionDto> suggestions;

public static SuggestionListDto from(List<SuggestionDto> suggestions) {
return SuggestionListDto.builder()
.suggestions(suggestions)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package swm.hkcc.LGTM.app.modules.suggestion.exception;

import swm.hkcc.LGTM.app.global.constant.ResponseCode;
import swm.hkcc.LGTM.app.global.exception.GeneralException;

public class NotExistSuggestion extends GeneralException {
public NotExistSuggestion() {
super(ResponseCode.NOT_EXIST_SUGGESTION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package swm.hkcc.LGTM.app.modules.suggestion.exception;

import swm.hkcc.LGTM.app.global.constant.ResponseCode;
import swm.hkcc.LGTM.app.global.exception.GeneralException;

public class NotMySuggestion extends GeneralException {
public NotMySuggestion() {
super(ResponseCode.NOT_MY_SUGGESTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
import swm.hkcc.LGTM.app.modules.suggestion.domain.SuggestionLike;

public interface SuggestionLikeRepository extends JpaRepository<SuggestionLike, Long> {
Integer countBySuggestion_SuggestionId(Long suggestionId);
Boolean existsBySuggestion_SuggestionIdAndMember_MemberId(Long suggestionId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.suggestion.domain.Suggestion;
import swm.hkcc.LGTM.app.modules.suggestion.dto.CreateSuggestionRequest;
import swm.hkcc.LGTM.app.modules.suggestion.dto.CreateSuggestionResponse;
import swm.hkcc.LGTM.app.modules.suggestion.dto.SuggestionDto;
import swm.hkcc.LGTM.app.modules.suggestion.exception.NotExistSuggestion;
import swm.hkcc.LGTM.app.modules.suggestion.exception.NotMySuggestion;
import swm.hkcc.LGTM.app.modules.suggestion.repository.SuggestionLikeRepository;
import swm.hkcc.LGTM.app.modules.suggestion.repository.SuggestionRepository;

import java.util.List;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@Service
Expand All @@ -25,4 +32,37 @@ public CreateSuggestionResponse createSuggestion(CreateSuggestionRequest request
return CreateSuggestionResponse.from(suggestion);
}

public List<SuggestionDto> getSuggestionList(Member member) {
List<Suggestion> suggestionList = suggestionRepository.findAll(
Sort.by(Sort.Direction.DESC, "createdAt"));
return suggestionList.stream()
.map(suggestion -> getSuggestionDetail(suggestion, member))
.toList();
}

public SuggestionDto getSuggestion(Long suggestionId, Member member) {
Suggestion suggestion = suggestionRepository.findById(suggestionId).orElseThrow(NotExistSuggestion::new);
return getSuggestionDetail(suggestion, member);
}

private SuggestionDto getSuggestionDetail(Suggestion suggestion, Member member) {
return SuggestionDto.from(suggestion,
suggestionLikeRepository.countBySuggestion_SuggestionId(suggestion.getSuggestionId()),
suggestionLikeRepository.existsBySuggestion_SuggestionIdAndMember_MemberId(suggestion.getSuggestionId(), member.getMemberId()),
Objects.equals(member.getMemberId(), suggestion.getWriter().getMemberId()));
}

public boolean deleteSuggestion(Long suggestionId, Member member) {
Suggestion suggestion = suggestionRepository.findById(suggestionId).orElseThrow(NotExistSuggestion::new);
validateMySuggestion(suggestion, member);
suggestionRepository.delete(suggestion);
return true;
}

private void validateMySuggestion(Suggestion suggestion, Member member) {
if (!Objects.equals(member.getMemberId(), suggestion.getWriter().getMemberId())) {
throw new NotMySuggestion();
}
}

}
Loading

0 comments on commit 0d84266

Please sign in to comment.