Skip to content

Commit

Permalink
Merge pull request #63 from taco-official/KL-166/유저-api-구현
Browse files Browse the repository at this point in the history
feat(KL-166): user related api
  • Loading branch information
ohhamma authored Sep 10, 2024
2 parents b3a6c43 + 8a47033 commit 398ee9e
Show file tree
Hide file tree
Showing 53 changed files with 634 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public List<CommentResponse> findCommentsByProductId(@PathVariable final Long pr
}

@PostMapping
@Operation(summary = "댓글 등록", description = "작성한 댓글을 저장합니다.")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "댓글 등록", description = "작성한 댓글을 저장합니다.")
public CommentResponse addComment(
@PathVariable final Long productId,
@RequestBody @Valid final CommentCreateUpdateRequest commentCreateRequestDto
Expand All @@ -50,8 +50,8 @@ public CommentResponse addComment(
}

@PutMapping("/{commentId}")
@Operation(summary = "댓글 수정", description = "작성한 댓글을 수정합니다.")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "댓글 수정", description = "작성한 댓글을 수정합니다.")
public CommentResponse updateComment(
@PathVariable final Long productId,
@PathVariable final Long commentId,
Expand All @@ -65,8 +65,8 @@ public CommentResponse updateComment(
}

@DeleteMapping("/{commentId}")
@Operation(summary = "댓글 삭제", description = "작성한 댓글을 삭제합니다.")
@ResponseStatus(HttpStatus.NO_CONTENT)
@Operation(summary = "댓글 삭제", description = "작성한 댓글을 삭제합니다.")
public ResponseEntity<Void> deleteComment(
@PathVariable final Long productId,
@PathVariable final Long commentId
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/taco/klkl/domain/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDateTime;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -11,11 +10,9 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import taco.klkl.domain.notification.domain.Notification;
import taco.klkl.domain.product.domain.Product;
import taco.klkl.domain.user.domain.User;

Expand All @@ -36,13 +33,6 @@ public class Comment {
@JoinColumn(name = "user_id", nullable = false)
private User user;

@OneToOne(
mappedBy = "comment",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Notification notifications;

@Column(
name = "content",
nullable = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private Comment createCommentEntity(
final CommentCreateUpdateRequest commentCreateUpdateRequest
) {
//TODO: getCurrentUser() 함수로 교채
final User user = userUtil.findTestUser();
final User user = userUtil.getCurrentUser();
final Product product = productUtil.findProductEntityById(productId);
return Comment.of(
product,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -29,45 +31,47 @@ public class ImageController {

private final ImageService imageService;

@PostMapping("/v1/users/me/upload-url")
@ResponseStatus(HttpStatus.CREATED)
@Operation(
summary = "유저 이미지 업로드 Presigned URL 생성",
description = "유저 이미지 업로드를 위한 Presigned URL를 생성합니다."
)
@PostMapping("/v1/users/me/upload-url")
public PresignedUrlResponse createUserImageUploadUrl(
@Valid @RequestBody final SingleImageUploadRequest request
) {
return imageService.createUserImageUploadUrl(request);
}

@PostMapping("/v1/products/{productId}/upload-url")
@ResponseStatus(HttpStatus.CREATED)
@Operation(
summary = "상품 이미지 업로드 Presigned URL 생성",
description = "상품 이미지 업로드를 위한 Presigned URL를 생성합니다."
)
@PostMapping("/v1/products/{productId}/upload-url")
public List<PresignedUrlResponse> createProductImageUploadUrls(
@PathVariable final Long productId,
@Valid @RequestBody final MultipleImagesUploadRequest request
) {
return imageService.createProductImageUploadUrls(productId, request);
}

@PostMapping("/v1/users/me/upload-complete")
@Operation(
summary = "유저 이미지 업로드 완료 처리",
description = "유저 이미지 업로드를 완료 처리합니다."
)
@PostMapping("/v1/users/me/upload-complete")
public SingleUploadCompleteResponse uploadCompleteUserImage(
@Valid @RequestBody final SingleImageUpdateRequest request
) {
return imageService.uploadCompleteUserImage(request);
}

@PostMapping("/v1/products/{productId}/upload-complete")
@Operation(
summary = "상품 이미지 업로드 완료 처리",
description = "상품 이미지 업로드를 완료 처리합니다."
)
@PostMapping("/v1/products/{productId}/upload-complete")
public MultipleUploadCompleteResponse uploadCompleteProductImages(
@PathVariable final Long productId,
@Valid @RequestBody final MultipleImagesUpdateRequest request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class ImageServiceImpl implements ImageService {
@Override
@Transactional
public PresignedUrlResponse createUserImageUploadUrl(final SingleImageUploadRequest uploadRequest) {
final User currentUser = userUtil.findCurrentUser();
final User currentUser = userUtil.getCurrentUser();
final FileExtension fileExtension = FileExtension.from(uploadRequest.fileExtension());
return createImageUploadUrl(ImageType.USER_IMAGE, currentUser.getId(), fileExtension);
}
Expand All @@ -80,7 +80,7 @@ public List<PresignedUrlResponse> createProductImageUploadUrls(
public SingleUploadCompleteResponse uploadCompleteUserImage(
final SingleImageUpdateRequest updateRequest
) {
final User currentUser = userUtil.findCurrentUser();
final User currentUser = userUtil.getCurrentUser();
expireOldImages(ImageType.USER_IMAGE, currentUser.getId());

Image updatedImage = imageUtil.findImageEntityByImageTypeAndId(ImageType.USER_IMAGE, updateRequest.imageId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package taco.klkl.domain.like.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -23,6 +25,7 @@ public class LikeController {
private final LikeService likeService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "좋아요 누르기", description = "상품에 좋아요를 누릅니다.")
public LikeResponse addLike(@PathVariable final Long productId) {
return likeService.createLike(productId);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/taco/klkl/domain/like/dao/LikeRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package taco.klkl.domain.like.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -9,8 +11,9 @@

@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {
Page<Like> findByUserId(final Long userId, final Pageable pageable);

void deleteByProductAndUser(Product product, User user);
void deleteByProductAndUser(final Product product, final User user);

boolean existsByProductAndUser(Product product, User user);
boolean existsByProductAndUser(final Product product, final User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private Product findProductById(final Long productId) {
}

private User findCurrentUser() {
return userUtil.findCurrentUser();
return userUtil.getCurrentUser();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import taco.klkl.domain.user.domain.User;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findAllByComment_Product_User(User user);
List<Notification> findByComment_Product_User(final User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,30 @@ public List<NotificationResponse> findAllNotifications() {
@Transactional
public NotificationUpdateResponse readAllNotifications() {
final User receiver = findReceiver();
final List<Notification> notifications = notificationRepository.findAllByComment_Product_User(receiver);
final List<Notification> notifications = notificationRepository.findByComment_Product_User(receiver);
notifications.forEach(Notification::read);
final Long notificationCount = notificationRepository.count();
return NotificationUpdateResponse.of(notificationCount);
final Long updatedCount = (long)notifications.size();
return NotificationUpdateResponse.of(updatedCount);
}

@Override
@Transactional
public NotificationUpdateResponse readNotificationById(final Long id) {
final Notification notification = notificationRepository.findById(id)
.orElseThrow(NotificationNotFoundException::new);
validateMyNotification(notification);
notification.read();
return NotificationUpdateResponse.of(1L);
}

@Override
@Transactional
public NotificationDeleteResponse deleteAllNotifications() {
final Long notificationCount = notificationRepository.count();
notificationRepository.deleteAll();
return NotificationDeleteResponse.of(notificationCount);
final User receiver = findReceiver();
final List<Notification> notifications = notificationRepository.findByComment_Product_User(receiver);
notificationRepository.deleteAll(notifications);
final Long deletedCount = (long)notifications.size();
return NotificationDeleteResponse.of(deletedCount);
}

@Override
Expand All @@ -92,6 +95,13 @@ public void createNotificationByComment(final Comment comment) {

// TODO: 토큰으로 유저 가져오는 방식으로 수정하기
private User findReceiver() {
return userUtil.findTestUser();
return userUtil.getCurrentUser();
}

private void validateMyNotification(final Notification notification) {
final User receiver = findReceiver();
if (!notification.getComment().getProduct().getUser().equals(receiver)) {
throw new NotificationNotFoundException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import taco.klkl.domain.oauth.dao.OauthRepository;
import taco.klkl.domain.oauth.domain.Oauth;
import taco.klkl.domain.oauth.dto.request.KakaoUserInfoRequest;
import taco.klkl.domain.user.domain.Gender;
import taco.klkl.domain.user.domain.User;
import taco.klkl.domain.user.dto.request.UserCreateRequest;
import taco.klkl.domain.user.dto.response.UserDetailResponse;
Expand Down Expand Up @@ -58,8 +57,6 @@ private User registerUser(final KakaoUserInfoRequest userInfoRequest) {
// TODO: 성별, 나이는 기본값으로 넣고 있습니다.
final UserCreateRequest userCreateRequest = UserCreateRequest.of(
name,
Gender.MALE.getValue(),
0,
""
);

Expand Down
Loading

0 comments on commit 398ee9e

Please sign in to comment.