-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/trothly/trothcam/controller/web/LikeProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package trothly.trothcam.controller.web; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
import trothly.trothcam.domain.member.Member; | ||
import trothly.trothcam.dto.web.LikeProductReqDto; | ||
import trothly.trothcam.dto.web.LikeResDto; | ||
import trothly.trothcam.exception.base.BaseResponse; | ||
import trothly.trothcam.exception.custom.BadRequestException; | ||
import trothly.trothcam.service.web.LikeProductService; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/like-product") | ||
public class LikeProductController { | ||
|
||
private final LikeProductService likeProductService; | ||
|
||
/* 좋아요 저장 */ | ||
@PostMapping("") | ||
public BaseResponse<LikeResDto> saveLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) { | ||
if(req.getProductId() == null) { | ||
throw new BadRequestException("존재하지 않는 상품 아이디 입니다."); | ||
} | ||
|
||
LikeResDto res = likeProductService.saveLike(req, member); | ||
return BaseResponse.onSuccess(res); | ||
} | ||
|
||
/* 좋아요 삭제 */ | ||
@DeleteMapping("") | ||
public BaseResponse<LikeResDto> deleteLike(@RequestBody LikeProductReqDto req, @AuthenticationPrincipal Member member) { | ||
if(req.getProductId() == null) { | ||
throw new BadRequestException("존재하지 않는 상품 아이디 입니다."); | ||
} | ||
|
||
LikeResDto res = likeProductService.deleteLike(req, member); | ||
return BaseResponse.onSuccess(res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/trothly/trothcam/domain/like/LikeProductRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package trothly.trothcam.domain.like; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface LikeProductRepository extends JpaRepository<LikeProduct, Long> { | ||
|
||
Optional<LikeProduct> findByProductIdAndMemberId(Long productId, Long memberId); | ||
|
||
Long countById(Long id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/trothly/trothcam/dto/web/LikeProductReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package trothly.trothcam.dto.web; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class LikeProductReqDto { | ||
private Long productId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package trothly.trothcam.dto.web; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class LikeResDto { | ||
private String message; | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/trothly/trothcam/service/web/LikeProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package trothly.trothcam.service.web; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import trothly.trothcam.domain.like.LikeProduct; | ||
import trothly.trothcam.domain.like.LikeProductRepository; | ||
import trothly.trothcam.domain.member.Member; | ||
import trothly.trothcam.domain.product.Product; | ||
import trothly.trothcam.domain.product.ProductRepository; | ||
import trothly.trothcam.dto.web.LikeProductReqDto; | ||
import trothly.trothcam.dto.web.LikeResDto; | ||
import trothly.trothcam.exception.base.BaseException; | ||
import trothly.trothcam.exception.custom.BadRequestException; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class LikeProductService { | ||
|
||
private final LikeProductRepository likeProductRepository; | ||
private final ProductRepository productRepository; | ||
|
||
// 좋아요 저장 | ||
public LikeResDto saveLike(LikeProductReqDto req, Member member) throws BaseException { | ||
Optional<LikeProduct> like = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()); | ||
|
||
if(like.isPresent()) { | ||
throw new BadRequestException("이미 좋아요를 누른 상품입니다."); | ||
} | ||
|
||
Product product = productRepository.findById(req.getProductId()).orElseThrow( | ||
() -> new BadRequestException("존재하지 않는 상품입니다.") | ||
); | ||
|
||
LikeProduct newLike = likeProductRepository.save(new LikeProduct(product, member)); | ||
|
||
return new LikeResDto("좋아요 성공"); | ||
} | ||
|
||
// 좋아요 삭제 | ||
public LikeResDto deleteLike(LikeProductReqDto req, Member member) throws BaseException { | ||
LikeProduct likeProduct = likeProductRepository.findByProductIdAndMemberId(req.getProductId(), member.getId()).orElseThrow( | ||
() -> new BadRequestException("좋아요를 누르지 않은 상품입니다.") | ||
); | ||
|
||
likeProductRepository.deleteById(likeProduct.getId()); | ||
|
||
return new LikeResDto("좋아요 해제 성공"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters