Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#50 [feat] 글모임 최신 글감 GET #51

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mile.exception.message.SuccessMessage;
import com.mile.moim.serivce.MoimService;
import com.mile.moim.serivce.dto.ContentListResponse;
import com.mile.moim.serivce.dto.MoimTopicResponse;
import com.mile.moim.serivce.dto.MoimInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -20,24 +21,34 @@ public class MoimController implements MoimControllerSwagger {

private final MoimService moimService;

@GetMapping("/{moimId}")
@Override
@GetMapping("/{moimId}")
public SuccessResponse<ContentListResponse> getTopicsFromMoim(
@PathVariable final Long moimId,
final Principal principal
) {
return SuccessResponse.of(SuccessMessage.TOPIC_SEARCH_SUCCESS, moimService.getContentsFromMoim(moimId, Long.valueOf(principal.getName())));
}

@GetMapping("/{moimId}/authenticate")
@Override
@GetMapping("/{moimId}/authenticate")
public SuccessResponse getAuthenticationOfMoim(
final Long moimId,
final Principal principal
) {
return SuccessResponse.of(SuccessMessage.MOIM_AUTHENTICATE_SUCCESS, moimService.getAuthenticateUserOfMoim(moimId, Long.valueOf(principal.getName())));
}


@Override
@GetMapping("/{moimId}/topic")
public SuccessResponse<MoimTopicResponse> getTopicFromMoim(
@PathVariable Long moimId
) {
return SuccessResponse.of(SuccessMessage.MOIM_TOPIC_GET_SUCCESS, moimService.getTopicFromMoim(moimId));

}

@GetMapping("/{moimId}/info")
@Override
public SuccessResponse<MoimInfoResponse> getMoimInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mile.dto.ErrorResponse;
import com.mile.dto.SuccessResponse;
import com.mile.moim.serivce.dto.ContentListResponse;
import com.mile.moim.serivce.dto.MoimTopicResponse;
import com.mile.moim.serivce.dto.MoimInfoResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -38,7 +39,8 @@ SuccessResponse<ContentListResponse> getTopicsFromMoim(
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "사용자의 권한이 확인되었습니다."),
@ApiResponse(responseCode = "403", description = "사용자 검증 토큰이 유효하지 안습니다.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))
@ApiResponse(responseCode = "403", description = "사용자 검증 토큰이 유효하지 않습니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
),
@ApiResponse(responseCode = "500", description = "서버 내부 오류입니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
Expand All @@ -49,6 +51,21 @@ SuccessResponse getAuthenticationOfMoim(
final Principal principal
);


@Operation(summary = "글모임 최근 글감 ")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "글감 조회가 완료되었습니다."),
@ApiResponse(responseCode = "404", description = "1. 해당 글모임이 존재하지 않습니다.\n 2.해당 모임의 주제가 존재하지 않습니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "서버 내부 오류입니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
}
)
SuccessResponse<MoimTopicResponse> getTopicFromMoim(
@PathVariable Long moimId
);

@Operation(summary = "글모임 뷰 - 글모임 정보 조회")
@ApiResponses(
value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ErrorMessage {
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 댓글이 존재하지 않습니다."),
CURIOUS_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 궁금해요는 존재하지 않습니다."),
TOPIC_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 주제가 존재하지 않습니다."),
MOIM_TOPIC_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "해당 모임의 주제가 존재하지 않습니다."),
/*
Bad Request
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public enum SuccessMessage {
MOIM_INFO_SUCCESS(HttpStatus.OK.value(), "글모임 조회가 완료되었습니다."),
PRESIGNED_URL_GET_SUCCESS(HttpStatus.OK.value(), "이미지를 업로드할 url이 발행되었습니다."),
POST_DELETE_SUCCESS(HttpStatus.OK.value(), "글 삭제가 완료되었습니다."),
MOIM_TOPIC_GET_SUCCESS(HttpStatus.OK.value(), "글감 조회가 완료되었습니다."),

/*
201 CREATED
*/
Expand Down
14 changes: 14 additions & 0 deletions module-domain/src/main/java/com/mile/moim/serivce/MoimService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mile.moim.repository.MoimRepository;
import com.mile.moim.serivce.dto.ContentListResponse;
import com.mile.moim.serivce.dto.MoimAuthenticateResponse;
import com.mile.moim.serivce.dto.MoimTopicResponse;
import com.mile.moim.serivce.dto.MoimInfoResponse;
import com.mile.topic.serivce.TopicService;
import com.mile.utils.DateUtil;
Expand Down Expand Up @@ -44,7 +45,20 @@ public MoimAuthenticateResponse getAuthenticateUserOfMoim(
) {
return MoimAuthenticateResponse.of(writerNameService.isUserInMoim(moimId, userId));
}
private Moim findById(
final Long moimId
) {
return moimRepository.findById(moimId).orElseThrow(
() -> new NotFoundException(ErrorMessage.MOIM_NOT_FOUND)
);
}

public MoimTopicResponse getTopicFromMoim(
final Long moimId
) {
return MoimTopicResponse.of(topicService.findLatestTopicByMoim(findById(moimId)));
}

public MoimInfoResponse getMoimInfo(
final Long moimId
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mile.moim.serivce.dto;

public record MoimTopicResponse(
String content
) {

public static MoimTopicResponse of(
final String content
) {
return new MoimTopicResponse(content);
}
}
4 changes: 1 addition & 3 deletions module-domain/src/main/java/com/mile/topic/domain/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public class Topic extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private Moim moim;

private String keyword;
private String topic;
private String content;
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.List;

public interface TopicRepository extends JpaRepository<Topic, Long> {
public interface TopicRepository extends JpaRepository<Topic, Long>, TopicRepositoryCustom {

public List<Topic> findByMoimId(final Long moimId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mile.topic.repository;

import com.mile.moim.domain.Moim;

import java.util.Optional;

public interface TopicRepositoryCustom {

Optional<String> findLatestTopicByMoim(final Moim moim);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mile.topic.repository;

import com.mile.moim.domain.Moim;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.Optional;

import static com.mile.topic.domain.QTopic.topic;

@RequiredArgsConstructor
public class TopicRepositoryImpl implements TopicRepositoryCustom {

private final JPAQueryFactory jpaQueryFactory;

@Override
public Optional<String> findLatestTopicByMoim(final Moim moim) {
return Optional.ofNullable(jpaQueryFactory.selectFrom(topic)
.select(topic.content)
.where(topic.moim.eq(moim))
.orderBy(topic.createdAt.desc())
.fetchFirst());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryDSL을 사용하니까 가독성이 너무 좋네요..

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mile.config.BaseTimeEntity;
import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.NotFoundException;
import com.mile.moim.domain.Moim;
import com.mile.topic.domain.Topic;
import com.mile.topic.repository.TopicRepository;
import com.mile.topic.serivce.dto.ContentResponse;
Expand Down Expand Up @@ -61,4 +62,13 @@ public Topic findById(
() -> new NotFoundException(ErrorMessage.TOPIC_NOT_FOUND)
);
}

public String findLatestTopicByMoim(
final Moim moim
) {
return topicRepository.findLatestTopicByMoim(moim)
.orElseThrow(
() -> new NotFoundException(ErrorMessage.MOIM_TOPIC_NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public record ContentResponse(
public static ContentResponse of(
final Topic topic
) {
return new ContentResponse(topic.getId(), topic.getTopic());
return new ContentResponse(topic.getId(), topic.getContent());
}
}
Loading