Skip to content

Commit

Permalink
Merge pull request #59 from SanE-Seo/changha
Browse files Browse the repository at this point in the history
feat: 좋아요 여부 API추가 (카테고리 api안되고있음)
  • Loading branch information
Changha-dev authored Apr 30, 2024
2 parents 417ef1c + d2e9b95 commit e38258d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public int countByPostId(Long postId) {
return likeRepository.countByPostId(postId);
}

@Override
public boolean existsByPostIdAndMemberId(Long postId, Long memberId) {
return likeRepository.existsByPostIdAndMemberId(postId, memberId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public ResponseEntity<?> getLikeCount(@PathVariable Long postId) {
return ApiResponse.ok("좋아요 수 조회 성공", new GetLikeResponse(postId, likeCount));
}

@GetMapping("/posts/{postId}/members/{memberId}/likes")
public ResponseEntity<?> hasMemberLikedPost(@PathVariable Long postId, @PathVariable Long memberId) {
boolean hasLiked = likeService.hasMemberLikedPost(memberId, postId);
return ApiResponse.ok("좋아요 여부 조회 성공", hasLiked);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface LikeRepository extends JpaRepository<Likes, Long> {
int countByPostId(Long postId);
void deleteByPostAndMember(Post post, Member member);

boolean existsByPostIdAndMemberId(Long postId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface LikePort {

int countByPostId(Long postId);

boolean existsByPostIdAndMemberId(Long postId, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public int getLikeCount(Long postId) {
return likePort.countByPostId(postId);
}

public boolean hasMemberLikedPost(Long memberId, Long postId) {
return likePort.existsByPostIdAndMemberId(memberId, postId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Component;

import java.util.List;
Expand Down Expand Up @@ -47,4 +50,10 @@ public List<PostDistrict> findAll() {
public List<PostDistrict> findByPostCategory(Category category) {
return postDistrictRepository.findByPostCategory(category);
}

// @Override
// public Slice<PostDistrict> findByCategory(Category category, Pageable pageable) {
// return postDistrictRepository.findByCategory(category, pageable);
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.application.service.PostDistrictService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -29,11 +32,14 @@ public ResponseEntity<?> getPostDistrict(@PathVariable Long districtId) {
}

//전체 게시글 조회
@GetMapping("/posts/{category}")
public ResponseEntity<?> getAllPostDistrictByCategory(@PathVariable int category) {
List<GetPostDistrictResponse> responses = postDistrictService.getAllPostDistrict(category);
return ApiResponse.ok("전체 게시글 목록 조회 성공", responses);
}
// @GetMapping("/posts")
// public ResponseEntity<?> getAllPostDistrictByCategory(@RequestParam(value = "page", defaultValue = "0") int page,
// @RequestParam(value = "size", defaultValue = "10") int size,
// @RequestParam(value = "category", required = true) int category) {
// Pageable pageable = PageRequest.of(page, size);
// Slice<GetPostDistrictResponse> responses = (Slice<GetPostDistrictResponse>) postDistrictService.getAllPostDistrict(pageable, category);
// return ApiResponse.ok("전체 게시글 목록 조회 성공", responses);
// }

//좋아요 순으로 정렬된 게시글 조회
@GetMapping("/posts/{category}/sorted-by-likes")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.seoultech.sanEseo.post_district.adapter;

import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -13,4 +16,7 @@ public interface PostDistrictRepository extends JpaRepository<PostDistrict, Long

List<PostDistrict> findByPostCategory(Category category);

// Slice<PostDistrict> findByCategory(Category category, Pageable pageable);


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;

Expand All @@ -19,4 +21,6 @@ public interface PostDistrictPort {
List<PostDistrict> findAll();

List<PostDistrict> findByPostCategory(Category category);

// Slice<PostDistrict> findByCategory(Category category, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.seoultech.sanEseo.post.domain.Post;
import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort;
import com.seoultech.sanEseo.post_district.domain.PostDistrict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -45,11 +47,13 @@ public List<GetPostDistrictResponse> getPostDistrict(Long districtId) {
return getPostDistrictResponses(postDistricts);
}

public List<GetPostDistrictResponse> getAllPostDistrict(int category) {
Category categoryEnum = Category.from(category);
List<PostDistrict> postDistricts = postDistrictPort.findByPostCategory(categoryEnum);
return getPostDistrictResponses(postDistricts);
}
// public List<GetPostDistrictResponse> getAllPostDistrict(Pageable pageable, int category) {
// Category categoryEnum = Category.from(category);
// Slice<PostDistrict> postDistricts = postDistrictPort.findByCategory(categoryEnum, pageable);
//
// return getPostDistrictResponses(postDistricts);
// }


public List<GetPostDistrictResponse> getPostByLikesSortedDesc(int category) {
Category categoryEnum = Category.from(category);
Expand Down Expand Up @@ -88,4 +92,26 @@ private List<GetPostDistrictResponse> getPostDistrictResponses(List<PostDistrict
}).collect(Collectors.toList());
return responses;
}

private List<GetPostDistrictResponse> getPostDistrictResponses(Slice<PostDistrict> postDistricts) {
List<GetPostDistrictResponse> responses = postDistricts.stream().map(postDistrict -> {
Post post = postDistrict.getPost();
List<PostImage> images = imageService.getPostImages(post.getId());
List<GetImageResponse> imageResponses = images.stream().map(image -> new GetImageResponse(image.getImageUrl())).collect(Collectors.toList());
int likeCount = likeService.getLikeCount(post.getId());
return new GetPostDistrictResponse(
post.getId(),
imageResponses,
post.getTitle(),
post.getSubTitle(),
post.getTime(),
likeCount, // 가정: Post 엔티티에 좋아요 수를 반환하는 getLikes() 메소드가 있음
post.getDistance(),
post.getLevel(),
postDistrict.getDistrict().getName()
);
}).collect(Collectors.toList());
return responses;
}

}

0 comments on commit e38258d

Please sign in to comment.