diff --git a/src/main/java/com/example/codebase/domain/feed/dto/FeedItemResponseDto.java b/src/main/java/com/example/codebase/domain/feed/dto/FeedItemResponseDto.java index 26774122..3aad79fc 100644 --- a/src/main/java/com/example/codebase/domain/feed/dto/FeedItemResponseDto.java +++ b/src/main/java/com/example/codebase/domain/feed/dto/FeedItemResponseDto.java @@ -4,6 +4,7 @@ import com.example.codebase.domain.agora.dto.AgoraParticipantResponseDTO; import com.example.codebase.domain.agora.entity.Agora; import com.example.codebase.domain.agora.entity.AgoraMedia; +import com.example.codebase.domain.agora.entity.AgoraWithParticipant; import com.example.codebase.domain.artwork.entity.Artwork; import com.example.codebase.domain.artwork.entity.ArtworkMedia; import com.example.codebase.domain.artwork.entity.ArtworkWithIsLike; @@ -76,6 +77,8 @@ public class FeedItemResponseDto { private String disagreeText; private String naturalText; + + private String userVoteStatus; // @Builder.Default @@ -90,38 +93,38 @@ public class FeedItemResponseDto { public static FeedItemResponseDto from(Artwork artwork) { String thumbnailUrl = artwork.getArtworkMedia().get(0).getMediaUrl(); List mediaUrls = artwork.getArtworkMedia().stream() - .map(ArtworkMedia::getMediaUrl) - .collect(Collectors.toList()); + .map(ArtworkMedia::getMediaUrl) + .collect(Collectors.toList()); String authorName = artwork.getMember().getName(); String authorUsername = artwork.getMember().getUsername(); String authorDescription = artwork.getMember().getIntroduction(); String authorProfileImageUrl = artwork.getMember().getPicture(); List tags = Arrays.stream(artwork.getTags().split(",")) - .map(String::trim).collect(Collectors.toList()); + .map(String::trim).collect(Collectors.toList()); FeedItemResponseDto dto = FeedItemResponseDto.builder() - .id(artwork.getId()) - .type(FeedType.artwork) - .title(artwork.getTitle()) - .content(artwork.getDescription()) - .thumbnailUrl(thumbnailUrl) - .authorName(authorName) - .authorUsername(authorUsername) - .authorIntroduction(authorDescription) - .authorProfileImageUrl(authorProfileImageUrl) - .authorCompanyName( - artwork.getMember().getCompanyName() != null ? artwork.getMember().getCompanyName() : null) - .authorCompanyRole( - artwork.getMember().getCompanyRole() != null ? artwork.getMember().getCompanyRole() : null) - .tags(tags) - .categoryId(FeedType.artwork.name()) - .views(artwork.getViews()) - .likes(artwork.getLikes()) - .comments(artwork.getComments()) - .mediaUrls(mediaUrls) - .createdTime(artwork.getCreatedTime()) - .updatedTime(artwork.getUpdatedTime()) - .build(); + .id(artwork.getId()) + .type(FeedType.artwork) + .title(artwork.getTitle()) + .content(artwork.getDescription()) + .thumbnailUrl(thumbnailUrl) + .authorName(authorName) + .authorUsername(authorUsername) + .authorIntroduction(authorDescription) + .authorProfileImageUrl(authorProfileImageUrl) + .authorCompanyName( + artwork.getMember().getCompanyName() != null ? artwork.getMember().getCompanyName() : null) + .authorCompanyRole( + artwork.getMember().getCompanyRole() != null ? artwork.getMember().getCompanyRole() : null) + .tags(tags) + .categoryId(FeedType.artwork.name()) + .views(artwork.getViews()) + .likes(artwork.getLikes()) + .comments(artwork.getComments()) + .mediaUrls(mediaUrls) + .createdTime(artwork.getCreatedTime()) + .updatedTime(artwork.getUpdatedTime()) + .build(); return dto; } @@ -133,31 +136,31 @@ public static FeedItemResponseDto of(Artwork artwork, Boolean isLiked) { public static FeedItemResponseDto from(Post post) { List mediaUrls = post.getPostMedias().stream() - .map(PostMedia::getMediaUrl) - .collect(Collectors.toList()); + .map(PostMedia::getMediaUrl) + .collect(Collectors.toList()); FeedItemResponseDto dto = FeedItemResponseDto.builder() - .id(post.getId()) - .type(FeedType.post) - .title(null) - .content(post.getContent()) - .authorName(post.getAuthor().getName()) - .authorUsername(post.getAuthor().getUsername()) - .authorIntroduction(post.getAuthor().getIntroduction()) - .authorProfileImageUrl(post.getAuthor().getPicture()) - .authorCompanyName( - post.getAuthor().getCompanyName() != null ? post.getAuthor().getCompanyName() : null) - .authorCompanyRole( - post.getAuthor().getCompanyRole() != null ? post.getAuthor().getCompanyRole() : null) - .tags(null) - .categoryId(FeedType.post.name()) - .views(post.getViews()) - .likes(post.getLikes()) - .comments(post.getComments()) - .mediaUrls(mediaUrls) - .createdTime(post.getCreatedTime()) - .updatedTime(post.getUpdatedTime()) - .build(); + .id(post.getId()) + .type(FeedType.post) + .title(null) + .content(post.getContent()) + .authorName(post.getAuthor().getName()) + .authorUsername(post.getAuthor().getUsername()) + .authorIntroduction(post.getAuthor().getIntroduction()) + .authorProfileImageUrl(post.getAuthor().getPicture()) + .authorCompanyName( + post.getAuthor().getCompanyName() != null ? post.getAuthor().getCompanyName() : null) + .authorCompanyRole( + post.getAuthor().getCompanyRole() != null ? post.getAuthor().getCompanyRole() : null) + .tags(null) + .categoryId(FeedType.post.name()) + .views(post.getViews()) + .likes(post.getLikes()) + .comments(post.getComments()) + .mediaUrls(mediaUrls) + .createdTime(post.getCreatedTime()) + .updatedTime(post.getUpdatedTime()) + .build(); return dto; } @@ -169,31 +172,31 @@ public static FeedItemResponseDto from(PostWithIsLiked postWithIsLiked) { public static FeedItemResponseDto from(Exhibition exhibition) { FeedItemResponseDto dto = - FeedItemResponseDto.builder() - .id(exhibition.getId()) - .type(FeedType.exhibition) - .title(exhibition.getTitle()) - .content(exhibition.getDescription()) - .authorName(exhibition.getMember().getName()) - .authorUsername(exhibition.getMember().getUsername()) - .authorIntroduction(exhibition.getMember().getIntroduction()) - .authorProfileImageUrl(exhibition.getMember().getPicture()) - .authorCompanyName( - exhibition.getMember().getCompanyName() != null - ? exhibition.getMember().getCompanyName() - : null) - .authorCompanyRole( - exhibition.getMember().getCompanyRole() != null - ? exhibition.getMember().getCompanyRole() - : null) - .tags(null) - .categoryId(FeedType.exhibition.name()) - .views(0) - .likes(0) - .comments(0) - .createdTime(exhibition.getCreatedTime()) - .updatedTime(exhibition.getUpdatedTime()) - .build(); + FeedItemResponseDto.builder() + .id(exhibition.getId()) + .type(FeedType.exhibition) + .title(exhibition.getTitle()) + .content(exhibition.getDescription()) + .authorName(exhibition.getMember().getName()) + .authorUsername(exhibition.getMember().getUsername()) + .authorIntroduction(exhibition.getMember().getIntroduction()) + .authorProfileImageUrl(exhibition.getMember().getPicture()) + .authorCompanyName( + exhibition.getMember().getCompanyName() != null + ? exhibition.getMember().getCompanyName() + : null) + .authorCompanyRole( + exhibition.getMember().getCompanyRole() != null + ? exhibition.getMember().getCompanyRole() + : null) + .tags(null) + .categoryId(FeedType.exhibition.name()) + .views(0) + .likes(0) + .comments(0) + .createdTime(exhibition.getCreatedTime()) + .updatedTime(exhibition.getUpdatedTime()) + .build(); if (exhibition.getFirstEventSchedule() != null) { FeedItemEventResponseDto eventDto = @@ -212,9 +215,9 @@ public static FeedItemResponseDto from(Exhibition exhibition) { if (exhibition.getExhibitionMedias().size() > 0) { String thumbnailUrl = exhibition.getExhibitionMedias().get(0).getMediaUrl(); List mediaUrls = - exhibition.getExhibitionMedias().stream() - .map(ExhibitionMedia::getMediaUrl) - .collect(Collectors.toList()); + exhibition.getExhibitionMedias().stream() + .map(ExhibitionMedia::getMediaUrl) + .collect(Collectors.toList()); dto.setThumbnailUrl(thumbnailUrl); dto.setMediaUrls(mediaUrls); } @@ -227,6 +230,16 @@ public static FeedItemResponseDto from(ArtworkWithIsLike artworkWithIsLike) { dto.setIsLiked(artworkWithIsLike.getIsLike()); return dto; } + + public static FeedItemResponseDto from(AgoraWithParticipant agoraWithParticipant) { + FeedItemResponseDto dto = from(agoraWithParticipant.getAgora()); + agoraWithParticipant.getAgoraParticipant() + .ifPresent(agoraParticipant -> { + dto.setUserVoteStatus(agoraParticipant.getVote()); + }); + return dto; + } + public static FeedItemResponseDto from(Agora agora) { AgoraParticipantResponseDTO agoraParticipantResponseDTO = AgoraParticipantResponseDTO.of(agora.getAuthor(), agora.getIsAnonymous(), 0); @@ -267,4 +280,5 @@ public static FeedItemResponseDto from(Agora agora) { return dto; } + } diff --git a/src/test/java/com/example/codebase/controller/FeedControllerTest.java b/src/test/java/com/example/codebase/controller/FeedControllerTest.java index 6d883d39..0bb12a95 100644 --- a/src/test/java/com/example/codebase/controller/FeedControllerTest.java +++ b/src/test/java/com/example/codebase/controller/FeedControllerTest.java @@ -23,6 +23,7 @@ import com.example.codebase.domain.post.entity.Post; import com.example.codebase.domain.post.repository.PostRepository; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.transaction.Transactional; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -36,9 +37,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import jakarta.transaction.Transactional; import java.io.IOException; - import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -112,13 +111,13 @@ public Member createOrLoadMember() { } Member dummy = Member.builder() - .username("testid") - .password(passwordEncoder.encode("1234")) - .email("email") - .name("test") - .activated(true) - .createdTime(LocalDateTime.now()) - .build(); + .username("testid") + .password(passwordEncoder.encode("1234")) + .email("email") + .name("test") + .activated(true) + .createdTime(LocalDateTime.now()) + .build(); MemberAuthority memberAuthority = new MemberAuthority(); memberAuthority.setAuthority(Authority.of("ROLE_USER")); @@ -142,22 +141,22 @@ public Artwork createOrLoadArtwork(int index, boolean isVisible, int mediaSize) String url = "https://test.com/image.jpg"; ArtworkMedia artworkMedia = ArtworkMedia.builder() - .artworkMediaType(MediaType.image) - .mediaUrl(url) - .description("미디어 설명") - .build(); + .artworkMediaType(MediaType.image) + .mediaUrl(url) + .description("미디어 설명") + .build(); artworkMediaList.add(artworkMedia); } Artwork dummy = Artwork.builder() - .title("아트워크_테스트" + index) - .description("작품 설명") - .tags("태그1,태그2,태그3") - .visible(isVisible) - .member(createOrLoadMember()) - .artworkMedia(artworkMediaList) - .createdTime(LocalDateTime.now().plusSeconds(index)) - .build(); + .title("아트워크_테스트" + index) + .description("작품 설명") + .tags("태그1,태그2,태그3") + .visible(isVisible) + .member(createOrLoadMember()) + .artworkMedia(artworkMediaList) + .createdTime(LocalDateTime.now().plusSeconds(index)) + .build(); return artworkRepository.save(dummy); } @@ -167,10 +166,10 @@ public Post createPost() { Member loadMember = createOrLoadMember(); Post post = Post.builder() - .content("content") - .author(loadMember) - .createdTime(LocalDateTime.now()) - .build(); + .content("content") + .author(loadMember) + .createdTime(LocalDateTime.now()) + .build(); return postRepository.save(post); } @@ -182,60 +181,60 @@ public Exhibition createOrLoadExhibition(int idx) { } Exhibition exhibition = - Exhibition.builder() - .title("공모전 제목" + idx) - .description("공모전 설명" + idx) - .link("링크" + idx) - .price(10000 + idx) - .type(EventType.STANDARD) - .createdTime(LocalDateTime.now()) - .member(createOrLoadMember()) - .build(); + Exhibition.builder() + .title("공모전 제목" + idx) + .description("공모전 설명" + idx) + .link("링크" + idx) + .price(10000 + idx) + .type(EventType.STANDARD) + .createdTime(LocalDateTime.now()) + .member(createOrLoadMember()) + .build(); ExhibitionMedia thumbnail = - ExhibitionMedia.builder() - .mediaUrl("url" + idx) - .exhibtionMediaType(ExhibtionMediaType.image) - .createdTime(LocalDateTime.now()) - .build(); + ExhibitionMedia.builder() + .mediaUrl("url" + idx) + .exhibtionMediaType(ExhibtionMediaType.image) + .createdTime(LocalDateTime.now()) + .build(); exhibition.addExhibitionMedia(thumbnail); ExhibitionMedia media = - ExhibitionMedia.builder() - .mediaUrl("url" + idx) - .exhibtionMediaType(ExhibtionMediaType.image) - .createdTime(LocalDateTime.now()) - .build(); + ExhibitionMedia.builder() + .mediaUrl("url" + idx) + .exhibtionMediaType(ExhibtionMediaType.image) + .createdTime(LocalDateTime.now()) + .build(); exhibition.addExhibitionMedia(media); exhibitionRepository.save(exhibition); EventSchedule eventSchedule = - EventSchedule.builder() - .eventDate(LocalDate.now()) - .startTime(LocalTime.now()) - .endTime(LocalTime.now().plusHours(2)) - .detailLocation("상세 위치") - .createdTime(LocalDateTime.now()) - .build(); + EventSchedule.builder() + .eventDate(LocalDate.now()) + .startTime(LocalTime.now()) + .endTime(LocalTime.now().plusHours(2)) + .detailLocation("상세 위치") + .createdTime(LocalDateTime.now()) + .build(); eventSchedule.setEvent(exhibition); Location location = - Location.builder() - .latitude(123.123) - .longitude(123.123) - .address("주소") - .name("장소 이름") - .englishName("장소 영어 이름") - .phoneNumber("010-1234-1234") - .webSiteUrl("test.com") - .snsUrl("test.com") - .build(); + Location.builder() + .latitude(123.123) + .longitude(123.123) + .address("주소") + .name("장소 이름") + .englishName("장소 영어 이름") + .phoneNumber("010-1234-1234") + .webSiteUrl("test.com") + .snsUrl("test.com") + .build(); eventSchedule.setLocation(location); locationRepository.save(location); ExhibitionParticipant exhibitionParticipant = - ExhibitionParticipant.builder().member(createOrLoadMember()).build(); + ExhibitionParticipant.builder().member(createOrLoadMember()).build(); exhibitionParticipant.setEventSchedule(eventSchedule); exhibitionParticipantRepository.save(exhibitionParticipant); @@ -274,6 +273,14 @@ public Agora createAgora(int idx, boolean isAnnoymous) { return dummy; } + @Transactional + public void voteAgora(Agora agora, Member member, String voteText) { + AgoraParticipant agoraParticipant = AgoraParticipant.create(); + agoraParticipant.setAgoraAndMember(agora, member); + agoraParticipant.createVote(voteText); + agoraParticipantRepository.save(agoraParticipant); + } + @DisplayName("피드 생성") @Test public void createFeed() throws Exception { @@ -344,11 +351,11 @@ public void createFeed_Agora() throws Exception { createAgora(3, true); mockMvc.perform( - post("/api/feed") - .param("page", "0") - ) - .andExpect(status().isCreated()) - .andDo(print()); + post("/api/feed") + .param("page", "0") + ) + .andExpect(status().isCreated()) + .andDo(print()); } @DisplayName("피드 생성 시 일부 데이터 없을 떄 ") @@ -372,11 +379,11 @@ public void createFeed_Agora() throws Exception { createPost(); mockMvc.perform( - post("/api/feed") - .param("page", "1") - ) - .andExpect(status().isCreated()) - .andDo(print()); + post("/api/feed") + .param("page", "1") + ) + .andExpect(status().isCreated()) + .andDo(print()); } @DisplayName("피드 생성 시 일부 데이터가 없다면 ") @@ -390,11 +397,11 @@ public void createFeed2() throws Exception { createOrLoadExhibition(1); mockMvc.perform( - post("/api/feed") - .param("page", "0") - ) - .andExpect(status().isCreated()) - .andDo(print()); + post("/api/feed") + .param("page", "0") + ) + .andExpect(status().isCreated()) + .andDo(print()); } @DisplayName("피드 생성 시 전체 데이터가 없다면 ") @@ -402,11 +409,11 @@ public void createFeed2() throws Exception { public void createFeed3() throws Exception { mockMvc.perform( - post("/api/feed") - .param("page", "0") - ) - .andExpect(status().isCreated()) - .andDo(print()); + post("/api/feed") + .param("page", "0") + ) + .andExpect(status().isCreated()) + .andDo(print()); } @DisplayName("피드 조회 시 page가 0이면 에러 발생") @@ -423,11 +430,11 @@ public void createFeed4() throws Exception { createOrLoadExhibition(1); mockMvc.perform( - post("/api/feed") - .param("page", "0") - ) - .andExpect(status().isCreated()) - .andDo(print()); + post("/api/feed") + .param("page", "0") + ) + .andExpect(status().isCreated()) + .andDo(print()); } @WithMockCustomUser(username = "testid", role = "USER") @@ -444,24 +451,69 @@ public void createFeed5() throws Exception { createOrLoadExhibition(1); mockMvc.perform( - post("/api/posts/" + post.getId() + "/like") - ) - .andDo(print()) - .andExpect(status().isOk()); + post("/api/posts/" + post.getId() + "/like") + ) + .andDo(print()) + .andExpect(status().isOk()); mockMvc.perform( - post("/api/artworks/" + artwork.getId() + "/like") - ) - .andDo(print()) - .andExpect(status().isOk()); + post("/api/artworks/" + artwork.getId() + "/like") + ) + .andDo(print()) + .andExpect(status().isOk()); mockMvc.perform( - post("/api/feed") - .param("page", "0") - ) - .andDo(print()) - .andExpect(status().isCreated()); + post("/api/feed") + .param("page", "0") + ) + .andDo(print()) + .andExpect(status().isCreated()); } + @WithMockCustomUser(username = "testid", role = "USER") + @DisplayName("일부 아고라 투표 여부에 따른 피드 조회") + @Test + public void createFeed6() throws Exception { + Member member = createOrLoadMember(); + Artwork artwork = createOrLoadArtwork(1, true, 1); + createOrLoadArtwork(2, true, 1); + + // Post 생성 및 저장 + Post post = createPost(); + createPost(); + createOrLoadExhibition(1); + + // Agora + Agora agora1 = createAgora(1, false); + createAgora(2, true); + createAgora(3, true); + + mockMvc.perform( + post("/api/posts/" + post.getId() + "/like") + ) + .andDo(print()) + .andExpect(status().isOk()); + + mockMvc.perform( + post("/api/artworks/" + artwork.getId() + "/like") + ) + .andDo(print()) + .andExpect(status().isOk()); + + mockMvc.perform( + post("/api/agoras/" + agora1.getId() + "/vote") + .contentType(org.springframework.http.MediaType.APPLICATION_JSON) + .content(agora1.getAgreeText()) + ) + .andDo(print()) + .andExpect(status().isOk()); + + mockMvc.perform( + post("/api/feed") + .param("page", "0") + ) + .andDo(print()) + .andExpect(status().isCreated()); + } } \ No newline at end of file