orders logs, feign update in seller

parent 910f2f77
......@@ -28,4 +28,10 @@ public class CustomerController {
List<ProductDto> productDtoList = service.buyProducts(productName, String.valueOf(productQuantity));
return new ResponseEntity<List<ProductDto>>(productDtoList, HttpStatus.OK);
}
@GetMapping("/product/{id}")
public ResponseEntity<?> getProductById(@PathVariable int id) {
ProductDto productDto = service.getProductById(id);
return ResponseEntity.ok(productDto);
}
}
......@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@FeignClient(name = "inventory-api", url = "http://localhost:8083/inventory")
@FeignClient(name = "seller-api", url = "http://localhost:8082/seller")
public interface FeignService {
@GetMapping("/product/{id}")
......
......@@ -23,4 +23,9 @@ public class CustomerService {
ResponseEntity<List<ProductDto>> productDto = feignService.buyProducts(productName, productQuantity);
return productDto.getBody();
}
public ProductDto getProductById(int id) {
ProductDto productDto = feignService.getProductById(id);
return productDto;
}
}
......@@ -49,8 +49,39 @@ public class ProductService {
return productList;
}
public List<Product> buyProducts(String productName, int productQuantity) {
List<Product> product = repository.findByName(productName);
return product;
public List<Product> buyProducts(String productName, int requiredQuant) {
List<Product> productList = repository.findByName(productName);
List<Product> myProducts = new ArrayList<>();
int requiredProducts = 0;
for(Product product: productList) {
if (product.getQuantity() >= requiredQuant) {
// create copy of product object.
Product deliverProduct = new Product();
deliverProduct.setId(product.getId());
deliverProduct.setName(product.getName());
deliverProduct.setPrice(product.getPrice());
deliverProduct.setQuantity(requiredQuant);
myProducts.add(deliverProduct);
product.setQuantity(product.getQuantity() - requiredQuant);
repository.save(product);
}
else if (product.getQuantity() > 0 && product.getQuantity() < requiredQuant){
requiredProducts = requiredQuant - product.getQuantity();
// create copy of product object.
Product deliverProduct = new Product();
deliverProduct.setId(product.getId());
deliverProduct.setName(product.getName());
deliverProduct.setPrice(product.getPrice());
deliverProduct.setQuantity(product.getQuantity());
System.out.println(requiredProducts);
myProducts.add(deliverProduct);
product.setQuantity(0);
repository.save(product);
}
else {
myProducts.add(null);
}
}
return myProducts;
}
}
id=11, name='Cheese', price=120, quantity=0 28/Jul/2022 20:20:30
id=12, name='Cream', price=250, quantity=5 28/Jul/2022 20:20:31
id=13, name='Shortbread', price=200, quantity=0 28/Jul/2022 20:20:33
id=14, name='Sprite', price=230, quantity=5 28/Jul/2022 20:20:34
id=15, name='Vinegar', price=400, quantity=3 28/Jul/2022 20:20:34
......@@ -15,8 +15,13 @@
<description>Final project</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
......@@ -47,6 +52,18 @@
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
......
......@@ -2,8 +2,10 @@ package com.qadeer.seller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class SellerServiceApplication {
public static void main(String[] args) {
......
package com.qadeer.seller.controller;
import com.qadeer.seller.dto.ProductDto;
import com.qadeer.seller.model.Seller;
import com.qadeer.seller.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
......@@ -10,7 +12,7 @@ import org.springframework.web.bind.annotation.*;
public class SellerController {
@Autowired
private SellerService sellerService;
private SellerService service;
@GetMapping("/hello")
public String hello() {
......@@ -19,12 +21,18 @@ public class SellerController {
@PostMapping
public void createProduct(@RequestBody Seller seller) {
sellerService.saveProduct(seller);
service.saveProduct(seller);
}
@GetMapping("/{id}")
public Seller getProduct(@PathVariable int id) {
return sellerService.getProduct(id);
return service.getProduct(id);
}
@GetMapping("/product/{id}")
public ResponseEntity<?> getProductById(@PathVariable int id) {
ProductDto productDto = service.getProductById(id);
return ResponseEntity.ok(productDto);
}
}
package com.qadeer.seller.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductDto {
private int id;
private String name;
private int price;
private int quantity;
@Override
public String toString() {
return "id=" + id +
", name='" + name + '\'' +
", price=" + price +
", quantity=" + quantity;
}
}
package com.qadeer.seller.feign;
import com.qadeer.seller.dto.ProductDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "inventory-api", url = "http://localhost:8083/inventory")
public interface FeignService {
@GetMapping("/product/{id}")
ProductDto getProductById(@PathVariable int id);
}
package com.qadeer.seller.service;
import com.qadeer.seller.dto.ProductDto;
import com.qadeer.seller.feign.FeignService;
import com.qadeer.seller.model.Seller;
import com.qadeer.seller.repository.SellerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static java.awt.SystemColor.text;
@Service
public class SellerService {
@Autowired
private SellerRepository repository;
@Autowired
private FeignService feignService;
String path = "/Users/aqadeer/Desktop/java-training-project-final/seller-service/orders.csv";
FileWriter fw = new FileWriter(path);
public SellerService() throws IOException {
}
public void saveProduct(Seller seller){
repository.save(seller);
};
......@@ -18,4 +43,30 @@ public class SellerService {
public Seller getProduct(int id) {
return repository.findById(id).get();
}
public ProductDto getProductById(int id) {
ProductDto productDto = feignService.getProductById(id);
orderLogs(productDto);
return productDto;
}
public void orderLogs(ProductDto productDto) {
// String path = "/Users/aqadeer/Desktop/java-training-project-final/seller-service/orders.csv";
// BufferedWriter bw = null;
try {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String myTime = dtf.format(now);
String text = productDto.toString() + " " + myTime + "\n";
Files.write(Paths.get(path), text.getBytes(), StandardOpenOption.APPEND);
// bw = new BufferedWriter(fw);
// bw.append(productDto.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
finally {
// bw.close();
}
}
}
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