Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/mvnw text eol=lf
*.cmd text eol=crlf
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
3 changes: 3 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wrapperVersion=3.3.4
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip
52 changes: 52 additions & 0 deletions main/java/com/example/demo/Controller/CustomerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.demo.Controller;

import com.example.demo.Model.Customer;
import com.example.demo.Service.CustomerService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/customers")
public class CustomerController {
private final CustomerService customerService;

public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
private static final String Api_Key="123456";
public boolean isValidApiKey(String ApiKey){
return Api_Key.equals(ApiKey);
}
@PostMapping
public ResponseEntity<?> addNewCustomers(@RequestHeader("API-Key") String apikey, @Valid @RequestBody Customer customer){
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api key");
}
customerService.addCustomer(customer);
return ResponseEntity.status(HttpStatus.CREATED).body(customer);
}
@GetMapping
public ResponseEntity<?> GetAllCustomers(@RequestHeader("API-Key") String apikey){
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api key");
}
List<Customer> customers=customerService.getAllCustomers();
return ResponseEntity.ok(customers);
}
@GetMapping("/email")
public ResponseEntity<?>GetCustomerByEmail(@RequestHeader("API-Key") String apikey,@PathVariable String email){
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api Key ");
}
Optional<Customer> customer=customerService.getCustomersByEmail(email);
if(customer.isEmpty()){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Requested body is absent");
}
return ResponseEntity.ok(customer);
}
}
80 changes: 80 additions & 0 deletions main/java/com/example/demo/Controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.demo.Controller;

import com.example.demo.Model.Product;
import com.example.demo.Service.ProductService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tools.jackson.databind.deser.bean.CreatorCandidate;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;

public ProductController(ProductService productService) {
this.productService = productService;
}

private static final String Api_Key="123456";

private boolean isValidApiKey(String apiKey) {
return Api_Key.equals(apiKey);
}
@PostMapping
public ResponseEntity<?> createProduct(@RequestHeader("API-Key")String apikey, @Valid @RequestBody Product product )
{
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api key");
}
productService.addProduct(product);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}
@GetMapping
public ResponseEntity<?> getAllProducts(@RequestHeader("API-Key")String apikey){
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(("Invalid Api key"));

}
List<Product> products=productService.getAllProducts();
return ResponseEntity.ok(products);
}
@GetMapping("/{name}")
public ResponseEntity<Object> getProductsByName(@RequestHeader("API-Key") String apikey,@PathVariable String name){
if(!isValidApiKey(apikey)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api Key");
}
Optional<Product> product=productService.getProductByName(name);
if(product.isEmpty()){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Requested body was not found");
}
return ResponseEntity.ok(product);
}
@GetMapping("/category/{category}")
public ResponseEntity<?> getProductByCategory(@RequestHeader("API-Key") String api_Key,@PathVariable String category){
if(!isValidApiKey(api_Key)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid Api key");
}
List<Product> products=productService.getProductsByCategory(category);
if(products==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Requested body was not found");
}
return ResponseEntity.ok(products);
}
@GetMapping("/price")
public ResponseEntity<?> getProductByPriceRange(@RequestHeader("API-Key") String api_Key,@RequestParam double min,@RequestParam double max){
if(!isValidApiKey(api_Key)){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid api key");
}
List<Product> products=productService.getProductsByPriceRange(min,max);
if(products==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Requested body was not found");
}
return ResponseEntity.ok(products);
}
}

13 changes: 13 additions & 0 deletions main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
56 changes: 56 additions & 0 deletions main/java/com/example/demo/Model/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.demo.Model;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;

public class Customer {
@NotBlank(message = "name cannot be blank")
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Customer(String name, String address, int age, String email) {
this.name = name;
this.address = address;
this.age = age;
this.email = email;
}

@Email(message = "Inappropriate email text")
private String email;
@Min(value = 18,message = "Age cannot be less than 18")
private int age;
@NotBlank(message = "Address cannot be blank")
private String address;
}
57 changes: 57 additions & 0 deletions main/java/com/example/demo/Model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.demo.Model;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;

public class Product {
@NotBlank(message = "Name cannot be blank")
@Size(min=3,message = "Name's size cannot be more than 3")
private String name;

@Positive(message = "Price must be positive")
private double price;
@NotBlank(message = "Category cannot be blank")
private String Category;
@Positive(message = "Price must be positive")
private int quantity;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getCategory() {
return Category;
}

public void setCategory(String category) {
Category = category;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public Product(String name, double price, int quantity, String category) {
this.name = name;
this.price = price;
this.quantity = quantity;
Category = category;
}
}
24 changes: 24 additions & 0 deletions main/java/com/example/demo/Service/CustomerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.demo.Service;

import com.example.demo.Model.Customer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CustomerService {
private List<Customer> customers;

public List<Customer> addCustomer(Customer customer){
customers.add(customer);
return customers;
}
public List<Customer> getAllCustomers(){
return customers;
}
public Optional<Customer> getCustomersByEmail(String email){
return customers.stream().filter(p->p.getEmail().equalsIgnoreCase(email)).findFirst();
}
public boolean DeleteCustomer(String name){return customers.removeIf(p->p.getName().equalsIgnoreCase(name));}
}
49 changes: 49 additions & 0 deletions main/java/com/example/demo/Service/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.demo.Service;

import com.example.demo.Model.Product;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ProductService {
private List<Product> products=new ArrayList<>();

public List<Product> addProduct(Product product){
products.add(product);
return products;
}
public List<Product> getAllProducts(){
return products;
}
public Optional<Product> getProductByName(String name){
return products.stream().
filter(p-> p.getName().equalsIgnoreCase(name)).findFirst();

}
public boolean updateProduct(String name, Product updatedProduct) {
for (Product product : products) {
if (product.getName().equalsIgnoreCase(name)) {
product.setName(updatedProduct.getName());
product.setPrice(updatedProduct.getPrice());
product.setCategory(updatedProduct.getCategory());
product.setQuantity(updatedProduct.getQuantity());
return true;
}
}
return false;
}
public boolean deleteProduct(String name){
return products.removeIf(p->p.getName().equalsIgnoreCase(name));
}
public List<Product> getProductsByCategory(String category){
return products.stream().filter(p->p.getCategory().equalsIgnoreCase(category)).toList();
}
public List<Product> getProductsByPriceRange(double minPrice,double maxPrice){
return products.stream().filter(p->p.getPrice()>minPrice &&p.getPrice()<maxPrice).toList();
}


}
1 change: 1 addition & 0 deletions main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=demo
Loading