From 6fa837d6edb8b26bbd342ff7324d996153178804 Mon Sep 17 00:00:00 2001 From: dharshib Date: Wed, 3 Jul 2024 07:59:12 +0530 Subject: [PATCH] add crud operations --- pom.xml | 5 +- .../controllers/InventoryController.java | 42 +++++ .../springboot_cicd/dtos/ApiResponseDto.java | 12 ++ .../dtos/InventoryRequestDto.java | 18 ++ .../springboot_cicd/modals/Inventory.java | 25 +++ .../modals/InventoryStatus.java | 6 + .../repositories/InventoryRepository.java | 11 ++ .../services/InventoryService.java | 21 +++ .../services/InventoryServiceImpl.java | 166 ++++++++++++++++++ src/main/resources/application.properties | 2 + 10 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/dharshi/springboot_cicd/controllers/InventoryController.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/dtos/InventoryRequestDto.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/modals/InventoryStatus.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/repositories/InventoryRepository.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/services/InventoryService.java create mode 100644 src/main/java/com/dharshi/springboot_cicd/services/InventoryServiceImpl.java diff --git a/pom.xml b/pom.xml index da507fa..29ee3b6 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-data-mongodb + org.projectlombok lombok diff --git a/src/main/java/com/dharshi/springboot_cicd/controllers/InventoryController.java b/src/main/java/com/dharshi/springboot_cicd/controllers/InventoryController.java new file mode 100644 index 0000000..cfe5933 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/controllers/InventoryController.java @@ -0,0 +1,42 @@ +package com.dharshi.springboot_cicd.controllers; + +import com.dharshi.springboot_cicd.dtos.ApiResponseDto; +import com.dharshi.springboot_cicd.dtos.InventoryRequestDto; +import com.dharshi.springboot_cicd.services.InventoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/inventory") +@CrossOrigin(origins = "*") +public class InventoryController { + + @Autowired + private InventoryService inventoryService; + + @GetMapping("/all") + public ResponseEntity> getAllInventories(){ + return inventoryService.getAllInventories(); + } + + @GetMapping("/{inventoryId}") + public ResponseEntity> getInventoryById(@PathVariable String inventoryId){ + return inventoryService.getInventoryById(inventoryId); + } + + @PostMapping("/") + public ResponseEntity> createInventory(@RequestBody InventoryRequestDto inventoryRequestDto){ + return inventoryService.createInventory(inventoryRequestDto); + } + + @PutMapping("/") + public ResponseEntity> editInventory(@RequestParam("inventoryId") String inventoryId, @RequestBody InventoryRequestDto inventoryRequestDto){ + return inventoryService.editInventory(inventoryId, inventoryRequestDto); + } + + @DeleteMapping("/") + public ResponseEntity> deleteInventory(@RequestParam("inventoryId") String inventoryId){ + return inventoryService.deleteInventory(inventoryId); + } +} diff --git a/src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java b/src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java new file mode 100644 index 0000000..d542dd3 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java @@ -0,0 +1,12 @@ +package com.dharshi.springboot_cicd.dtos; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class ApiResponseDto { + private boolean isSuccess; + private String message; + private T response; +} diff --git a/src/main/java/com/dharshi/springboot_cicd/dtos/InventoryRequestDto.java b/src/main/java/com/dharshi/springboot_cicd/dtos/InventoryRequestDto.java new file mode 100644 index 0000000..c1356a5 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/dtos/InventoryRequestDto.java @@ -0,0 +1,18 @@ +package com.dharshi.springboot_cicd.dtos; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class InventoryRequestDto { + + private String itemName; + + private double price; + + private int quantity; + +} diff --git a/src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java b/src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java new file mode 100644 index 0000000..d86acc4 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java @@ -0,0 +1,25 @@ +package com.dharshi.springboot_cicd.modals; + + +import lombok.Builder; +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Data +@Document(collection = "inventory") +@Builder +public class Inventory { + + @Id + private String itemId; + + private String itemName; + + private double price; + + private int quantity; + + private InventoryStatus status; + +} diff --git a/src/main/java/com/dharshi/springboot_cicd/modals/InventoryStatus.java b/src/main/java/com/dharshi/springboot_cicd/modals/InventoryStatus.java new file mode 100644 index 0000000..3de9b92 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/modals/InventoryStatus.java @@ -0,0 +1,6 @@ +package com.dharshi.springboot_cicd.modals; + +public enum InventoryStatus { + In_Stock, + Out_of_Stock +} diff --git a/src/main/java/com/dharshi/springboot_cicd/repositories/InventoryRepository.java b/src/main/java/com/dharshi/springboot_cicd/repositories/InventoryRepository.java new file mode 100644 index 0000000..ad9ad54 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/repositories/InventoryRepository.java @@ -0,0 +1,11 @@ +package com.dharshi.springboot_cicd.repositories; + +import com.dharshi.springboot_cicd.modals.Inventory; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + + +@Repository +public interface InventoryRepository extends MongoRepository { + +} diff --git a/src/main/java/com/dharshi/springboot_cicd/services/InventoryService.java b/src/main/java/com/dharshi/springboot_cicd/services/InventoryService.java new file mode 100644 index 0000000..ffc1f59 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/services/InventoryService.java @@ -0,0 +1,21 @@ +package com.dharshi.springboot_cicd.services; + + +import com.dharshi.springboot_cicd.dtos.ApiResponseDto; +import com.dharshi.springboot_cicd.dtos.InventoryRequestDto; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +@Service +public interface InventoryService { + + ResponseEntity> getAllInventories(); + + ResponseEntity> getInventoryById(String inventoryId); + + ResponseEntity> createInventory(InventoryRequestDto inventoryRequestDto); + + ResponseEntity> editInventory(String inventoryId, InventoryRequestDto inventoryRequestDto); + + ResponseEntity> deleteInventory(String inventoryId); +} diff --git a/src/main/java/com/dharshi/springboot_cicd/services/InventoryServiceImpl.java b/src/main/java/com/dharshi/springboot_cicd/services/InventoryServiceImpl.java new file mode 100644 index 0000000..544ff23 --- /dev/null +++ b/src/main/java/com/dharshi/springboot_cicd/services/InventoryServiceImpl.java @@ -0,0 +1,166 @@ +package com.dharshi.springboot_cicd.services; + +import com.dharshi.springboot_cicd.dtos.ApiResponseDto; +import com.dharshi.springboot_cicd.dtos.InventoryRequestDto; +import com.dharshi.springboot_cicd.modals.Inventory; +import com.dharshi.springboot_cicd.modals.InventoryStatus; +import com.dharshi.springboot_cicd.repositories.InventoryRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class InventoryServiceImpl implements InventoryService { + + @Autowired + InventoryRepository inventoryRepository; + + @Override + public ResponseEntity> getAllInventories(){ + List inventories = inventoryRepository.findAll(); + try { + return ResponseEntity.ok( + ApiResponseDto.builder() + .isSuccess(true) + .response(inventories) + .message(inventories.size() + " results found!") + .build() + ); + }catch (Exception e) { +// Try to create a custom exception and handle them using exception handlers + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( + ApiResponseDto.builder() + .isSuccess(false) + .response("Unable to process right now. Try again later!") + .message("No results found!") + .build() + ); + } + } + + @Override + public ResponseEntity> getInventoryById(String inventoryId) { + + try { + Inventory category = inventoryRepository.findById(inventoryId).orElse(null); + return ResponseEntity.ok( + ApiResponseDto.builder() + .isSuccess(true) + .response(category) + .build() + ); + }catch (Exception e) { +// Try to create a custom exception and handle them using exception handlers + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( + ApiResponseDto.builder() + .isSuccess(false) + .response("Unable to process right now. Try again later!") + .message("No results found!") + .build() + ); + } + } + + @Override + public ResponseEntity> createInventory(InventoryRequestDto inventoryRequestDto) { + try { + + InventoryStatus status = inventoryRequestDto.getQuantity() > 0 ? InventoryStatus.In_Stock : InventoryStatus.Out_of_Stock; + + Inventory inventory = Inventory.builder() + .itemName(inventoryRequestDto.getItemName()) + .price(inventoryRequestDto.getPrice()) + .quantity(inventoryRequestDto.getQuantity()) + .status(status) + .build(); + + inventoryRepository.insert(inventory); + return ResponseEntity.status(HttpStatus.CREATED).body( + ApiResponseDto.builder() + .isSuccess(true) + .message("Inventory saved successfully!") + .build() + ); + + }catch (Exception e) { +// Try to create a custom exception and handle them using exception handlers + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( + ApiResponseDto.builder() + .isSuccess(false) + .response("Unable to process right now. Try again later!") + .message("Unable to create Inventory.") + .build() + ); + } + } + + + @Override + public ResponseEntity> editInventory(String inventoryId, InventoryRequestDto inventoryRequestDto) { + try { + + Inventory inventory = inventoryRepository.findById(inventoryId).orElse(null); + + if (inventory == null) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body( + ApiResponseDto.builder() + .isSuccess(false) + .message("Inventory not found!") + .build() + ); + } + + InventoryStatus status = inventoryRequestDto.getQuantity() > 0 ? InventoryStatus.In_Stock : InventoryStatus.Out_of_Stock; + + inventory.setItemName(inventoryRequestDto.getItemName()); + inventory.setPrice(inventoryRequestDto.getPrice()); + inventory.setQuantity(inventoryRequestDto.getQuantity()); + inventory.setStatus(status); + + inventoryRepository.save(inventory); + return ResponseEntity.status(HttpStatus.OK).body( + ApiResponseDto.builder() + .isSuccess(true) + .message("Inventory updated successfully!") + .build() + ); + + }catch (Exception e) { +// Try to create a custom exception and handle them using exception handlers + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( + ApiResponseDto.builder() + .isSuccess(false) + .response("Unable to process right now. Try again later!") + .message("Unable to edit Inventory.") + .build() + ); + } + } + + + @Override + public ResponseEntity> deleteInventory(String inventoryId) { + + try { + inventoryRepository.deleteById(inventoryId); + return ResponseEntity.ok( + ApiResponseDto.builder() + .isSuccess(true) + .message("Inventory deleted successfully!") + .build() + ); + }catch (Exception e) { +// Try to create a custom exception and handle them using exception handlers + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( + ApiResponseDto.builder() + .isSuccess(false) + .response("Unable to process right now. Try again later!") + .message("No results found!") + .build() + ); + } + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 799b3a0..51316d3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,3 @@ spring.application.name=springboot-cicd +spring.data.mongodb.uri=mongodb+srv://demo:demo@mycluster.ndj34hk.mongodb.net/?retryWrites=true&w=majority&appName=mycluster +spring.data.mongodb.database=inventory-service \ No newline at end of file