Skip to content

Commit

Permalink
Merge pull request #67 from taco-official/KL-181/cors-관련-문제-해결
Browse files Browse the repository at this point in the history
fix(KL-181): cors 관련 문제 해결
  • Loading branch information
ohhamma authored Oct 4, 2024
2 parents 86546a8 + 40c077a commit da0cb41
Show file tree
Hide file tree
Showing 36 changed files with 306 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ImageController {

private final ImageService imageService;

@PostMapping("/v1/members/me/upload-url")
@PostMapping("/v1/me/upload-url")
@ResponseStatus(HttpStatus.CREATED)
@Operation(
summary = "회원 이미지 업로드 Presigned URL 생성",
Expand All @@ -55,7 +55,7 @@ public List<PresignedUrlResponse> createProductImageUploadUrls(
return imageService.createProductImageUploadUrls(productId, request);
}

@PostMapping("/v1/members/me/upload-complete")
@PostMapping("/v1/me/upload-complete")
@Operation(
summary = "회원 이미지 업로드 완료 처리",
description = "회원 이미지 업로드를 완료 처리합니다."
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/taco/klkl/domain/like/controller/LikeController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package taco.klkl.domain.like.controller;

import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,30 +17,35 @@
import lombok.extern.slf4j.Slf4j;
import taco.klkl.domain.like.dto.response.LikeResponse;
import taco.klkl.domain.like.service.LikeService;
import taco.klkl.domain.product.dto.response.ProductSimpleResponse;
import taco.klkl.global.common.constants.ProductConstants;
import taco.klkl.global.common.response.PagedResponse;

@Slf4j
@RestController
@RequestMapping("/v1/products/{productId}/likes")
@RequestMapping("/v1/likes")
@RequiredArgsConstructor
@Tag(name = "3. 좋아요", description = "좋아요 관련 API")
public class LikeController {

private final LikeService likeService;

@GetMapping
@Operation(summary = "특정 상품의 좋아요 여부 조회", description = "특정 상품의 좋아요 여부를 조회합니다.")
public LikeResponse getLike(@PathVariable final Long productId) {
return likeService.getLike(productId);
@Operation(summary = "종아요 목록 조회", description = "좋아요를 누른 상품 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMyLikes(
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) Pageable pageable
) {
return likeService.getLikes(pageable);
}

@PostMapping
@PostMapping("/products/{productId}")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "좋아요 누르기", description = "상품에 좋아요를 누릅니다.")
public LikeResponse addLike(@PathVariable final Long productId) {
return likeService.createLike(productId);
}

@DeleteMapping
@DeleteMapping("/products/{productId}")
@Operation(summary = "좋아요 취소", description = "상품에 누른 좋아요를 취소합니다.")
public LikeResponse removeLike(@PathVariable final Long productId) {
return likeService.deleteLike(productId);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/taco/klkl/domain/like/service/LikeService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package taco.klkl.domain.like.service;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import taco.klkl.domain.like.dto.response.LikeResponse;
import taco.klkl.domain.member.domain.Member;
import taco.klkl.domain.product.domain.Product;
import taco.klkl.domain.product.dto.response.ProductSimpleResponse;
import taco.klkl.global.common.response.PagedResponse;

@Service
public interface LikeService {

LikeResponse getLike(final Long productId);
PagedResponse<ProductSimpleResponse> getLikes(final Pageable pageable);

/**
* 상품에 좋아요를 누르는 경우
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/taco/klkl/domain/like/service/LikeServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package taco.klkl.domain.like.service;

import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -12,8 +14,11 @@
import taco.klkl.domain.like.dto.response.LikeResponse;
import taco.klkl.domain.member.domain.Member;
import taco.klkl.domain.product.domain.Product;
import taco.klkl.domain.product.dto.response.ProductSimpleResponse;
import taco.klkl.domain.product.service.ProductService;
import taco.klkl.global.common.response.PagedResponse;
import taco.klkl.global.util.MemberUtil;
import taco.klkl.global.util.PageableUtil;
import taco.klkl.global.util.ProductUtil;

@Slf4j
Expand All @@ -29,16 +34,16 @@ public class LikeServiceImpl implements LikeService {

private final MemberUtil memberUtil;
private final ProductUtil productUtil;
private final PageableUtil pageableUtil;

@Override
@Transactional(readOnly = true)
public LikeResponse getLike(final Long productId) {
final Product product = findProductById(productId);
public PagedResponse<ProductSimpleResponse> getLikes(Pageable pageable) {
final Pageable sortedPageable = pageableUtil.createPageableSortedByCreatedAtDesc(pageable);
final Member member = memberUtil.getCurrentMember();
if (likeRepository.existsByProductAndMember(product, member)) {
return LikeResponse.of(true, product.getLikeCount());
}
return LikeResponse.of(false, product.getLikeCount());
final Page<Like> likes = likeRepository.findByMemberId(member.getId(), sortedPageable);
final Page<Product> likedProducts = likes.map(Like::getProduct);
return PagedResponse.of(likedProducts, ProductSimpleResponse::from);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package taco.klkl.domain.member.controller;

import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
Expand All @@ -11,7 +12,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -34,27 +35,26 @@
@Slf4j
@RestController
@Tag(name = "1. 회원", description = "회원 관련 API")
@RequestMapping("/v1/members")
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;
private final MemberUtil memberUtil;

@GetMapping("/me")
@GetMapping("/v1/me")
@Operation(summary = "내 정보 조회", description = "내 정보를 조회합니다.")
public MemberDetailResponse getMe() {
final Member me = memberUtil.getCurrentMember();
return memberService.getMemberById(me.getId());
}

@GetMapping("/{memberId}")
@Operation(summary = "유저 정보 조회", description = "유저 정보를 조회합니다.")
public MemberDetailResponse getMember(@PathVariable final Long memberId) {
return memberService.getMemberById(memberId);
@PutMapping("/v1/me")
@Operation(summary = " 정보 수정", description = " 정보를 수정합니다.")
public MemberDetailResponse updateMe(@Valid @RequestBody final MemberUpdateRequest request) {
return memberService.updateMember(request);
}

@GetMapping("/me/products")
@GetMapping("/v1/me/products")
@Operation(summary = "내 상품 목록 조회", description = "내 상품 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMyProducts(
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) Pageable pageable
Expand All @@ -63,52 +63,52 @@ public PagedResponse<ProductSimpleResponse> getMyProducts(
return memberService.getMemberProductsById(me.getId(), pageable);
}

@GetMapping("/{memberId}/products")
@Operation(summary = "회원의 상품 목록 조회", description = "회원의 상품 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMemberProducts(
@PathVariable final Long memberId,
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) Pageable pageable
) {
return memberService.getMemberProductsById(memberId, pageable);
}

@GetMapping("/me/likes")
@Operation(summary = "내 종아요 목록 조회", description = "내 좋아요 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMyLikes(
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) Pageable pageable
) {
final Member me = memberUtil.getCurrentMember();
return memberService.getMemberLikesById(me.getId(), pageable);
}

@GetMapping("/me/following")
@GetMapping("/v1/me/following")
@Operation(summary = "내 팔로잉 목록 조회", description = "내 팔로잉 목록을 조회합니다.")
public List<MemberSimpleResponse> getMyFollowings() {
return memberService.getFollowings();
}

@GetMapping("/me/following/{memberId}")
@GetMapping("/v1/me/following/{memberId}")
@Operation(summary = "특정 회원의 팔로우 여부 조회", description = "특정 회원을 팔로우했는지 여부를 조회합니다.")
public FollowResponse getFollowingStatus(@PathVariable final Long memberId) {
return memberService.getFollowingStatus(memberId);
}

@PostMapping("/me/following/{memberId}")
@PostMapping("/v1/me/following/{memberId}")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "회원 팔로우", description = "회원을 팔로우합니다.")
public FollowResponse followMember(@PathVariable final Long memberId) {
return memberService.createFollow(memberId);
}

@DeleteMapping("/me/following/{memberId}")
@DeleteMapping("/v1/me/following/{memberId}")
@Operation(summary = "회원 언팔로우", description = "회원을 언팔로우합니다")
public FollowResponse unfollowMember(@PathVariable final Long memberId) {
return memberService.removeFollow(memberId);
}

@PutMapping("/me")
@Operation(summary = "내 정보 수정", description = "내 정보를 수정합니다.")
public MemberDetailResponse updateMe(@Valid @RequestBody final MemberUpdateRequest request) {
return memberService.updateMember(request);
@GetMapping("/v1/me/following/products")
@Operation(summary = "내 팔로잉의 상품 목록 조회", description = "내 팔로잉 유저들의 상품 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMyFollowingProducts(
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) final Pageable pageable,
@RequestParam(value = "member_id", required = false) final Set<Long> memberIds
) {
return memberService.getFollowingProducts(pageable, memberIds);
}

@GetMapping("/v1/members/{memberId}")
@Operation(summary = "유저 정보 조회", description = "유저 정보를 조회합니다.")
public MemberDetailResponse getMember(@PathVariable final Long memberId) {
return memberService.getMemberById(memberId);
}

@GetMapping("/v1/members/{memberId}/products")
@Operation(summary = "회원의 상품 목록 조회", description = "회원의 상품 목록을 조회합니다.")
public PagedResponse<ProductSimpleResponse> getMemberProducts(
@PathVariable final Long memberId,
@PageableDefault(size = ProductConstants.DEFAULT_PAGE_SIZE) Pageable pageable
) {
return memberService.getMemberProductsById(memberId, pageable);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package taco.klkl.domain.member.service;

import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -21,12 +22,12 @@ public interface MemberService {

PagedResponse<ProductSimpleResponse> getMemberProductsById(final Long id, final Pageable pageable);

PagedResponse<ProductSimpleResponse> getMemberLikesById(final Long id, final Pageable pageable);

List<MemberSimpleResponse> getFollowings();

FollowResponse getFollowingStatus(final Long followingId);

PagedResponse<ProductSimpleResponse> getFollowingProducts(final Pageable pageable, final Set<Long> memberIds);

FollowResponse createFollow(final Long followingId);

FollowResponse removeFollow(final Long followingId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package taco.klkl.domain.member.service;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import taco.klkl.domain.like.domain.Like;
import taco.klkl.domain.member.dao.FollowRepository;
import taco.klkl.domain.member.dao.MemberRepository;
import taco.klkl.domain.member.domain.Follow;
Expand All @@ -27,8 +27,8 @@
import taco.klkl.domain.product.domain.Product;
import taco.klkl.domain.product.dto.response.ProductSimpleResponse;
import taco.klkl.global.common.response.PagedResponse;
import taco.klkl.global.util.LikeUtil;
import taco.klkl.global.util.MemberUtil;
import taco.klkl.global.util.PageableUtil;
import taco.klkl.global.util.ProductUtil;

@Slf4j
Expand All @@ -43,7 +43,7 @@ public class MemberServiceImpl implements MemberService {

private final ProductUtil productUtil;
private final MemberUtil memberUtil;
private final LikeUtil likeUtil;
private final PageableUtil pageableUtil;

/**
* 임시 나의 정보 조회
Expand All @@ -59,20 +59,11 @@ public MemberDetailResponse getMemberById(final Long id) {
@Override
public PagedResponse<ProductSimpleResponse> getMemberProductsById(final Long id, final Pageable pageable) {
validateUser(id);
final Pageable sortedPageable = createPageableSortedByCreatedAtDesc(pageable);
final Pageable sortedPageable = pageableUtil.createPageableSortedByCreatedAtDesc(pageable);
final Page<Product> userProducts = productUtil.findProductsByMemberId(id, sortedPageable);
return PagedResponse.of(userProducts, ProductSimpleResponse::from);
}

@Override
public PagedResponse<ProductSimpleResponse> getMemberLikesById(final Long id, final Pageable pageable) {
validateUser(id);
final Pageable sortedPageable = createPageableSortedByCreatedAtDesc(pageable);
final Page<Like> likes = likeUtil.findLikesByMemberId(id, sortedPageable);
final Page<Product> likedProducts = likes.map(Like::getProduct);
return PagedResponse.of(likedProducts, ProductSimpleResponse::from);
}

@Override
public List<MemberSimpleResponse> getFollowings() {
final Member follower = memberUtil.getCurrentMember();
Expand All @@ -82,6 +73,20 @@ public List<MemberSimpleResponse> getFollowings() {
.toList();
}

@Override
public PagedResponse<ProductSimpleResponse> getFollowingProducts(
final Pageable pageable,
final Set<Long> memberIds
) {
final Pageable sortedPageable = pageableUtil.createPageableSortedByCreatedAtDesc(pageable);
final Set<Long> followingIds = Optional.ofNullable(memberIds)
.orElseGet(() -> getFollowings().stream()
.map(MemberSimpleResponse::id)
.collect(Collectors.toSet()));
final Page<Product> followingProducts = productUtil.findProductsByMemberIds(followingIds, sortedPageable);
return PagedResponse.of(followingProducts, ProductSimpleResponse::from);
}

@Override
public FollowResponse getFollowingStatus(final Long followingId) {
final Member follower = memberUtil.getCurrentMember();
Expand Down Expand Up @@ -150,14 +155,6 @@ private void updateMemberEntity(final Member member, final MemberUpdateRequest u
member.update(name, description);
}

private Pageable createPageableSortedByCreatedAtDesc(final Pageable pageable) {
return PageRequest.of(
pageable.getPageNumber(),
pageable.getPageSize(),
Sort.by(Sort.Direction.DESC, "createdAt")
);
}

private String generateUniqueTag(final String name) {
String tag;
do {
Expand Down
Loading

0 comments on commit da0cb41

Please sign in to comment.