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

[FEAT] 공간에 대한 아티클 조회 기능을 구현한다. #52

Merged
merged 6 commits into from
Jul 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public List<ArticleSummaryResponseDto> readArticleSummaryList(final Long userId)

return result;
}

public Article findArticleBySpace(final Space space) {
return articleRepository.findArticleBySpace(space).orElseThrow(
() -> new NotFoundException(Error.NOT_FOUND_ARTICLE_OF_SPACE_EXCEPTION,
Error.NOT_FOUND_ARTICLE_OF_SPACE_EXCEPTION.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import indipage.org.indipage.api.space.controller.dto.response.BookRecommendationResponseDto;
import indipage.org.indipage.api.space.controller.dto.response.FollowSpaceRelationResponseDto;
import indipage.org.indipage.api.space.controller.dto.response.SpaceDto;
import indipage.org.indipage.api.space.controller.dto.response.SpaceOfArticleResponseDto;
import indipage.org.indipage.api.space.service.SpaceService;
import indipage.org.indipage.common.dto.ApiResponse;
import indipage.org.indipage.exception.Success;
Expand Down Expand Up @@ -46,4 +47,10 @@ public ApiResponse createFollowSpace(@PathVariable final Long spaceId) {
spaceService.createFollowSpace(1L, spaceId);
return ApiResponse.success(Success.CREATE_FOLLOW_SPACE_SUCCESS);
}

@GetMapping("/{spaceId}/article")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<SpaceOfArticleResponseDto> readArticleOfSpace(@PathVariable final Long spaceId) {
return ApiResponse.success(Success.READ_ARTICLE_OF_SPACE_SUCCESS, spaceService.readArticleOfSpace(spaceId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package indipage.org.indipage.api.space.controller.dto.response;

import indipage.org.indipage.domain.Article;
import indipage.org.indipage.domain.Space;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class SpaceOfArticleResponseDto {

private String spaceName;
private String title;
private String spaceType;
private Long id;
private Boolean isIssued;

private static boolean getIsIssued(Article article) {
return article.getIssueDate().isBefore(LocalDateTime.now());
}

public static SpaceOfArticleResponseDto of(Space space, Article article) {
return SpaceOfArticleResponseDto
.builder()
.spaceName(space.getName())
.title(article.getTitle())
.spaceType(space.getType().getTypeName())
.id(article.getId())
.isIssued(getIsIssued(article))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package indipage.org.indipage.api.space.service;

import indipage.org.indipage.api.article.service.ArticleService;
import indipage.org.indipage.api.space.controller.dto.response.BookRecommendationResponseDto;
import indipage.org.indipage.api.space.controller.dto.response.FollowSpaceRelationResponseDto;
import indipage.org.indipage.api.space.controller.dto.response.SpaceDto;
import indipage.org.indipage.api.space.controller.dto.response.SpaceOfArticleResponseDto;
import indipage.org.indipage.api.ticket.service.TicketService;
import indipage.org.indipage.api.user.service.UserService;
import indipage.org.indipage.domain.*;
import indipage.org.indipage.domain.Relation.BookRecommendationRelation;
Expand All @@ -25,6 +28,8 @@ public class SpaceService {
private final SpaceRepository spaceRepository;
private final FollowSpaceRelationRepository followSpaceRelationRepository;
private final UserService userService;
private final ArticleService articleService;
private final TicketService ticketService;

public SpaceDto readSpace(final Long spaceId) {
Space space = findSpace(spaceId);
Expand Down Expand Up @@ -61,6 +66,12 @@ public void createFollowSpace(final Long userId, final Long spaceId) {
followSpaceRelationRepository.save(FollowSpaceRelation.create(user, space));
}

public SpaceOfArticleResponseDto readArticleOfSpace(final Long spaceId) {
Space space = findSpace(spaceId);
Article article = articleService.findArticleBySpace(space);
return SpaceOfArticleResponseDto.of(space, article);
}

public Space findSpace(final Long spaceId) {
return spaceRepository.findById(spaceId).orElseThrow(
() -> new NotFoundException(Error.NOT_FOUND_SPACE_EXCEPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface ArticleRepository extends Repository<Article, Long> {
Optional<Article> findById(Long id);

List<Article> findAll();

Optional<Article> findArticleBySpace(Space space);
}
1 change: 1 addition & 0 deletions src/main/java/indipage/org/indipage/exception/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum Error {
NOT_FOUND_TICKET_EXCEPTION(HttpStatus.NOT_FOUND, "해당 공간에 티켓이 존재하지 않습니다."),
NOT_FOUND_ARTICLE_BOOKMARK_EXCEPTION(HttpStatus.NOT_FOUND, "해당 아티클에 대한 북마크가 존재하지 않습니다."),
NOT_FOUND_SPACE_BOOKMARK_EXCEPTION(HttpStatus.NOT_FOUND, "해당 공간에 대한 북마크가 존재하지 않습니다."),
NOT_FOUND_ARTICLE_OF_SPACE_EXCEPTION(HttpStatus.NOT_FOUND, "해당 공간에 대한 아티클이 존재하지 않습니다."),
/**
* 409 CONFLICT
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/indipage/org/indipage/exception/Success.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum Success {
READ_IS_SPACE_BOOKMARKED_SUCCESS(HttpStatus.OK, "공간 북마크 여부 조회 성공"),
DELETE_ARTICLE_BOOKMARK_SUCCESS(HttpStatus.OK, "아티클 북마크 삭제 성공"),
DELETE_SPACE_BOOKMARK_SUCCESS(HttpStatus.OK, "공간 북마크 삭제 성공"),
READ_ARTICLE_OF_SPACE_SUCCESS(HttpStatus.OK, "공간에 대한 아티클 조회 성공"),
/**
* 201 CREATED
*/
Expand All @@ -35,7 +36,8 @@ public enum Success {
CREATE_FOLLOW_SPACE_SUCCESS(HttpStatus.CREATED, "조르기 등록에 성공했습니다."),
CREATE_RECEIVE_TICKET_SUCCESS(HttpStatus.CREATED, "티켓 수령에 성공했습니다."),
CREATE_ARTICLE_BOOKMARK_SUCCESS(HttpStatus.CREATED, "아티클 북마크 등록에 성공했습니다."),
CREATE_SPACE_BOOKMARK_SUCCESS(HttpStatus.CREATED, "공간 북마크 등록에 성공했습니다.")
CREATE_SPACE_BOOKMARK_SUCCESS(HttpStatus.CREATED, "공간 북마크 등록에 성공했습니다."),

;

private final HttpStatus httpStatus;
Expand Down