Skip to content

Commit

Permalink
Feat: 최근 조회 상품 디비에 저장
Browse files Browse the repository at this point in the history
Feat: 최근 조회 상품 디비에 저장
  • Loading branch information
tokyj515 authored Aug 3, 2023
2 parents 08cf162 + 3a732e1 commit 40aca37
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/example/neoul/controller/MyController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.neoul.controller;

import com.example.neoul.dto.board.BoardReq;
import com.example.neoul.dto.brand.BrandRes;
import com.example.neoul.dto.product.ProductRes;
import com.example.neoul.global.entity.ApiResponse;
Expand Down Expand Up @@ -78,4 +79,12 @@ public ApiResponse<List<ProductRes.ProductSimpleRes>> getUserRecentlyClickedProd
}


@ApiOperation(value = "내가 최근 조회한 상품 디비에 저장", notes = "내가 최근 조회한 상품 디비에 저장")
@PostMapping("/click/product")
public ApiResponse<String> createUserRecentlyClickedProduct(@RequestBody BoardReq.RecentlyClickedReq recentlyClickedReq){
productService.createUserRecentlyClickedProduct(recentlyClickedReq);
return new ApiResponse("최근 조회 상품이 등록되었습니다");
}


}
16 changes: 7 additions & 9 deletions src/main/java/com/example/neoul/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ public ApiResponse<User> now(){
}


@ApiOperation(value = "회원 정보 수정", notes = "회원 정보 수정")
@PatchMapping("/edit")
public ApiResponse<String> signup(@RequestBody UserReq.UserInfoEditReq userInfoEditReq) {
userService.userInfoEdit(userInfoEditReq);
return new ApiResponse<>("회원 정보 수정이 완료되었습니다");
}

@ApiOperation(value = "회원가입", notes = "회원가입")
@PostMapping("/signup")
public ApiResponse<UserRes.UserDetailDto> signup(@RequestBody UserReq.SignupUserDto signupUserDto) {
Expand All @@ -54,8 +47,6 @@ public ApiResponse<UserRes.UserDetailDto> signup(@RequestBody UserReq.SignupUser

//올바른 이름인지 확인



return new ApiResponse<>(userService.signup(signupUserDto));
}

Expand All @@ -67,6 +58,13 @@ public ApiResponse<TokenRes> signup(@RequestBody UserReq.LoginUserDto loginUserD
}


@ApiOperation(value = "회원 정보 수정", notes = "회원 정보 수정")
@PatchMapping("/edit")
public ApiResponse<String> signup(@RequestBody UserReq.UserInfoEditReq userInfoEditReq) {
userService.userInfoEdit(userInfoEditReq);
return new ApiResponse<>("회원 정보 수정이 완료되었습니다");
}


//
// @ApiOperation(value = "회원 탈퇴", notes = "회원 탈퇴")
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/example/neoul/dto/board/BoardReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.time.LocalDateTime;

public class BoardReq {


Expand All @@ -20,4 +22,18 @@ public static class RecruitQAReq{
private String social;
}


@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public static class RecentlyClickedReq{
private Long productId;
private LocalDateTime clickedAt;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
public interface RecentlyClickedRepository extends JpaRepository<RecentlyClicked, Long> {

List<RecentlyClicked> findAllByUserOrderByClickedAtDesc(User user);

boolean existsByUserAndProduct(User user, Product product);

boolean existsRecentlyClickedByUserAndProduct(User user, Product product);

Optional<RecentlyClicked> findByUserAndProduct(User user, Product product);
}
22 changes: 22 additions & 0 deletions src/main/java/com/example/neoul/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.neoul.service;

import com.example.neoul.dto.board.BoardReq;
import com.example.neoul.dto.product.ProductRes;
import com.example.neoul.entity.brand.Product;
import com.example.neoul.entity.brand.ProductImage;
Expand Down Expand Up @@ -159,4 +160,25 @@ public List<ProductRes.ProductSimpleRes> getUserRecentlyClickedProduct() {

return result;
}

public void createUserRecentlyClickedProduct(BoardReq.RecentlyClickedReq recentlyClickedReq) {
User user = userService.findNowLoginUser();
Product product = getProductByProductId(recentlyClickedReq.getProductId());

if(!recentlyClickedRepository.existsRecentlyClickedByUserAndProduct(user, product)){
RecentlyClicked recentlyClicked = RecentlyClicked.builder()
.user(user)
.product(product)
.clickedAt(recentlyClickedReq.getClickedAt())
.build();
recentlyClickedRepository.save(recentlyClicked);
} else {
RecentlyClicked recentlyClicked = recentlyClickedRepository.findByUserAndProduct(user, product).get();
recentlyClicked.setClickedAt(recentlyClickedReq.getClickedAt());
recentlyClickedRepository.save(recentlyClicked);
}



}
}

0 comments on commit 40aca37

Please sign in to comment.