feat: entrepreneur api and api service

This commit is contained in:
Théo Le Lez
2025-02-26 14:51:02 +01:00
parent e66fa33577
commit dd5ca2cbd7
5 changed files with 176 additions and 25 deletions

View File

@ -0,0 +1,54 @@
package enseirb.myinpulse.service.database;
import enseirb.myinpulse.model.Annotation;
import enseirb.myinpulse.repository.AnnotationRepository;
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 {
private final AnnotationRepository annotationRepository;
@Autowired
AnnotationService(AnnotationRepository annotationRepository) {
this.annotationRepository = annotationRepository;
}
public Iterable<Annotation> getAllAnnotations() {
return annotationRepository.findAll();
}
public Annotation getAnnotationById(Long id) {
Optional<Annotation> annotation = annotationRepository.findById(id);
if (annotation.isEmpty()) {
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> annotation = annotationRepository.findById(id);
if (annotation.isEmpty()) {
return null;
}
if (comment != null) {
annotation.get().setComment(comment);
}
return this.annotationRepository.save(annotation.get());
}
}