model, controller added

parent b1d025a1
......@@ -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>
......@@ -48,7 +53,17 @@
<artifactId>spring-boot-starter-validation</artifactId>
</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>
......
1,noodles,250,5,12
1,noodles,250,5,12
1,noodles,250,5,12
1,noodles,250,5,12
\ No newline at end of file
......@@ -2,8 +2,10 @@ package com.qadeer.customer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class CustomerServiceApplication {
public static void main(String[] args) {
......
package com.qadeer.customer.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qadeer.customer.dto.ProductDto;
import com.qadeer.customer.model.Customer;
import com.qadeer.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService service;
@GetMapping("/hello")
public String hello() {
return "Hello";
}
@GetMapping("/buy/{productName}/{quantity}")
public ResponseEntity<List<ProductDto>> buy(@PathVariable("productName") String productName, @PathVariable("quantity") int productQuantity) {
List<ProductDto> productDtoList = service.buyProducts(productName, String.valueOf(productQuantity));
return new ResponseEntity<List<ProductDto>>(productDtoList, HttpStatus.OK);
}
}
package com.qadeer.customer.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;
}
package com.qadeer.customer.feign;
import com.qadeer.customer.dto.ProductDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@FeignClient(name = "inventory-api", url = "http://localhost:8083/inventory")
public interface FeignService {
@GetMapping("/product/{id}")
ProductDto getProductById(@PathVariable int id);
@GetMapping("/product/{productName}/{productQuantity}")
ResponseEntity<List<ProductDto>> buyProducts(@PathVariable("productName") String productName, @PathVariable("productQuantity") String productQuantity);
}
......@@ -15,5 +15,6 @@ public class Customer {
@Id
private int id;
private String name;
private int budget;
private String phoneNo;
private String creditCardNo;
}
package com.qadeer.customer.service;
import com.qadeer.customer.dto.ProductDto;
import com.qadeer.customer.feign.FeignService;
import com.qadeer.customer.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerService {
@Autowired
private CustomerRepository repository;
@Autowired
private FeignService feignService;
public List<ProductDto> buyProducts(String productName, String productQuantity) {
ResponseEntity<List<ProductDto>> productDto = feignService.buyProducts(productName, productQuantity);
return productDto.getBody();
}
}
product_name,product_price,product_quantity
Cheese,120,3
Cream,250,5
Shortbread,200,3
Sprite,230,5
Vinegar,400,3
package com.qadeer.inventory.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qadeer.inventory.model.Product;
import com.qadeer.inventory.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/inventory")
public class InventoryController {
@Autowired
private ProductService service;
@GetMapping("/hello")
public String hello() {
return "Hello";
}
@PostMapping("/restock")
public ResponseEntity<?> restock(MultipartFile file) throws IOException {
List< Product> productList = service.restock(file);
return new ResponseEntity<List<Product>>(productList, HttpStatus.OK);
}
@GetMapping("/product/{id}")
public ResponseEntity<?> getProductById(@PathVariable int id) {
Product product = service.getProductById(id);
return new ResponseEntity<Product>(product, HttpStatus.OK);
}
@GetMapping("/product/{productName}/{productQuantity}")
public ResponseEntity<?> buyProducts(@PathVariable("productName") String productName, @PathVariable("productQuantity") int productQuantity) {
List<Product> productList = service.buyProducts(productName, productQuantity);
return new ResponseEntity<List<Product>>(productList, HttpStatus.OK);
}
}
......@@ -5,6 +5,8 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
......@@ -13,10 +15,15 @@ import javax.persistence.Id;
@NoArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int price;
private int quantity;
private int sellerId;
public Product(String name, int price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
}
......@@ -2,6 +2,11 @@ package com.qadeer.inventory.repository;
import com.qadeer.inventory.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
List<Product> findByName(String productName);
}
package com.qadeer.inventory.service;
import com.qadeer.inventory.model.Product;
import com.qadeer.inventory.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public List<Product> getAllProducts() {
return repository.findAll();
}
public Product getProductById(int id) {
return repository.findById(id).get();
}
public void createProduct(Product product) {
repository.save(product);
}
public List<Product> restock(MultipartFile file) throws IOException {
InputStreamReader ir = new InputStreamReader(file.getInputStream());
BufferedReader br = new BufferedReader(ir);
String line = br.readLine();
line = br.readLine();
List<Product> productList = new ArrayList<>();
while (line != null) {
String[] content = line.split(",");
String name = content[0].trim();
int price = Integer.parseInt(content[1]);
int quantity = Integer.parseInt(content[2]);
Product product = new Product(name, price, quantity);
repository.save(product);
productList.add(product);
line = br.readLine();
}
return productList;
}
public List<Product> buyProducts(String productName, int productQuantity) {
List<Product> product = repository.findByName(productName);
return product;
}
}
product_name,product_quantity
Cheese,3
Cream,5
Shortbread,3
Sprite,5
Vinegar3
Mussels,2
Croissant,5
Beef,1
Bandage,2
Tea,5
Fond,5
Cake,5
Fudge,2
Wine,1
Spinach,2
Fish,1
Mushroom,5
Mustard,4
Food Colouring,5
Bread,3
\ No newline at end of file
......@@ -26,4 +26,5 @@ public class SellerController {
public Seller getProduct(@PathVariable int id) {
return sellerService.getProduct(id);
}
}
......@@ -17,9 +17,7 @@ import javax.validation.constraints.Pattern;
@NoArgsConstructor
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String address;
private int phoneNo;
private int productId;
}
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