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

[feature/7-storyLike] 스토리 좋아요 누르기 #26

Merged
merged 4 commits into from
Aug 10, 2023
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
@@ -0,0 +1,39 @@
package com.umc.place.exhibition.dto;

import com.umc.place.exhibition.entity.Exhibition;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class SearchExhibitionsByNameResDto {

private List<SearchedExhibitionByName> searchedExhibitions = new ArrayList();

@Builder
public SearchExhibitionsByNameResDto(Page<Exhibition> exhibitions) {
this.searchedExhibitions
= exhibitions.stream()
.map(exhibition -> new SearchedExhibitionByName(exhibition))
.collect(Collectors.toList());
}

@Data
@NoArgsConstructor
public static class SearchedExhibitionByName {
private Long exhibitionIdx;
private String exhibitionName;

@Builder
public SearchedExhibitionByName(Exhibition exhibition) {
this.exhibitionIdx = exhibition.getExhibitionIdx();
this.exhibitionName = exhibition.getExhibitionName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
@Repository
public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {
Page<Exhibition> findByCategory(Category category, Pageable pageable); // 카테고리 기반 전체 조회(페이징)

Page<Exhibition> findAll(Pageable pageable); // 전체 조회(페이징)

boolean existsByCategory(Category category);

@Query("select case when count(e) > 0 then true else false end from Exhibition e where Function('replace', e.location, ' ', '') like %:location%")
Expand All @@ -25,4 +27,6 @@ public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {

@Query("select e from Exhibition e where Function('replace', e.location, ' ', '') like %:location%")
Page<Exhibition> findByLocationLike(@Param("location") String location, Pageable pageable);

Page<Exhibition> findByExhibitionNameContainingOrderByExhibitionName(String searchKeyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import com.umc.place.comment.service.CommentService;
import com.umc.place.common.BaseException;
import com.umc.place.common.BaseResponse;
import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto;
import com.umc.place.story.dto.StoryDetailResponseDto;
import com.umc.place.story.dto.StoryUploadRequestDto;
import com.umc.place.story.dto.StoryUploadResponseDto;
import com.umc.place.story.service.StoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import static com.umc.place.common.BaseResponseStatus.NULL_STORY;
import static com.umc.place.common.BaseResponseStatus.SUCCESS;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -41,6 +45,17 @@ public BaseResponse<StoryDetailResponseDto> getStoryDetail(@PathVariable Long st
}
}

@GetMapping("/search")
public BaseResponse<SearchExhibitionsByNameResDto> getExhibitionWhenUploadStory(
@PageableDefault(size = 5) Pageable pageable,
@RequestParam(required = false) String searchWord) {
try {
return new BaseResponse<>(storyService.searchExhibitionByName(searchWord, pageable));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

@PostMapping("/{storyIdx}/comment")
public BaseResponse<CommentUploadResDto> uploadStoryComment(@PathVariable Long storyIdx,
@RequestBody CommentUploadReqDto reqDto,
Expand All @@ -52,6 +67,17 @@ public BaseResponse<CommentUploadResDto> uploadStoryComment(@PathVariable Long s
}
}

@PostMapping("/{storyIdx}/like")
public BaseResponse<Void> likeStory(@PathVariable Long storyIdx,
@RequestParam Long userIdx) {
try {
storyService.likeStory(storyIdx, userIdx);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

@GetMapping("/uploadView")
public BaseResponse<StoryUploadResponseDto> getStoryUploadView(@RequestParam Long userId) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class StoryLike extends BaseEntity {
private Story story;

@Builder
public StoryLike(Long storyLikeIdx, User user, Story story) {
this.storyLikeIdx = storyLikeIdx;
public StoryLike(User user, Story story) {
this.user = user;
this.story = story;
this.story.getLikes().add(this); // 양방향 연관관계 메서드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
@Repository
public interface StoryLikeRepository extends JpaRepository<StoryLike, Long> {
Boolean existsByUserAndStory(User user, Story story);

StoryLike findByUserAndStory(User user, Story story);

List<StoryLike> findByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import com.umc.place.comment.dto.CommentResDto;
import com.umc.place.comment.repository.CommentRepository;
import com.umc.place.common.BaseException;
import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto;
import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.exhibition.repository.ExhibitionRepository;
import com.umc.place.story.dto.StoryDetailResponseDto;
import com.umc.place.story.dto.StoryUploadRequestDto;
import com.umc.place.story.dto.StoryUploadResponseDto;
import com.umc.place.story.entity.Story;
import com.umc.place.story.entity.StoryHistory;
import com.umc.place.story.entity.StoryLike;
import com.umc.place.story.repository.StoryHistoryRepository;
import com.umc.place.story.repository.StoryLikeRepository;
import com.umc.place.story.repository.StoryRepository;
import com.umc.place.user.entity.User;
import com.umc.place.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -25,6 +29,8 @@
import java.util.stream.Collectors;

import static com.umc.place.common.BaseResponseStatus.*;
import static com.umc.place.common.Constant.ACTIVE;
import static com.umc.place.common.Constant.INACTIVE;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -79,6 +85,19 @@ public StoryDetailResponseDto getStoryDetail(Long storyIdx, Long userId) throws
}
}

public SearchExhibitionsByNameResDto searchExhibitionByName(String searchWord, Pageable page) throws BaseException {
try {
searchWord = searchWord.trim();
Page<Exhibition> searchedExhibitions
= exhibitionRepository.findByExhibitionNameContainingOrderByExhibitionName(searchWord, page);
return SearchExhibitionsByNameResDto.builder()
.exhibitions(searchedExhibitions)
.build();
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

@Transactional
public StoryUploadResponseDto uploadStory(StoryUploadRequestDto storyUploadRequestDto, Long userId) throws BaseException {
try {
Expand Down Expand Up @@ -137,4 +156,31 @@ public StoryUploadResponseDto getStoryView(Long userId) throws BaseException {
throw new BaseException(DATABASE_ERROR);
}
}

@Transactional
public void likeStory(Long storyIdx, Long userIdx) throws BaseException {
try {
User user = userRepository.findById(userIdx).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Story story = storyRepository.findById(storyIdx).orElseThrow(() -> new BaseException(INVALID_STORY_IDX));

StoryLike storyLike = storyLikeRepository.findByUserAndStory(user, story);
if (storyLike == null) {
StoryLike newLike = StoryLike.builder()
.user(user)
.story(story)
.build();
storyLikeRepository.save(newLike);
} else {
if (storyLike.getStatus().equals(ACTIVE)) {
storyLike.setStatus(INACTIVE);
} else {
storyLike.getStatus().equals(ACTIVE);
}
}
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}
}