-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f7d87c
commit 6fa837d
Showing
10 changed files
with
307 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/dharshi/springboot_cicd/controllers/InventoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/com/dharshi/springboot_cicd/dtos/ApiResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/dharshi/springboot_cicd/dtos/InventoryRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/java/com/dharshi/springboot_cicd/modals/Inventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/dharshi/springboot_cicd/modals/InventoryStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/dharshi/springboot_cicd/repositories/InventoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/dharshi/springboot_cicd/services/InventoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
166 changes: 166 additions & 0 deletions
166
src/main/java/com/dharshi/springboot_cicd/services/InventoryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |