Skip to content

Instantly share code, notes, and snippets.

@Shashwat79802
Created June 10, 2023 12:25
Show Gist options
  • Save Shashwat79802/75c45eda568c3dab26b55f0c68257534 to your computer and use it in GitHub Desktop.
Save Shashwat79802/75c45eda568c3dab26b55f0c68257534 to your computer and use it in GitHub Desktop.
package com.example.potionsbank.service;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.potionsapi.exception.ResourceNotFoundException;
import com.example.potionsapi.model.Potion;
import com.example.potionsapi.repository.PotionRepository;
@Service
@Transactional
public class PotionServiceImpl implements PotionService {
@Autowired
private PotionRepository potionRepository;
@Override
public Potion createPotion( Potion potion ){
return potionRepository.save(potion);
}
@Override
public Potion updatePotion( UUID id, Potion potion ) {
Optional < Potion > PotionsDB = this.potionRepository.findById(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());
}
}
@Override
public List <Potion> getAllPotion() {
return this.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);
}
}
@Override
public void deletePotion( UUID potionId ) {
Optional < Potion > PotionsDB = this.potionRepository.findById(potionId);
if (PotionsDB.isPresent()) {
this.potionRepository.delete(PotionsDB.get());
}
else {
throw new ResourceNotFoundException("Record not found with id: " + potionId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment