Skip to content

Commit

Permalink
add crud operations
Browse files Browse the repository at this point in the history
  • Loading branch information
DharshiBalasubramaniyam committed Jul 3, 2024
1 parent 4f7d87c commit 6fa837d
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ApiResponseDto<?>> getAllInventories(){
return inventoryService.getAllInventories();
}

@GetMapping("/{inventoryId}")
public ResponseEntity<ApiResponseDto<?>> getInventoryById(@PathVariable String inventoryId){
return inventoryService.getInventoryById(inventoryId);
}

@PostMapping("/")
public ResponseEntity<ApiResponseDto<?>> createInventory(@RequestBody InventoryRequestDto inventoryRequestDto){
return inventoryService.createInventory(inventoryRequestDto);
}

@PutMapping("/")
public ResponseEntity<ApiResponseDto<?>> editInventory(@RequestParam("inventoryId") String inventoryId, @RequestBody InventoryRequestDto inventoryRequestDto){
return inventoryService.editInventory(inventoryId, inventoryRequestDto);
}

@DeleteMapping("/")
public ResponseEntity<ApiResponseDto<?>> deleteInventory(@RequestParam("inventoryId") String inventoryId){
return inventoryService.deleteInventory(inventoryId);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dharshi.springboot_cicd.dtos;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class ApiResponseDto<T> {
private boolean isSuccess;
private String message;
private T response;
}
Original file line number Diff line number Diff line change
@@ -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;

}
25 changes: 25 additions & 0 deletions src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dharshi.springboot_cicd.modals;

public enum InventoryStatus {
In_Stock,
Out_of_Stock
}
Original file line number Diff line number Diff line change
@@ -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<Inventory,String> {

}
Original file line number Diff line number Diff line change
@@ -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<ApiResponseDto<?>> getAllInventories();

ResponseEntity<ApiResponseDto<?>> getInventoryById(String inventoryId);

ResponseEntity<ApiResponseDto<?>> createInventory(InventoryRequestDto inventoryRequestDto);

ResponseEntity<ApiResponseDto<?>> editInventory(String inventoryId, InventoryRequestDto inventoryRequestDto);

ResponseEntity<ApiResponseDto<?>> deleteInventory(String inventoryId);
}
Original file line number Diff line number Diff line change
@@ -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<ApiResponseDto<?>> getAllInventories(){
List<Inventory> 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<ApiResponseDto<?>> 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<ApiResponseDto<?>> 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<ApiResponseDto<?>> 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<ApiResponseDto<?>> 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()
);
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6fa837d

Please sign in to comment.