Add RetailManagementSystem app
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.project.code;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.project.code")
|
||||
public class CodeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CodeApplication.class, args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.project.code.Controller;
|
||||
|
||||
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
// 1. Set Up the Global Exception Handler Class:
|
||||
// - Annotate the class with `@RestControllerAdvice` to enable global exception handling for REST controllers.
|
||||
// - This allows the class to handle exceptions thrown in any of the controllers globally.
|
||||
|
||||
|
||||
// 2. Define the `handleJsonParseException` Method:
|
||||
// - Annotate with `@ExceptionHandler(HttpMessageNotReadableException.class)` to handle cases where the request body is not correctly formatted (e.g., invalid JSON).
|
||||
// - The `HttpMessageNotReadableException` typically occurs when the input data cannot be deserialized or is improperly formatted.
|
||||
// - Use `@ResponseStatus(HttpStatus.BAD_REQUEST)` to specify that the response status will be **400 Bad Request** when this exception is thrown.
|
||||
// - The method should return a `Map<String, Object>` with the following key:
|
||||
// - **`message`**: The error message should indicate that the input provided is invalid. The value should be `"Invalid input: The data provided is not valid."`.
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.project.code.Controller;
|
||||
|
||||
public class InventoryController {
|
||||
// 1. Set Up the Controller Class:
|
||||
// - Annotate the class with `@RestController` to indicate that this is a REST controller, which handles HTTP requests and responses.
|
||||
// - Use `@RequestMapping("/inventory")` to set the base URL path for all methods in this controller. All endpoints related to inventory will be prefixed with `/inventory`.
|
||||
|
||||
|
||||
// 2. Autowired Dependencies:
|
||||
// - Autowire necessary repositories and services:
|
||||
// - `ProductRepository` will be used to interact with product data (i.e., finding, updating products).
|
||||
// - `InventoryRepository` will handle CRUD operations related to the inventory.
|
||||
// - `ServiceClass` will help with the validation logic (e.g., validating product IDs and inventory data).
|
||||
|
||||
|
||||
// 3. Define the `updateInventory` Method:
|
||||
// - This method handles HTTP PUT requests to update inventory for a product.
|
||||
// - It takes a `CombinedRequest` (containing `Product` and `Inventory`) in the request body.
|
||||
// - The product ID is validated, and if valid, the inventory is updated in the database.
|
||||
// - If the inventory exists, update it and return a success message. If not, return a message indicating no data available.
|
||||
|
||||
|
||||
// 4. Define the `saveInventory` Method:
|
||||
// - This method handles HTTP POST requests to save a new inventory entry.
|
||||
// - It accepts an `Inventory` object in the request body.
|
||||
// - It first validates whether the inventory already exists. If it exists, it returns a message stating so. If it doesn’t exist, it saves the inventory and returns a success message.
|
||||
|
||||
|
||||
// 5. Define the `getAllProducts` Method:
|
||||
// - This method handles HTTP GET requests to retrieve products for a specific store.
|
||||
// - It uses the `storeId` as a path variable and fetches the list of products from the database for the given store.
|
||||
// - The products are returned in a `Map` with the key `"products"`.
|
||||
|
||||
|
||||
// 6. Define the `getProductName` Method:
|
||||
// - This method handles HTTP GET requests to filter products by category and name.
|
||||
// - If either the category or name is `"null"`, adjust the filtering logic accordingly.
|
||||
// - Return the filtered products in the response with the key `"product"`.
|
||||
|
||||
|
||||
// 7. Define the `searchProduct` Method:
|
||||
// - This method handles HTTP GET requests to search for products by name within a specific store.
|
||||
// - It uses `name` and `storeId` as parameters and searches for products that match the `name` in the specified store.
|
||||
// - The search results are returned in the response with the key `"product"`.
|
||||
|
||||
|
||||
// 8. Define the `removeProduct` Method:
|
||||
// - This method handles HTTP DELETE requests to delete a product by its ID.
|
||||
// - It first validates if the product exists. If it does, it deletes the product from the `ProductRepository` and also removes the related inventory entry from the `InventoryRepository`.
|
||||
// - Returns a success message with the key `"message"` indicating successful deletion.
|
||||
|
||||
|
||||
// 9. Define the `validateQuantity` Method:
|
||||
// - This method handles HTTP GET requests to validate if a specified quantity of a product is available in stock for a given store.
|
||||
// - It checks the inventory for the product in the specified store and compares it to the requested quantity.
|
||||
// - If sufficient stock is available, return `true`; otherwise, return `false`.
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.project.code.Controller;
|
||||
|
||||
public class ProductController {
|
||||
// 1. Set Up the Controller Class:
|
||||
// - Annotate the class with `@RestController` to designate it as a REST controller for handling HTTP requests.
|
||||
// - Map the class to the `/product` URL using `@RequestMapping("/product")`.
|
||||
|
||||
|
||||
// 2. Autowired Dependencies:
|
||||
// - Inject the following dependencies via `@Autowired`:
|
||||
// - `ProductRepository` for CRUD operations on products.
|
||||
// - `ServiceClass` for product validation and business logic.
|
||||
// - `InventoryRepository` for managing the inventory linked to products.
|
||||
|
||||
|
||||
// 3. Define the `addProduct` Method:
|
||||
// - Annotate with `@PostMapping` to handle POST requests for adding a new product.
|
||||
// - Accept `Product` object in the request body.
|
||||
// - Validate product existence using `validateProduct()` in `ServiceClass`.
|
||||
// - Save the valid product using `save()` method of `ProductRepository`.
|
||||
// - Catch exceptions (e.g., `DataIntegrityViolationException`) and return appropriate error message.
|
||||
|
||||
|
||||
// 4. Define the `getProductbyId` Method:
|
||||
// - Annotate with `@GetMapping("/product/{id}")` to handle GET requests for retrieving a product by ID.
|
||||
// - Accept product ID via `@PathVariable`.
|
||||
// - Use `findById(id)` method from `ProductRepository` to fetch the product.
|
||||
// - Return the product in a `Map<String, Object>` with key `products`.
|
||||
|
||||
|
||||
// 5. Define the `updateProduct` Method:
|
||||
// - Annotate with `@PutMapping` to handle PUT requests for updating an existing product.
|
||||
// - Accept updated `Product` object in the request body.
|
||||
// - Use `save()` method from `ProductRepository` to update the product.
|
||||
// - Return a success message with key `message` after updating the product.
|
||||
|
||||
|
||||
// 6. Define the `filterbyCategoryProduct` Method:
|
||||
// - Annotate with `@GetMapping("/category/{name}/{category}")` to handle GET requests for filtering products by `name` and `category`.
|
||||
// - Use conditional filtering logic if `name` or `category` is `"null"`.
|
||||
// - Fetch products based on category using methods like `findByCategory()` or `findProductBySubNameAndCategory()`.
|
||||
// - Return filtered products in a `Map<String, Object>` with key `products`.
|
||||
|
||||
|
||||
// 7. Define the `listProduct` Method:
|
||||
// - Annotate with `@GetMapping` to handle GET requests to fetch all products.
|
||||
// - Fetch all products using `findAll()` method from `ProductRepository`.
|
||||
// - Return all products in a `Map<String, Object>` with key `products`.
|
||||
|
||||
|
||||
// 8. Define the `getProductbyCategoryAndStoreId` Method:
|
||||
// - Annotate with `@GetMapping("filter/{category}/{storeid}")` to filter products by `category` and `storeId`.
|
||||
// - Use `findProductByCategory()` method from `ProductRepository` to retrieve products.
|
||||
// - Return filtered products in a `Map<String, Object>` with key `product`.
|
||||
|
||||
|
||||
// 9. Define the `deleteProduct` Method:
|
||||
// - Annotate with `@DeleteMapping("/{id}")` to handle DELETE requests for removing a product by its ID.
|
||||
// - Validate product existence using `ValidateProductId()` in `ServiceClass`.
|
||||
// - Remove product from `Inventory` first using `deleteByProductId(id)` in `InventoryRepository`.
|
||||
// - Remove product from `Product` using `deleteById(id)` in `ProductRepository`.
|
||||
// - Return a success message with key `message` indicating product deletion.
|
||||
|
||||
|
||||
// 10. Define the `searchProduct` Method:
|
||||
// - Annotate with `@GetMapping("/searchProduct/{name}")` to search for products by `name`.
|
||||
// - Use `findProductBySubName()` method from `ProductRepository` to search products by name.
|
||||
// - Return search results in a `Map<String, Object>` with key `products`.
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.project.code.Controller;
|
||||
|
||||
public class ReviewController {
|
||||
// 1. Set Up the Controller Class:
|
||||
// - Annotate the class with `@RestController` to designate it as a REST controller for handling HTTP requests.
|
||||
// - Map the class to the `/reviews` URL using `@RequestMapping("/reviews")`.
|
||||
|
||||
|
||||
// 2. Autowired Dependencies:
|
||||
// - Inject the following dependencies via `@Autowired`:
|
||||
// - `ReviewRepository` for accessing review data.
|
||||
// - `CustomerRepository` for retrieving customer details associated with reviews.
|
||||
|
||||
|
||||
// 3. Define the `getReviews` Method:
|
||||
// - Annotate with `@GetMapping("/{storeId}/{productId}")` to fetch reviews for a specific product in a store by `storeId` and `productId`.
|
||||
// - Accept `storeId` and `productId` via `@PathVariable`.
|
||||
// - Fetch reviews using `findByStoreIdAndProductId()` method from `ReviewRepository`.
|
||||
// - Filter reviews to include only `comment`, `rating`, and the `customerName` associated with the review.
|
||||
// - Use `findById(review.getCustomerId())` from `CustomerRepository` to get customer name.
|
||||
// - Return filtered reviews in a `Map<String, Object>` with key `reviews`.
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.project.code.Controller;
|
||||
|
||||
|
||||
public class StoreController {
|
||||
// 1. Set Up the Controller Class:
|
||||
// - Annotate the class with `@RestController` to designate it as a REST controller for handling HTTP requests.
|
||||
// - Map the class to the `/store` URL using `@RequestMapping("/store")`.
|
||||
|
||||
|
||||
// 2. Autowired Dependencies:
|
||||
// - Inject the following dependencies via `@Autowired`:
|
||||
// - `StoreRepository` for managing store data.
|
||||
// - `OrderService` for handling order-related functionality.
|
||||
|
||||
|
||||
// 3. Define the `addStore` Method:
|
||||
// - Annotate with `@PostMapping` to create an endpoint for adding a new store.
|
||||
// - Accept `Store` object in the request body.
|
||||
// - Return a success message in a `Map<String, String>` with the key `message` containing store creation confirmation.
|
||||
|
||||
|
||||
// 4. Define the `validateStore` Method:
|
||||
// - Annotate with `@GetMapping("validate/{storeId}")` to check if a store exists by its `storeId`.
|
||||
// - Return a **boolean** indicating if the store exists.
|
||||
|
||||
|
||||
// 5. Define the `placeOrder` Method:
|
||||
// - Annotate with `@PostMapping("/placeOrder")` to handle order placement.
|
||||
// - Accept `PlaceOrderRequestDTO` in the request body.
|
||||
// - Return a success message with key `message` if the order is successfully placed.
|
||||
// - Return an error message with key `Error` if there is an issue processing the order.
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
public class CombinedRequest {
|
||||
|
||||
private Product product;
|
||||
private Inventory inventory;
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
// Setter for product
|
||||
public void setProduct(Product product) {
|
||||
this.product = product;
|
||||
}
|
||||
|
||||
// Getter for store
|
||||
public Inventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
// Setter for store
|
||||
public void setInventory(Inventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class Customer {
|
||||
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private long
|
||||
// - It should be auto-incremented.
|
||||
// - Use @Id to mark it as the primary key and @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'name' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// Example: @NotNull(message = "Name cannot be null")
|
||||
|
||||
// 3. Add 'email' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// Example: @NotNull(message = "Email cannot be null")
|
||||
|
||||
// 4. Add 'phone' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// Example: @NotNull(message = "Phone cannot be null")
|
||||
|
||||
// 5. Add one-to-many relationship with orders:
|
||||
// - A customer can have multiple orders.
|
||||
// - Use the @OneToMany annotation to establish this relationship.
|
||||
// - Specify "mappedBy = 'customer'" to indicate that the 'customer' field in the 'Order' entity owns the relationship.
|
||||
// - Use @JsonManagedReference to ensure proper JSON serialization of related orders.
|
||||
|
||||
// Example: @OneToMany(mappedBy = "customer", fetch = FetchType.EAGER)
|
||||
// Example: @JsonManagedReference
|
||||
|
||||
// 6. Getters and Setters:
|
||||
// - For each field ('id', 'name', 'email', 'phone'), add getter and setter methods.
|
||||
// - Example: public Long getId(), public void setId(Long id)
|
||||
// - Example: public String getName(), public void setName(String name)
|
||||
// - Add getters and setters for 'email' and 'phone' fields as well.
|
||||
|
||||
// 7. Ensure to use proper annotations and validate constraints:
|
||||
// - Use @NotNull for fields that cannot be empty like 'name', 'email', and 'phone'.
|
||||
// - Make sure you add the correct annotations for entity mapping and relationship management like @Entity, @Id, @GeneratedValue, @OneToMany, and @JsonManagedReference.
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class Inventory {
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private long
|
||||
// - This field will represent the unique identifier for the inventory entry.
|
||||
// - Use @Id to mark it as the primary key.
|
||||
// - Use @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'product' field:
|
||||
// - Type: private Product
|
||||
// - This field will represent the product associated with the inventory entry.
|
||||
// - Use @ManyToOne to establish a many-to-one relationship with the Product entity.
|
||||
|
||||
// 3. Add 'store' field:
|
||||
// - Type: private Store
|
||||
// - This field will represent the store where the inventory is located.
|
||||
// - Use @ManyToOne to establish a many-to-one relationship with the Store entity.
|
||||
|
||||
// 4. Add 'stockLevel' field:
|
||||
// - Type: private Integer
|
||||
// - This field will represent the current stock level of the product at the store.
|
||||
|
||||
// 5. Add relationships:
|
||||
// - **Product Relationship**: Use @ManyToOne to link this inventory entry to a product.
|
||||
// - **Store Relationship**: Use @ManyToOne to link this inventory entry to a store.
|
||||
// - Use @JsonBackReference("inventory-product") to prevent circular references during JSON serialization for the product field.
|
||||
// - Use @JsonBackReference("inventory-store") to prevent circular references during JSON serialization for the store field.
|
||||
|
||||
// 6. Use @JoinColumn for foreign key associations:
|
||||
// - For the 'product' field, use @JoinColumn(name = "product_id") to specify the foreign key column.
|
||||
// - For the 'store' field, use @JoinColumn(name = "store_id") to specify the foreign key column.
|
||||
|
||||
// 7. Create a constructor:
|
||||
// - Add a constructor that takes a Product, Store, and Integer stockLevel to initialize the Inventory object.
|
||||
|
||||
// 8. Add @Entity annotation:
|
||||
// - Use @Entity above the class definition to mark it as a JPA entity that will be mapped to a database table.
|
||||
|
||||
// 9. Add Getters and Setters:
|
||||
// - Add getters and setters for 'id', 'product', 'store', and 'stockLevel' fields.
|
||||
// - Example: public Long getId(), public void setId(Long id)
|
||||
// - Example: public Product getProduct(), public void setProduct(Product product)
|
||||
// - Example: public Store getStore(), public void setStore(Store store)
|
||||
// - Example: public Integer getStockLevel(), public void setStockLevel(Integer stockLevel)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class OrderDetails {
|
||||
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private Long
|
||||
// - This field will be auto-incremented.
|
||||
// - Use @Id to mark it as the primary key.
|
||||
// - Use @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'customer' field:
|
||||
// - Type: private Customer
|
||||
// - This field refers to the customer who placed the order.
|
||||
// - Use @ManyToOne with @JoinColumn(name = "customer_id") to define the foreign key relationship.
|
||||
// - Apply @JsonManagedReference to handle bidirectional relationships and JSON serialization.
|
||||
|
||||
// 3. Add 'store' field:
|
||||
// - Type: private Store
|
||||
// - This field refers to the store from where the order was placed.
|
||||
// - Use @ManyToOne with @JoinColumn(name = "store_id") to define the foreign key relationship.
|
||||
// - Apply @JsonManagedReference to handle bidirectional relationships and JSON serialization.
|
||||
|
||||
// 4. Add 'totalPrice' field:
|
||||
// - Type: private Double
|
||||
// - This field represents the total price of the order.
|
||||
|
||||
// 5. Add 'date' field:
|
||||
// - Type: private LocalDateTime
|
||||
// - This field represents the date and time when the order was placed.
|
||||
|
||||
// 6. Add 'orderItems' field:
|
||||
// - Type: private List<OrderItem>
|
||||
// - This field represents the list of items in the order.
|
||||
// - Use @OneToMany(mappedBy = "order", fetch = FetchType.EAGER) to establish the one-to-many relationship with OrderItem.
|
||||
// - Apply @JsonManagedReference to prevent circular references during JSON serialization.
|
||||
|
||||
// 7. Add constructors:
|
||||
// - A no-argument constructor.
|
||||
// - A parameterized constructor that accepts Customer, Store, totalPrice, and date as parameters.
|
||||
|
||||
// 8. Add @Entity annotation:
|
||||
// - Use @Entity above the class name to mark it as a JPA entity.
|
||||
|
||||
// 9. Add Getters and Setters:
|
||||
// - Add getter and setter methods for all fields (id, customer, store, totalPrice, date, orderItems).
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
public class OrderItem {
|
||||
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private Long
|
||||
// - This field will be auto-incremented.
|
||||
// - Use @Id to mark it as the primary key.
|
||||
// - Use @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'order' field:
|
||||
// - Type: private OrderDetails
|
||||
// - This field refers to the order this item belongs to.
|
||||
// - Use @ManyToOne with @JoinColumn(name = "order_id") to define the foreign key relationship.
|
||||
// - Apply @JsonManagedReference to manage bidirectional relationships and JSON serialization.
|
||||
|
||||
// 3. Add 'product' field:
|
||||
// - Type: private Product
|
||||
// - This field refers to the product in the order.
|
||||
// - Use @ManyToOne with @JoinColumn(name = "product_id") to define the foreign key relationship.
|
||||
// - Apply @JsonManagedReference to prevent circular references during JSON serialization.
|
||||
|
||||
// 4. Add 'quantity' field:
|
||||
// - Type: private Integer
|
||||
// - This field represents the quantity of the product in the order.
|
||||
|
||||
// 5. Add 'price' field:
|
||||
// - Type: private Double
|
||||
// - This field represents the price of the product at the time of the order.
|
||||
|
||||
// 6. Add constructors:
|
||||
// - A no-argument constructor.
|
||||
// - A parameterized constructor that accepts OrderDetails, Product, quantity, and price as parameters.
|
||||
|
||||
// 7. Add @Entity annotation:
|
||||
// - Use @Entity above the class name to mark it as a JPA entity.
|
||||
|
||||
// 8. Add Getters and Setters:
|
||||
// - Add getter and setter methods for all fields (id, order, product, quantity, price).
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PlaceOrderRequestDTO {
|
||||
private Long storeId;
|
||||
private String customerName;
|
||||
private String customerEmail;
|
||||
private String customerPhone;
|
||||
private String datetime;
|
||||
private List<PurchaseProductDTO> purchaseProduct;
|
||||
private Double totalPrice;
|
||||
|
||||
// Getters and Setters
|
||||
|
||||
public Long getStoreId() {
|
||||
return storeId;
|
||||
}
|
||||
|
||||
public void setStoreId(Long storeId) {
|
||||
this.storeId = storeId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public String getCustomerEmail() {
|
||||
return customerEmail;
|
||||
}
|
||||
|
||||
public void setCustomerEmail(String customerEmail) {
|
||||
this.customerEmail = customerEmail;
|
||||
}
|
||||
|
||||
public String getCustomerPhone() {
|
||||
return customerPhone;
|
||||
}
|
||||
|
||||
public void setCustomerPhone(String customerPhone) {
|
||||
this.customerPhone = customerPhone;
|
||||
}
|
||||
|
||||
public String getDatetime() {
|
||||
return datetime;
|
||||
}
|
||||
|
||||
public void setDatetime(String datetime) {
|
||||
this.datetime = datetime;
|
||||
}
|
||||
|
||||
public List<PurchaseProductDTO> getPurchaseProduct() {
|
||||
return purchaseProduct;
|
||||
}
|
||||
|
||||
public void setPurchaseProduct(List<PurchaseProductDTO> purchaseProduct) {
|
||||
this.purchaseProduct = purchaseProduct;
|
||||
}
|
||||
|
||||
public Double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public void setTotalPrice(Double totalPrice) {
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class Product {
|
||||
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private long
|
||||
// - This field will be auto-incremented.
|
||||
// - Use @Id to mark it as the primary key.
|
||||
// - Use @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'name' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 3. Add 'category' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 4. Add 'price' field:
|
||||
// - Type: private Double
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 5. Add 'sku' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, must be unique, use the @NotNull annotation to enforce this rule.
|
||||
// - Use the @Table annotation with uniqueConstraints to ensure the 'sku' column is unique.
|
||||
|
||||
// Example: @Table(name = "product", uniqueConstraints = @UniqueConstraint(columnNames = "sku"))
|
||||
|
||||
// 6. Add relationships:
|
||||
// - **Inventory**: A product can have multiple inventory entries.
|
||||
// - Use @OneToMany(mappedBy = "product") to reflect the one-to-many relationship with Inventory.
|
||||
// - Use @JsonManagedReference("inventory-product") to manage bidirectional relationships and avoid circular references.
|
||||
|
||||
// 7. Add @Entity annotation:
|
||||
// - Use @Entity above the class name to mark it as a JPA entity.
|
||||
|
||||
// 8. Add Getters and Setters:
|
||||
// - Add getter and setter methods for all fields (id, name, category, price, sku).
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class PurchaseProductDTO {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Double price;
|
||||
private Integer quantity;
|
||||
private Double total;
|
||||
|
||||
// Getters and Setters
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
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 Integer getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(Integer quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public Double getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Double total) {
|
||||
this.total = total;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
public class Review {
|
||||
|
||||
// 1. Add 'customerId' field:
|
||||
// - Type: private Long
|
||||
// - This field represents the customer who created the review.
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 2. Add 'productId' field:
|
||||
// - Type: private Long
|
||||
// - This field represents the product being reviewed.
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 3. Add 'storeId' field:
|
||||
// - Type: private Long
|
||||
// - This field represents the store associated with the product.
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 4. Add 'rating' field:
|
||||
// - Type: private Integer
|
||||
// - This field represents the rating given to the product (out of 5).
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 5. Add 'comment' field:
|
||||
// - Type: private String
|
||||
// - This field represents an optional comment on the product.
|
||||
|
||||
// 6. Add validation:
|
||||
// - Apply @NotNull to the customerId, productId, storeId, and rating fields to ensure they are not null.
|
||||
// - Example: @NotNull(message = "Customer cannot be null")
|
||||
|
||||
// 7. Add @Document annotation:
|
||||
// - Use @Document(collection = "reviews") to indicate that this class represents a MongoDB document.
|
||||
|
||||
// 8. Add constructor:
|
||||
// - The constructor should accept values for customerId, productId, storeId, rating, and an optional comment.
|
||||
|
||||
// 9. Add @Id annotation:
|
||||
// - Use @Id to mark the field 'id' as the primary key for MongoDB.
|
||||
// - The 'id' field is of type String and will be automatically generated by MongoDB.
|
||||
|
||||
// 10. Add Getters and Setters:
|
||||
// - Add getter and setter methods for all fields (customerId, productId, storeId, rating, comment).
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.project.code.Model;
|
||||
|
||||
|
||||
public class Store {
|
||||
|
||||
// 1. Add 'id' field:
|
||||
// - Type: private long
|
||||
// - This field will be auto-incremented.
|
||||
// - Use @Id to mark it as the primary key.
|
||||
// - Use @GeneratedValue(strategy = GenerationType.IDENTITY) to auto-increment it.
|
||||
|
||||
// 2. Add 'name' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull annotation to enforce this rule.
|
||||
|
||||
// 3. Add 'address' field:
|
||||
// - Type: private String
|
||||
// - This field cannot be empty, use the @NotNull and @NotBlank annotations to enforce this rule.
|
||||
|
||||
// 4. Add relationships:
|
||||
// - **Inventory**: A store can have multiple inventory entries.
|
||||
// - Use @OneToMany(mappedBy = "store") to reflect the one-to-many relationship with Inventory.
|
||||
// - Use @JsonManagedReference("inventory-store") to manage bidirectional relationships and avoid circular references.
|
||||
|
||||
// 5. Add constructor:
|
||||
// - Create a constructor that accepts name and address as parameters to initialize the Store object.
|
||||
|
||||
// 6. Add @Entity annotation:
|
||||
// - Use @Entity above the class name to mark it as a JPA entity.
|
||||
|
||||
// 7. Add Getters and Setters:
|
||||
// - Add getter and setter methods for all fields (id, name, address).
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
|
||||
public interface CustomerRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<Customer, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface CustomerRepository extends JpaRepository<Customer, Long> {}
|
||||
|
||||
// 2. Add custom query methods:
|
||||
// - **findByEmail**:
|
||||
// - This method will allow you to find a customer by their email address.
|
||||
// - Return type: Customer
|
||||
// - Parameter: String email
|
||||
|
||||
// Example: public Customer findByEmail(String email);
|
||||
|
||||
// - **findById**:
|
||||
// - This method will allow you to find a customer by their ID.
|
||||
// - Return type: Customer
|
||||
// - Parameter: Long id
|
||||
|
||||
// Example: public Customer findById(Long id);
|
||||
|
||||
// 3. Add any additional methods you may need for custom queries:
|
||||
// - You can create other query methods as needed, like finding customers by name or phone number, etc.
|
||||
|
||||
// Example: public List<Customer> findByName(String name);
|
||||
|
||||
// 4. Add @Repository annotation:
|
||||
// - Mark the interface with @Repository to indicate that it's a Spring Data JPA repository.
|
||||
// - This annotation is optional if you extend JpaRepository, as Spring Data automatically registers the repository, but it's good practice to add it for clarity.
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
|
||||
public interface InventoryRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<Inventory, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface InventoryRepository extends JpaRepository<Inventory, Long> {}
|
||||
|
||||
// 2. Add custom query methods:
|
||||
// - **findByProductIdandStoreId**:
|
||||
// - This method will allow you to find an inventory record by its product ID and store ID.
|
||||
// - Return type: Inventory
|
||||
// - Parameters: Long productId, Long storeId
|
||||
|
||||
// Example: public Inventory findByProductIdandStoreId(Long productId, Long storeId);
|
||||
|
||||
// - **findByStore_Id**:
|
||||
// - This method will allow you to find a list of inventory records for a specific store.
|
||||
// - Return type: List<Inventory>
|
||||
// - Parameter: Long storeId
|
||||
|
||||
// Example: public List<Inventory> findByStore_Id(Long storeId);
|
||||
|
||||
// - **deleteByProductId**:
|
||||
// - This method will allow you to delete all inventory records related to a specific product ID.
|
||||
// - Return type: void
|
||||
// - Parameter: Long productId
|
||||
// - Use @Modifying and @Transactional annotations to ensure the database is modified correctly.
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
|
||||
public interface OrderDetailsRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<OrderDetails, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface OrderDetailsRepository extends JpaRepository<OrderDetails, Long> {}
|
||||
|
||||
// 2. Since no custom methods are required for this repository, the default CRUD operations (save, delete, update, findById, etc.) are available out of the box.
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
|
||||
|
||||
public interface OrderItemRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<OrderItem, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface OrderItemRepository extends JpaRepository<OrderItem, Long> {}
|
||||
|
||||
// 2. Since no custom methods are required for this repository, the default CRUD operations (save, delete, update, findById, etc.) are available out of the box.
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
|
||||
public interface ProductRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<Product, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface ProductRepository extends JpaRepository<Product, Long> {}
|
||||
|
||||
// 2. Add custom query methods:
|
||||
// - **findAll**:
|
||||
// - This method will retrieve all products.
|
||||
// - Return type: List<Product>
|
||||
|
||||
// Example: public List<Product> findAll();
|
||||
|
||||
// - **findByCategory**:
|
||||
// - This method will retrieve products by their category.
|
||||
// - Return type: List<Product>
|
||||
// - Parameter: String category
|
||||
|
||||
// Example: public List<Product> findByCategory(String category);
|
||||
|
||||
// - **findByPriceBetween**:
|
||||
// - This method will retrieve products within a price range.
|
||||
// - Return type: List<Product>
|
||||
// - Parameters: Double minPrice, Double maxPrice
|
||||
|
||||
// Example: public List<Product> findByPriceBetween(Double minPrice, Double maxPrice);
|
||||
|
||||
// - **findBySku**:
|
||||
// - This method will retrieve a product by its SKU.
|
||||
// - Return type: Product
|
||||
// - Parameter: String sku
|
||||
|
||||
// Example: public Product findBySku(String sku);
|
||||
|
||||
// - **findByName**:
|
||||
// - This method will retrieve a product by its name.
|
||||
// - Return type: Product
|
||||
// - Parameter: String name
|
||||
|
||||
// Example: public Product findByName(String name);
|
||||
|
||||
// - **findByNameLike**:
|
||||
// - This method will retrieve products by a name pattern for a specific store.
|
||||
// - Return type: List<Product>
|
||||
// - Parameters: Long storeId, String pname
|
||||
// - Use @Query annotation to write a custom query.
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
public interface ReviewRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend MongoRepository<Review, String> to inherit basic CRUD functionality for MongoDB operations.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface ReviewRepository extends MongoRepository<Review, String> {}
|
||||
|
||||
// 2. Add custom query methods:
|
||||
// - **findByStoreIdAndProductId**:
|
||||
// - This method will retrieve reviews for a specific product and store.
|
||||
// - Return type: List<Review>
|
||||
// - Parameters: Long storeId, Long productId
|
||||
|
||||
// Example: public List<Review> findByStoreIdAndProductId(Long storeId, Long productId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.project.code.Repo;
|
||||
|
||||
public interface StoreRepository {
|
||||
// 1. Add the repository interface:
|
||||
// - Extend JpaRepository<Store, Long> to inherit basic CRUD functionality.
|
||||
// - This allows the repository to perform operations like save, delete, update, and find without having to implement these methods manually.
|
||||
|
||||
// Example: public interface StoreRepository extends JpaRepository<Store, Long> {}
|
||||
|
||||
// 2. Add custom query methods:
|
||||
// - **findById**:
|
||||
// - This method will retrieve a store by its ID.
|
||||
// - Return type: Store
|
||||
// - Parameter: Long id
|
||||
|
||||
// Example: public Store findById(Long id);
|
||||
|
||||
// - **findBySubName**:
|
||||
// - This method will retrieve stores whose name contains a given substring.
|
||||
// - Return type: List<Store>
|
||||
// - Parameter: String pname
|
||||
// - Use @Query annotation to write a custom query.
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.project.code.Service;
|
||||
|
||||
|
||||
public class OrderService {
|
||||
// 1. **saveOrder Method**:
|
||||
// - Processes a customer's order, including saving the order details and associated items.
|
||||
// - Parameters: `PlaceOrderRequestDTO placeOrderRequest` (Request data for placing an order)
|
||||
// - Return Type: `void` (This method doesn't return anything, it just processes the order)
|
||||
|
||||
// 2. **Retrieve or Create the Customer**:
|
||||
// - Check if the customer exists by their email using `findByEmail`.
|
||||
// - If the customer exists, use the existing customer; otherwise, create and save a new customer using `customerRepository.save()`.
|
||||
|
||||
// 3. **Retrieve the Store**:
|
||||
// - Fetch the store by ID from `storeRepository`.
|
||||
// - If the store doesn't exist, throw an exception. Use `storeRepository.findById()`.
|
||||
|
||||
// 4. **Create OrderDetails**:
|
||||
// - Create a new `OrderDetails` object and set customer, store, total price, and the current timestamp.
|
||||
// - Set the order date using `java.time.LocalDateTime.now()` and save the order with `orderDetailsRepository.save()`.
|
||||
|
||||
// 5. **Create and Save OrderItems**:
|
||||
// - For each product purchased, find the corresponding inventory, update stock levels, and save the changes using `inventoryRepository.save()`.
|
||||
// - Create and save `OrderItem` for each product and associate it with the `OrderDetails` using `orderItemRepository.save()`.
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.project.code.Service;
|
||||
|
||||
|
||||
public class ServiceClass {
|
||||
|
||||
// 1. **validateInventory Method**:
|
||||
// - Checks if an inventory record exists for a given product and store combination.
|
||||
// - Parameters: `Inventory inventory`
|
||||
// - Return Type: `boolean` (Returns `false` if inventory exists, otherwise `true`)
|
||||
|
||||
// 2. **validateProduct Method**:
|
||||
// - Checks if a product exists by its name.
|
||||
// - Parameters: `Product product`
|
||||
// - Return Type: `boolean` (Returns `false` if a product with the same name exists, otherwise `true`)
|
||||
|
||||
// 3. **ValidateProductId Method**:
|
||||
// - Checks if a product exists by its ID.
|
||||
// - Parameters: `long id`
|
||||
// - Return Type: `boolean` (Returns `false` if the product does not exist with the given ID, otherwise `true`)
|
||||
|
||||
// 4. **getInventoryId Method**:
|
||||
// - Fetches the inventory record for a given product and store combination.
|
||||
// - Parameters: `Inventory inventory`
|
||||
// - Return Type: `Inventory` (Returns the inventory record for the product-store combination)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.project.code.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
// Allow CORS for all endpoints
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins("*") // Add your frontend URL here
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE") // Specify allowed methods
|
||||
.allowedHeaders("*"); // You can restrict headers if needed
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.application.name=code
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.project.code;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class CodeApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user