Skip to content

Commit

Permalink
#243 - Update create and update product apis
Browse files Browse the repository at this point in the history
  • Loading branch information
khoahd7621 committed Mar 15, 2023
1 parent bc9221f commit 2dd988b
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import jakarta.validation.Valid;
import java.util.List;

@Validated
@RestController
public class ProductController {
private final ProductService productService;
Expand All @@ -35,16 +36,13 @@ public ResponseEntity<ProductListGetVm> listProducts(
}


@PostMapping(path = "/backoffice/products", consumes = {MediaType.APPLICATION_JSON_VALUE,
MediaType.MULTIPART_FORM_DATA_VALUE})
@PostMapping(path = "/backoffice/products", consumes = {MediaType.APPLICATION_JSON_VALUE})
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = ProductGetDetailVm.class))),
@ApiResponse(responseCode = "400", description = "Bad request", content = @Content(schema = @Schema(implementation = ErrorVm.class)))})
public ResponseEntity<ProductGetDetailVm> createProduct(@RequestPart("productDetails") ProductPostVm productPostVm,
@RequestPart(value = "files", required = false) List<MultipartFile> files, UriComponentsBuilder uriComponentsBuilder) {
ProductGetDetailVm productGetDetailVm = productService.createProduct(productPostVm, files);
return ResponseEntity.created(uriComponentsBuilder.replacePath("/products/{id}").buildAndExpand(productGetDetailVm.id()).toUri())
.body(productGetDetailVm);
public ResponseEntity<ProductGetDetailVm> createProduct(@Valid @RequestBody ProductPostVm productPostVm) {
ProductGetDetailVm productGetDetailVm = productService.createProduct(productPostVm);
return new ResponseEntity<>(productGetDetailVm, HttpStatus.CREATED);
}

@PutMapping(path = "/backoffice/products/{id}")
Expand Down
11 changes: 4 additions & 7 deletions product/src/main/java/com/yas/product/model/ProductCategory.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.yas.product.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "product_category")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductCategory {
Expand All @@ -19,11 +16,11 @@ public class ProductCategory {
private Long id;

@ManyToOne
@JoinColumn(name="product_id", nullable=false)
@JoinColumn(name = "product_id", nullable = false)
private Product product;

@ManyToOne
@JoinColumn(name="category_id", nullable=false)
@JoinColumn(name = "category_id", nullable = false)
private Category category;

private int displayOrder;
Expand Down
16 changes: 5 additions & 11 deletions product/src/main/java/com/yas/product/model/ProductImage.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package com.yas.product.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import lombok.Getter;
import lombok.Setter;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Setter
@Builder
@Table(name = "product_image")
@NoArgsConstructor
@AllArgsConstructor
public class ProductImage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.yas.product.repository;

import com.yas.product.model.ProductImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.yas.product.model.ProductImage;
import java.util.List;

@Repository
public interface ProductImageRepository extends JpaRepository<ProductImage, Long>{

public interface ProductImageRepository extends JpaRepository<ProductImage, Long> {
void deleteByImageIdInAndProductId(List<Long> imageIds, Long productId);
}

0 comments on commit 2dd988b

Please sign in to comment.