package enseirb.myinpulse.service.database; import enseirb.myinpulse.model.Annotation; import enseirb.myinpulse.repository.AnnotationRepository; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.util.Optional; @Service public class AnnotationService { protected static final Logger logger = LogManager.getLogger(); private final AnnotationRepository annotationRepository; @Autowired AnnotationService(AnnotationRepository annotationRepository) { this.annotationRepository = annotationRepository; } public Iterable getAllAnnotations() { return annotationRepository.findAll(); } public Annotation getAnnotationById(Long id) { Optional annotation = annotationRepository.findById(id); if (annotation.isEmpty()) { logger.error("getAnnotationById : No annotation found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette annotation n'existe pas"); } return annotation.get(); } public Annotation addNewAnnotation(Annotation annotation) { return this.annotationRepository.save(annotation); } public void deleteAnnotationById(Long id) { this.annotationRepository.deleteById(id); } public Annotation updateAnnotation(Long id, String comment) { Optional annotation = annotationRepository.findById(id); if (annotation.isEmpty()) { logger.error("updateAnnotation : No annotation found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette annotation n'existe pas"); } if (comment != null) { annotation.get().setComment(comment); } return this.annotationRepository.save(annotation.get()); } }