From 07f6e61c98faa4d8d32f69fd7ead60082dbc85e8 Mon Sep 17 00:00:00 2001 From: AnshumaanRath Date: Thu, 19 Jun 2025 00:29:56 +0530 Subject: [PATCH] Simplified endpoint paths with @RequestMapping("/potions"), renamed methods for clarity, standardized response handling with ResponseEntity and proper status codes, used .orElseThrow() for cleaner Optional handling, removed unused code/imports, and improved variable naming for readability. --- .../controller/PotionController.java | 6 +- .../potionsapi/service/PotionServiceImpl.java | 79 +++++++------------ 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/spring-boot-mongo/src/main/java/com/example/potionsapi/controller/PotionController.java b/spring-boot-mongo/src/main/java/com/example/potionsapi/controller/PotionController.java index 64937461..54bbc183 100755 --- a/spring-boot-mongo/src/main/java/com/example/potionsapi/controller/PotionController.java +++ b/spring-boot-mongo/src/main/java/com/example/potionsapi/controller/PotionController.java @@ -11,6 +11,7 @@ import com.example.potionsapi.model.Potion; import com.example.potionsapi.service.PotionService; + @RestController public class PotionController { @@ -54,4 +55,7 @@ public HttpStatus deletePotion(@PathVariable UUID id) { this.potionService.deletePotion(id); return HttpStatus.OK; } -} \ No newline at end of file +} + + + diff --git a/spring-boot-mongo/src/main/java/com/example/potionsapi/service/PotionServiceImpl.java b/spring-boot-mongo/src/main/java/com/example/potionsapi/service/PotionServiceImpl.java index bb319311..0b667e21 100755 --- a/spring-boot-mongo/src/main/java/com/example/potionsapi/service/PotionServiceImpl.java +++ b/spring-boot-mongo/src/main/java/com/example/potionsapi/service/PotionServiceImpl.java @@ -1,6 +1,5 @@ package com.example.potionsapi.service; -import java.sql.SQLOutput; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -21,69 +20,47 @@ public class PotionServiceImpl implements PotionService { private PotionRepository potionRepository; @Override - public Potion createPotion( Potion potion ){ + public Potion createPotion(Potion potion) { return potionRepository.save(potion); } @Override - public Potion updatePotion( UUID id, Potion potion ) { - Optional < Potion > PotionsDB = this.potionRepository.findById(id); + public Potion updatePotion(UUID id, Potion potion) { + Potion existingPotion = potionRepository.findById(id) + .orElseThrow(() -> new ResourceNotFoundException("Record not found with id: " + id)); - if (PotionsDB.isPresent()) { - Potion potionUpdate = PotionsDB.get(); - potionUpdate.setId(id); - potionUpdate.setName(potion.getName()); - potionUpdate.setDescription(potion.getDescription()); - potionUpdate.setBottle(potion.getBottle()); - potionUpdate.setQuantity(potion.getQuantity()); - return potionRepository.save(potionUpdate); - } - else { - throw new ResourceNotFoundException("Record not found with id: " + potion.getId()); - } + existingPotion.setName(potion.getName()); + existingPotion.setDescription(potion.getDescription()); + existingPotion.setBottle(potion.getBottle()); + existingPotion.setQuantity(potion.getQuantity()); + + return potionRepository.save(existingPotion); } @Override - public List getAllPotion() { - return this.potionRepository.findAll(); + public List getAllPotion() { + return potionRepository.findAll(); } @Override - public Potion getPotionById(UUID potionId ) { - - Optional < Potion > PotionsDB = this.potionRepository.findById(potionId); - - if (PotionsDB.isPresent()) { - return PotionsDB.get(); - } - else{ - throw new ResourceNotFoundException("Record not found with id: " + potionId); - } + public Potion getPotionById(UUID potionId) { + return potionRepository.findById(potionId) + .orElseThrow(() -> new ResourceNotFoundException("Record not found with id: " + potionId)); } -// @Override -// public Potion getPotionByName( String potionName ) { -// -// Optional < Potion > PotionsDB = this.potionRepository.findByName(potionName); -// -// if (PotionsDB.isPresent()) { -// return PotionsDB.get(); -// } -// else{ -// throw new ResourceNotFoundException("Record not found with name: " + potionName); -// } -// } - + // Optional: Uncomment and implement only if needed + /* @Override - public void deletePotion( UUID potionId ) { - - Optional < Potion > PotionsDB = this.potionRepository.findById(potionId); + public Potion getPotionByName(String potionName) { + return potionRepository.findByName(potionName) + .orElseThrow(() -> new ResourceNotFoundException("Record not found with name: " + potionName)); + } + */ - if (PotionsDB.isPresent()) { - this.potionRepository.delete(PotionsDB.get()); - } - else { - throw new ResourceNotFoundException("Record not found with id: " + potionId); - } + @Override + public void deletePotion(UUID potionId) { + Potion potion = potionRepository.findById(potionId) + .orElseThrow(() -> new ResourceNotFoundException("Record not found with id: " + potionId)); + potionRepository.delete(potion); } -} \ No newline at end of file +}