Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.example.potionsapi.model.Potion;
import com.example.potionsapi.service.PotionService;


@RestController
public class PotionController {

Expand Down Expand Up @@ -54,4 +55,7 @@ public HttpStatus deletePotion(@PathVariable UUID id) {
this.potionService.deletePotion(id);
return HttpStatus.OK;
}
}
}



Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <Potion> getAllPotion() {
return this.potionRepository.findAll();
public List<Potion> 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);
}
}
}