Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/31 post view api #31 #33

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package com.teamjo.techeermarket.domain.mypage.controller;

import com.teamjo.techeermarket.domain.mypage.service.MyPageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RequiredArgsConstructor
//@RestController
//@RequestMapping("/api/mypage")
public class MyPageController {



}

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.teamjo.techeermarket.domain.products.entity.Products;
import com.teamjo.techeermarket.domain.users.entity.Users;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import javax.persistence.*;

Expand All @@ -14,6 +11,7 @@
@Getter
@AllArgsConstructor
@Entity
@Setter
@Builder
@NoArgsConstructor
@Table(name="user_product_like")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import com.teamjo.techeermarket.domain.products.entity.Products;
import com.teamjo.techeermarket.domain.users.entity.Users;
import com.teamjo.techeermarket.global.common.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import javax.persistence.*;

@Getter
@AllArgsConstructor
@Entity
@Builder
@Setter
@NoArgsConstructor
@Table(name="user_purchase")
public class UserPurchase extends BaseEntity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package com.teamjo.techeermarket.domain.mypage.mapper;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserPurchaseMapper {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.teamjo.techeermarket.domain.mypage.repository;

import com.teamjo.techeermarket.domain.mypage.entity.UserLike;
import com.teamjo.techeermarket.domain.products.entity.Products;
import com.teamjo.techeermarket.domain.users.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserLikeRepository extends JpaRepository<UserLike, Long> {
boolean existsByUsersAndProducts(Users users, Products products);

Optional<UserLike> findByUsersAndProducts(Users users, Products products);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.teamjo.techeermarket.domain.mypage.repository;

import com.teamjo.techeermarket.domain.mypage.entity.UserPurchase;
import com.teamjo.techeermarket.domain.products.entity.Products;
import com.teamjo.techeermarket.domain.users.entity.Users;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserPurchaseRepository extends JpaRepository<UserPurchase, Long> {
boolean existsBySellerIdAndProducts(Users sellerId, Products products);

}

Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package com.teamjo.techeermarket.domain.mypage.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

//@Service
//@RequiredArgsConstructor
public class MyPageService {






}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.teamjo.techeermarket.domain.products.controller;

import com.teamjo.techeermarket.domain.products.dto.request.ProductRequestDto;
import com.teamjo.techeermarket.domain.products.entity.ProductState;
import com.teamjo.techeermarket.domain.products.service.ProductService;
import com.teamjo.techeermarket.domain.products.service.ProductSubService;
import com.teamjo.techeermarket.global.config.UserDetailsImpl;
import com.teamjo.techeermarket.global.exception.product.InvalidProductStateException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

import static com.teamjo.techeermarket.global.exception.ErrorCode.INVALID_PRODUCT_STATE;


@RequiredArgsConstructor
Expand All @@ -21,6 +25,7 @@
public class ProductController {

private final ProductService productService;
private final ProductSubService productSubService;

@PostMapping
public HttpStatus saveProduct (@Validated @ModelAttribute ProductRequestDto productRequstDto,
Expand All @@ -29,8 +34,8 @@ public HttpStatus saveProduct (@Validated @ModelAttribute ProductRequestDto prod
// 상품 DB에 저장
Long productId = productService.saveProduct(productRequstDto, email);

// UserPurchase DB에 저장
// 상품 id + userid(seller-id) 만 저장
// UserPurchase DB에 => 상품 id + (seller-id) 만 저장
productSubService.updateProductSeller(email,productId);

// 상품 상세 조회로 redirect
// redirectAttributes.addAttribute("productId", productId);
Expand All @@ -40,5 +45,53 @@ public HttpStatus saveProduct (@Validated @ModelAttribute ProductRequestDto prod
}


/*
// 상품 게시물 상태 변경
*/
@PutMapping("/state/{productId}")
public ResponseEntity<String> updateProductState(
@PathVariable Long productId,
@RequestBody Map<String, String> requestBody,
@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) {
String email = userDetailsImpl.getUsername();
// 요청 바디에서 state 값을 가져옴
String state = requestBody.get("state");
// 상태가 올바른지 확인
try{
ProductState productState = ProductState.valueOf(state);
} catch (IllegalArgumentException e) {
throw new InvalidProductStateException() ; }

// 서비스를 통해 상태 변경
productSubService.updateProductState(productId, ProductState.valueOf(state),email);
return ResponseEntity.status(HttpStatus.OK).body("Product state updated successfully");
}



/*
// 게시물 좋아요 누르기
*/
@PostMapping("/like/{productId}")
public HttpStatus likeProduct (@PathVariable Long productId,
@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) throws IOException {
String email = userDetailsImpl.getUsername();
productSubService.likeProduct(email, productId);
return HttpStatus.OK;
}


/*
// 게시물 좋아요 취소 누르기
*/
@DeleteMapping("/like/{productId}")
public HttpStatus unlikeProduct (@PathVariable Long productId,
@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) throws IOException {
String email = userDetailsImpl.getUsername();
productSubService.unlikeProduct(email, productId);
return HttpStatus.OK;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ public class ProductService {
private final ProductImageRepository productImageRepository;


// 상품 저장 ( = 게시물 저장)
/*
// 상품 저장 ( = 게시물 저장)
*/
@Transactional
public Long saveProduct(ProductRequestDto request, String email) throws IOException {
Users findUsers = userRepository.findByEmail(email);
Users findUsers = userRepository.findUserByEmail(email)
.orElseThrow(UserNotFoundException::new);
Categorys findCategory = categoryRepository.findIdByName(request.getCategoryName());

Products products = productMapper.saveToEntity(request, findCategory);
Expand All @@ -71,12 +74,10 @@ public Long saveProduct(ProductRequestDto request, String email) throws IOExcept
ProductImage productImage = new ProductImage();
productImage.setProducts(products); // products가 Products 엔터티의 인스턴스여야 합니다.
productImage.setImageUrl(imageUrls.get(i));

// 이미지 이름 설정
productImage.setImageName(newProductID +"Image#" + (i+1));
// 이미지 번호 설정
productImage.setImageNum(i+1);

// ProductImage 저장
productImageRepository.save(productImage);
}
Expand All @@ -86,41 +87,24 @@ public Long saveProduct(ProductRequestDto request, String email) throws IOExcept





/*
// 상품 게시물 상세 조회
*/





// 게시물 전체 조회





// 썸네일 업데이트
// @Transactional
// public void updateThumbnail(List<ProductImage> images, Long productId) {
// Products product = productRepository.findById(productId).get();
// product.setThumbnail(images);
// }


// 게시물에서 이미지 저장
// @Transactional
// public List<ProductImage> getProductImage(List<MultipartFile> productImages, Products product){
// return productImages.stream()
// .map((image) -> {
// try {
// s3Service.uploadProductImageList(BucketDir.PRODUCT, imageFiles);
// return ProductImage.builder()
// .imageUrl(url)
// .fileName(s3Uploader.getFileName(url))
// .product(product)
// .build();
// return productImageService.save(productImageService.convert(image, product));
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// })
// .collect(Collectors.toList());
// }

// List<MultipartFile> imageFiles = request.getProductImages();
// List<String> imageUrls = s3Service.uploadProductImageList(BucketDir.PRODUCT, imageFiles);

}
Loading