Skip to content

Commit

Permalink
Merge pull request #8 from LikeLion-12th-SKHU/colorlist
Browse files Browse the repository at this point in the history
fix : moodId List로 받기, 분위기별 조회 수정#3
  • Loading branch information
shinheekim authored Aug 4, 2024
2 parents 4e4f18a + 50084bb commit 994eee1
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class Mood {
private String name;

@JsonIgnore
@OneToMany(mappedBy = "mood", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> postList = new ArrayList<>();
@ManyToMany(mappedBy = "moods", cascade = CascadeType.ALL)
private List<Post> posts = new ArrayList<>();

@Builder
public Mood(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.skhu.likelion12thteam03be.post.api.dto.response.PostInfoResDto;
import net.skhu.likelion12thteam03be.post.api.dto.response.PostListResDto;
import net.skhu.likelion12thteam03be.post.application.PostService;
import net.skhu.likelion12thteam03be.post.domain.Post;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,6 +14,7 @@

import java.io.IOException;
import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/posts")
Expand Down Expand Up @@ -63,8 +65,8 @@ public ResponseEntity<PostListResDto> postFindByCategoryId(@PathVariable("catego

// 글 분위기별 조회
@GetMapping("/moods/{moodId}")
public ResponseEntity<PostListResDto> postFindByMoodId(@PathVariable("moodId") Long moodId) {
PostListResDto postListResDto = postService.postFindByMoodId(moodId);
public ResponseEntity<PostListResDto> postFindByMoodId(@PathVariable("moodId") List<Long> moodIds) {
PostListResDto postListResDto = postService.postFindByMoodIds(moodIds);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.skhu.likelion12thteam03be.post.api.dto.request;

import java.util.List;

public record PostSaveReqDto(
String title,
String content,
Long locationId,
Integer time,
Integer price,
Long categoryId,
Long moodId
List<Long> moodIds
) {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.skhu.likelion12thteam03be.post.api.dto.request;

import java.util.List;

public record PostUpdateReqDto(
String title,
String content,
Long locationId,
Integer time,
Integer price,
Long categoryId,
Long moodId
List<Long> moodIds
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.skhu.likelion12thteam03be.post.domain.Post;

import java.time.LocalDateTime;
import java.util.List;

@Builder
public record PostInfoResDto(
Expand All @@ -17,7 +18,7 @@ public record PostInfoResDto(
Integer time,
Integer price,
Category category,
Mood mood,
List<Mood> moods,
LocalDateTime createDate,
LocalDateTime modifiedDate
) {
Expand All @@ -29,7 +30,7 @@ public static PostInfoResDto from(Post post) {
.time(post.getTime())
.price(post.getPrice())
.category(post.getCategory())
.mood(post.getMood())
.moods(post.getMoods())
.imgUrl(post.getImgUrl())
.createDate(post.getCreateDate())
.modifiedDate(post.getModifiedDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = false)
Expand All @@ -43,16 +45,15 @@ public void postSave(PostSaveReqDto postSaveReqDto, @RequestPart(required = fals
String loginId = principal.getName();

User user = userRepository.findByLoginId(loginId)
.orElseThrow(() -> new IllegalArgumentException("User not found with id = " + loginId));
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다. loginId = " + loginId));

Location location = locationRepository.findById(postSaveReqDto.locationId())
.orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId()));

Category category = categoryRepository.findById(postSaveReqDto.categoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId()));

Mood mood = moodRepository.findById(postSaveReqDto.moodId())
.orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postSaveReqDto.moodId()));
List<Mood> moods = moodRepository.findAllById(postSaveReqDto.moodIds());

Post post = Post.builder()
.title(postSaveReqDto.title())
Expand All @@ -61,7 +62,7 @@ public void postSave(PostSaveReqDto postSaveReqDto, @RequestPart(required = fals
.time(postSaveReqDto.time())
.price(postSaveReqDto.price())
.category(category)
.mood(mood)
.moods(moods)
.imgUrl(imgUrl)
.user(user)
.build();
Expand Down Expand Up @@ -115,15 +116,18 @@ public PostListResDto postFindByCategoryId(Long categoryId) {
}

// 글 분위기별 조회
public PostListResDto postFindByMoodId(Long moodId) {
Optional<Post> posts = Optional.ofNullable(postRepository.findById(moodId).orElseThrow(
() -> new IllegalArgumentException("해당 분위기의 글을 찾을 수 없습니다. moodId = " + moodId)
));
public PostListResDto postFindByMoodIds(List<Long> moodIds) {
List<PostInfoResDto> postInfoResDtoList = new ArrayList<>();

List<PostInfoResDto> postInfoResDtoList = posts.stream()
.map(PostInfoResDto::from)
.toList();
for (Long moodId : moodIds) {
List<Post> posts = postRepository.findByMoodId(moodId);

List<PostInfoResDto> collectPostInfoResDtoList = posts.stream()
.map(PostInfoResDto::from)
.collect(Collectors.toList());

postInfoResDtoList.addAll(collectPostInfoResDtoList);
}
return PostListResDto.from(postInfoResDtoList);
}

Expand Down Expand Up @@ -162,12 +166,11 @@ public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, Multipart
Category category = categoryRepository.findById(postUpdateReqDto.categoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postUpdateReqDto.categoryId()));

Mood mood = moodRepository.findById(postUpdateReqDto.moodId())
.orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postUpdateReqDto.moodId()));
List<Mood> moods = moodRepository.findAllById(postUpdateReqDto.moodIds());

String imgUrl = s3Service.upload(multipartFile, "post");

post.update(location, category, postUpdateReqDto, mood, imgUrl);
post.update(location, category, postUpdateReqDto, moods, imgUrl);
postRepository.save(post);
}

Expand All @@ -190,7 +193,7 @@ public void postDelete(Long postId, Principal principal) throws IOException {
try {
s3Service.delete(url, "post");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("이미지 삭제 중 오류 발 생", e);
throw new IllegalArgumentException("이미지 삭제 중 오류 발생", e);
}
postRepository.delete(post);
},
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto;
import net.skhu.likelion12thteam03be.user.domain.User;

import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -35,9 +37,15 @@ public class Post extends Time {
@JoinColumn(name = "categoryId")
private Category category;

@ManyToOne
@JoinColumn(name = "moodId")
private Mood mood; // 감정 키워드
/*@ManyToOne
@JoinColumn(name = "moodId")*/
@ManyToMany
@JoinTable(
name = "post_moods",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "mood_id")
)
private List<Mood> moods; // 분위기 키워드

private String imgUrl; // 사진

Expand All @@ -46,26 +54,26 @@ public class Post extends Time {
private User user;

@Builder
public Post(String title, String content, Location location, Integer time, Integer price, Category category, Mood mood, String imgUrl, User user) {
public Post(String title, String content, Location location, Integer time, Integer price, Category category, List<Mood> moods, String imgUrl, User user) {
this.title = title;
this.content = content;
this.location = location;
this.time = time;
this.price = price;
this.category = category;
this.mood = mood;
this.moods = moods;
this.imgUrl = imgUrl;
this.user = user;
}

public void update(Location location, Category category, PostUpdateReqDto postUpdateReqDto, Mood mood, String imgUrl) {
public void update(Location location, Category category, PostUpdateReqDto postUpdateReqDto, List<Mood> moods, String imgUrl) {
this.title = postUpdateReqDto.title();
this.content = postUpdateReqDto.content();
this.location = location;
this.time = postUpdateReqDto.time();
this.price = postUpdateReqDto.price();
this.category = category;
this.mood = mood;
this.moods = moods;
this.imgUrl = imgUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import net.skhu.likelion12thteam03be.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;


@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
@Query("select p from Post p join fetch p.moods m where m.moodId = :moodId ")
List<Post> findByMoodId(Long moodId);
}

0 comments on commit 994eee1

Please sign in to comment.