Commit 74b7c1c7 authored by Ben Anderson's avatar Ben Anderson

Created methods in service and controller for posting new product,...

Created methods in service and controller for posting new product, restructured component packages so they are contianed in the main package
parent 1270fc1d
......@@ -6,7 +6,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/> <!-- lookup parent from com.nisum.ascend.inventory.repository -->
</parent>
<groupId>com.nisum.ascend</groupId>
<artifactId>inventory</artifactId>
......
package com.nisum.ascend.inventory.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer {
}
package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.service.ProductService;
@RestController
@RequestMapping("/api/products")
public class ProductController {
@Autowired
ProductService productService;
@PostMapping("")
public ResponseEntity<Mono<ProductDto>> postProduct(@RequestBody Product product) {
Mono<ProductDto> newProduct = productService.createProduct(product);
return new ResponseEntity<>(newProduct, HttpStatus.ACCEPTED);
}
}
package dto;
package com.nisum.ascend.inventory.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import model.Product;
import com.nisum.ascend.inventory.model.Product;
@Getter
@AllArgsConstructor
......@@ -20,7 +20,7 @@ public class ProductDto {
public static ProductDto generateDtoFromProduct(Product product) {
// TODO: Fetch the product's promotion from the Promotions service
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
PromotionDto promotion = new PromotionDto();
return new ProductDto(product.getSku(), product.getUpc(), product.getProdName(), product.getProdDesc(), product.getPrice(), product.getProdImgUrl(), product.getBrand(), product.getCategory(), product.getAvailableStock(), promotion);
......
package dto;
package com.nisum.ascend.inventory.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
......
package model;
package com.nisum.ascend.inventory.model;
import lombok.Getter;
import lombok.Setter;
......@@ -24,6 +24,8 @@ public class Product {
private String brand;
private String category;
public Product() {}
public Product(String sku, String upc, String prodName, String prodDesc, float price, int availableStock,
String prodImgUrl, String brand, String category) {
this.sku = sku;
......
package repository;
package com.nisum.ascend.inventory.repository;
import model.Product;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
......
package service;
package com.nisum.ascend.inventory.service;
import dto.ProductDto;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import repository.ProductRepository;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.repository.ProductRepository;
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public Flux<ProductDto> testDto() {
return productRepository.findAll()
public Mono<ProductDto> createProduct(Product product) {
return productRepository.save(product)
.map(ProductDto::generateDtoFromProduct)
.flatMap(Flux::just);
.doOnError(System.out::println);
}
}
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import service.ProductService;
@RestController
public class ProductController {
@Autowired
ProductService productService;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment