Skip to content

Commit

Permalink
Merge pull request #96 from SanE-Seo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Changha-dev authored May 5, 2024
2 parents a6f6fcd + b7c7af4 commit 515744b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
import com.seoultech.sanEseo.like.application.service.AddLikeRequest;
import com.seoultech.sanEseo.like.application.service.GetLikeResponse;
import com.seoultech.sanEseo.like.application.service.LikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class LikeController {

private final LikeService likeService;

public LikeController(LikeService likeService) {
this.likeService = likeService;
}

@PostMapping("/posts/{postId}/likes")
public ResponseEntity<?> addLike(@LoginMember AuthMember member, @PathVariable Long postId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.seoultech.sanEseo.like.application.service;


import com.seoultech.sanEseo.image.GetImageResponse;
import com.seoultech.sanEseo.image.ImageService;
import com.seoultech.sanEseo.image.PostImage;
import com.seoultech.sanEseo.like.application.port.LikePort;
import com.seoultech.sanEseo.like.domain.Likes;
import com.seoultech.sanEseo.like.exception.DuplicateLikesException;
Expand All @@ -9,7 +12,12 @@
import com.seoultech.sanEseo.member.domain.Member;
import com.seoultech.sanEseo.post.application.port.PostPort;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import com.seoultech.sanEseo.post_district.application.service.PostDistrictService;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -18,12 +26,19 @@

@Service
@AllArgsConstructor

public class LikeService {

private final LikePort likePort;
private final MemberPort memberPort;
private final PostPort postPort;
private final ImageService imageService;
private @Lazy PostDistrictService postDistrictService;

@Autowired
public void setPostDistrictService(@Lazy PostDistrictService postDistrictService) {
this.postDistrictService = postDistrictService;
}


public void addLike(Long memberId, Long postId) {

Expand Down Expand Up @@ -75,9 +90,40 @@ public List<Post> findLikedPostsByMember(Long memberId) {
.collect(Collectors.toList());
}

public List<Post> filterPostsByCategory(List<Post> posts, int category) {
public List<GetPostDistrictResponse> filterPostsByCategory(List<Post> posts, int category) {
return posts.stream()
.filter(post -> post.getCategory().getValue() == category)
.map(post -> {
Member author = post.getMember();
List<PostImage> images = imageService.getPostImages(post.getId());
List<GetImageResponse> imageResponses = images.stream()
.map(image -> new GetImageResponse(image.getImageUrl()))
.collect(Collectors.toList());
int likes = getLikeCount(post.getId());

// Assuming a method that retrieves a string of district names for the post
String districts = postDistrictService.getDistrictsForPost(post.getId());



return new GetPostDistrictResponse(
post.getId(),
author.getId(),
author.getName(),
author.getProfile(),
imageResponses,
post.getTitle(),
post.getSubTitle(),
post.getTime(),
likes,
post.getDistance(),
post.getLevel(),
districts,
0.0,
0.0
);
})
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.seoultech.sanEseo.member.application.service.AuthService;
import com.seoultech.sanEseo.member.application.service.MemberService;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -55,7 +56,7 @@ public ResponseEntity<?> checkDuplicateName(@RequestParam String username) {
@GetMapping("/liked-posts/{category}")
public ResponseEntity<?> getLikedPosts(@LoginMember AuthMember authMember, @PathVariable int category) {
List<Post> posts = likesService.findLikedPostsByMember(authMember.getId());
List<Post> posts1 = likesService.filterPostsByCategory(posts, category);
List<GetPostDistrictResponse> posts1 = likesService.filterPostsByCategory(posts, category);
return ApiResponse.ok("좋아요한 게시글 조회 성공", posts1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.seoultech.sanEseo.post.application.service.PostService;
import com.seoultech.sanEseo.post.application.service.UpdatePostRequest;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -53,7 +54,7 @@ public ResponseEntity<?> deletePost(@LoginMember AuthMember authMember, @PathVar
@GetMapping("/by-district-prefix")
public ResponseEntity<?> getPostsByDistrictPrefix(@RequestParam String districtName) {
List<Post> posts = postService.findPostsByDistrictNameStart(districtName);
List<Post> filterPostsByCategory = likeService.filterPostsByCategory(posts, 1);
List<GetPostDistrictResponse> filterPostsByCategory = likeService.filterPostsByCategory(posts, 1);

return ApiResponse.ok("입력된 자치구를 포함하는 게시글 반환 완료", filterPostsByCategory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import com.seoultech.sanEseo.public_api.application.service.CoordinateService;
import com.seoultech.sanEseo.public_api.application.service.dto.GetCoordinateResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
Expand All @@ -23,26 +26,20 @@
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PostDistrictService {

private final DistrictPort districtPort;
private final PostDistrictPort postDistrictPort;
private final ImageService imageService;
private final LikeService likeService;
private @Lazy LikeService likeService;
private final CoordinateService coordinateService;
private final MemberService memberService;
private final PostPort postPort;

public PostDistrictService(DistrictPort districtPort, PostDistrictPort postDistrictPort, ImageService imageService, LikeService likeService, CoordinateService coordinateService, MemberService memberService, PostPort postPort) {
this.districtPort = districtPort;
this.postDistrictPort = postDistrictPort;
this.imageService = imageService;
@Autowired
public void setLikeService(@Lazy LikeService likeService) {
this.likeService = likeService;
this.coordinateService = coordinateService;
this.memberService = memberService;
this.postPort = postPort;
}


public List<GetPostDistrictResponse> getPostDistrict(Long districtId) {
List<PostDistrict> postDistricts = postDistrictPort.findByDistrictId(districtId);
// 해당 post의 image를 가져오기 위해 imageService를 사용
Expand Down Expand Up @@ -111,6 +108,10 @@ private List<GetPostDistrictResponse> getPostDistrictResponses(List<PostDistrict
}




public String getDistrictsForPost(Long id) {
List<PostDistrict> postDistricts = postDistrictPort.findByPostId(id);
return postDistricts.stream()
.map(postDistrict -> postDistrict.getDistrict().getName())
.collect(Collectors.joining(", "));
}
}

0 comments on commit 515744b

Please sign in to comment.