Skip to content

Commit

Permalink
Merge pull request #109 from jupyter471/weekly
Browse files Browse the repository at this point in the history
Weekly
  • Loading branch information
yooonwodyd authored Nov 14, 2024
2 parents 1db769e + 1ed2b83 commit 8879dfc
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.helpmeCookies.product.controller.docs.ProductApiDocs;
import com.helpmeCookies.product.dto.ProductImageResponse;
import com.helpmeCookies.product.dto.ProductPage;
import com.helpmeCookies.product.dto.ProductPage.Paging;
import com.helpmeCookies.product.dto.ProductRequest;
import com.helpmeCookies.product.dto.ProductResponse;
import com.helpmeCookies.product.entity.Product;
Expand Down Expand Up @@ -40,51 +41,47 @@ public class ProductController implements ProductApiDocs {
private final ReviewService reviewService;
private final ProductLikeService productLikeService;

@PostMapping("/successTest")
public ResponseEntity<ApiResponse<Void>> saveTest() {
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK));
}

@PostMapping
public ResponseEntity<Void> saveProduct(@RequestBody ProductRequest productRequest) {
public ResponseEntity<ApiResponse<Void>> saveProduct(@RequestBody ProductRequest productRequest) {
Product product = productService.save(productRequest);
productImageService.saveImages(product.getId(),productRequest.imageUrls());
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK));
}

@PostMapping("/images")
public ResponseEntity<ProductImageResponse> uploadImages(List<MultipartFile> files) {
public ResponseEntity<ApiResponse<ProductImageResponse>> uploadImages(List<MultipartFile> files) {
List<ImageUpload> responses = productImageService.uploadMultiFiles(files);
return ResponseEntity.ok(new ProductImageResponse(responses.stream().map(ImageUpload::photoUrl).toList()));
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK,new ProductImageResponse(responses.stream().map(ImageUpload::photoUrl).toList())));
}

@GetMapping("/{productId}")
public ResponseEntity<ProductResponse> getProductInfo(@PathVariable("productId") Long productId) {
public ResponseEntity<ApiResponse<ProductResponse>> getProductInfo(@PathVariable("productId") Long productId) {
Product product = productService.find(productId);
List<String> urls = productImageService.getImages(productId);
return ResponseEntity.ok(ProductResponse.from(product,urls));
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK,ProductResponse.from(product,urls)));
}

@PutMapping("/{productId}")
public ResponseEntity<Void> editProductInfo(@PathVariable("productId") Long productId,
public ResponseEntity<ApiResponse<Void>> editProductInfo(@PathVariable("productId") Long productId,
@RequestBody ProductRequest productRequest) {
productService.edit(productId, productRequest);
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK));
}

@PutMapping("/{productId}/images")
public ResponseEntity<Void> editImages(@PathVariable("productId") Long productId, List<MultipartFile> files) {
public ResponseEntity<ApiResponse<Void>> editImages(@PathVariable("productId") Long productId, List<MultipartFile> files) {
productImageService.editImages(productId, files);
List<String> images = productImageService.uploadMultiFiles(files).stream()
.map(ImageUpload::photoUrl).toList();
productService.editThumbnailImage(productId,images);
productImageService.saveImages(productId,images);
return ResponseEntity.ok().build();
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK));
}

@DeleteMapping("/{productId}")
public ResponseEntity<Void> deleteProduct(@PathVariable("productId") Long productId) {
public ResponseEntity<ApiResponse<Void>> deleteProduct(@PathVariable("productId") Long productId) {
productService.delete(productId);
return ResponseEntity.noContent().build();
return ResponseEntity.ok(ApiResponse.success(SuccessCode.NO_CONTENT));
}

@GetMapping
Expand All @@ -105,6 +102,7 @@ public ResponseEntity<ApiResponse<ProductPage.Paging>> getProductsWithRandomPagi
@RequestParam(name = "size", required = false, defaultValue = "20") int size
) {
Pageable pageable = PageRequest.of(0, size);

return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK, productService.getProductsWithRandomPaging(pageable)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.helpmeCookies.product.controller;

import com.helpmeCookies.global.ApiResponse.ApiResponse;
import com.helpmeCookies.global.ApiResponse.SuccessCode;
import com.helpmeCookies.global.jwt.JwtUser;
import com.helpmeCookies.product.dto.ProductPage.Paging;
import com.helpmeCookies.product.service.ProductLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/wishes")
@RequiredArgsConstructor
public class WishController {
private ProductLikeService productLikeService;

@GetMapping
public ResponseEntity<ApiResponse<Paging>> getAllMyLikeProducts(
@AuthenticationPrincipal JwtUser jwtUser, Pageable pageable) {
return ResponseEntity.ok(ApiResponse.success(SuccessCode.OK,productLikeService.getLikeProducts(jwtUser.getId(),pageable)));
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/helpmeCookies/product/dto/ProductPage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.helpmeCookies.product.dto;

import com.helpmeCookies.product.entity.Product;
import com.helpmeCookies.product.repository.dto.ProductSearch;
import java.util.List;
import org.springframework.data.domain.Page;
Expand All @@ -23,6 +24,16 @@ public static Info from(ProductSearch productSearch) {
);
}

public static Info fromProduct(Product product) {
return new Info(
product.getId(),
product.getName(),
product.getArtistInfo().getNickname(),
product.getPrice(),
product.getThumbnailUrl()
);
}

public static List<Info> of(List<ProductSearch> content) {
return content.stream()
.map(Info::from)
Expand All @@ -41,6 +52,13 @@ public static Paging from(Page<ProductSearch> productPage) {
Info.of(productPage.getContent())
);
}

public static Paging fromProduct(Page<Product> productPage) {
return new Paging(
productPage.hasNext(),
productPage.map(Info::fromProduct).toList()
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record ProductRequest(
List<String> imageUrls

) {
public Product toEntity(ArtistInfo artistInfo) {
public Product toEntity(ArtistInfo artistInfo,String thumbnailImage) {
return Product.builder()
.name(name)
.category(Category.fromString(category))
Expand All @@ -29,6 +29,11 @@ public Product toEntity(ArtistInfo artistInfo) {
.preferredLocation(preferredLocation)
.hashTags(hashTags)
.artistInfo(artistInfo)
.thumbnailUrl(thumbnailImage)
.build();
}

public String getThumbnailImage() {
return imageUrls.get(0);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/helpmeCookies/product/entity/Like.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public Like(User user, Product product) {
this.user = user;
this.product = product;
}

public Product getProduct() {
return product;
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/helpmeCookies/product/entity/OrderStatus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.helpmeCookies.product.entity;

public enum OrderStatus {
ORDER, CANCEL
ORDER("핀매 중"),
DONE("거래 완료"),
RESERVED("예약 중");

private final String orderStatusType;

OrderStatus(String orderStatusType) {
this.orderStatusType = orderStatusType;
}

public String getOrderStatusType() {
return orderStatusType;
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/helpmeCookies/product/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ public class Product extends BaseTimeEntity {
public Product() {}

@Builder
public Product(String name, Category category, String size, Long price, String description, String preferredLocation, List<HashTag> hashTags, ArtistInfo artistInfo) {
public Product(String name, Category category, String size, Long price, String description, String preferredLocation, List<HashTag> hashTags, ArtistInfo artistInfo,String thumbnailUrl) {
this.name = name;
this.category = category;
this.size = size;
this.price = price;
this.description = description;
this.preferredLocation = preferredLocation;
this.hashTags = hashTags;
this.thumbnailUrl = thumbnailUrl;
this.artistInfo = artistInfo;
}

Expand Down Expand Up @@ -105,6 +106,10 @@ public ArtistInfo getArtistInfo() {
return artistInfo;
}

public String getThumbnailUrl() {
return thumbnailUrl;
}

public void update(String name, Category category, String size, Long price, String description, String preferredLocation, List<HashTag> hashTags, ArtistInfo artistInfo) {
this.name = name;
this.category = category;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.helpmeCookies.product.service;

import com.helpmeCookies.product.dto.ProductPage;
import com.helpmeCookies.product.entity.Like;
import com.helpmeCookies.product.entity.Product;
import com.helpmeCookies.product.repository.ProductLikeRepository;
import com.helpmeCookies.product.repository.ProductRepository;
import com.helpmeCookies.user.entity.User;
import com.helpmeCookies.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,18 +22,29 @@ public class ProductLikeService {

@Transactional
public void productLike(Long userId, Long productId) {
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 유저Id입니다." + userId));
User user = getUser(userId);
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 상품Id입니다." + productId));
Like like = new Like(user, product);
productLikeRepository.save(like);
}

@Transactional(readOnly = true)
public ProductPage.Paging getLikeProducts(Long userId, Pageable pageable) {
User user = getUser(userId);
Page<Product> productPage = productLikeRepository.findAllByUser(user,pageable).map(Like::getProduct);
return ProductPage.Paging.fromProduct(productPage);
}

@Transactional
public void deleteProductLike(Long userId, Long productId) {
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 유저Id입니다." + userId));
User user = getUser(userId);
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 상품Id입니다." + productId));

Like like = productLikeRepository.findDistinctFirstByUserAndProduct(user,product).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 상품 찜 항목입니다."));
productLikeRepository.delete(like);
}

private User getUser(Long userId) {
return userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 유저Id입니다." + userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.helpmeCookies.user.entity.ArtistInfo;
import com.helpmeCookies.user.repository.ArtistInfoRepository;
import com.helpmeCookies.product.dto.ProductPage;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ProductPage.Paging getProductsWithRandomPaging(Pageable pageable) {
public Product save(ProductRequest productSaveRequest) {
ArtistInfo artistInfo = artistInfoRepository.findById(productSaveRequest.artistInfoId())
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 작가 정보입니다."));
Product product = productSaveRequest.toEntity(artistInfo);
Product product = productSaveRequest.toEntity(artistInfo,productSaveRequest.getThumbnailImage());
productRepository.save(product);
return product;
}
Expand All @@ -70,6 +71,12 @@ public void edit(Long productId, ProductRequest productRequest) {
artistInfo);
}

@Transactional
public void editThumbnailImage(Long productId, List<String> images) {
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 id입니다"));
product.updateThumbnail(images.getFirst());
}

public void delete(Long productId) {
Product product = productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("유효하지 않은 id입니다"));
productRepository.delete(product);
Expand Down

0 comments on commit 8879dfc

Please sign in to comment.