61 lines
1.9 KiB
Java
61 lines
1.9 KiB
Java
package enseirb.myinpulse.service.database;
|
|
|
|
import enseirb.myinpulse.model.Report;
|
|
import enseirb.myinpulse.repository.ReportRepository;
|
|
|
|
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 ReportService {
|
|
|
|
protected static final Logger logger = LogManager.getLogger();
|
|
|
|
private final ReportRepository reportRepository;
|
|
|
|
@Autowired
|
|
ReportService(ReportRepository reportRepository) {
|
|
this.reportRepository = reportRepository;
|
|
}
|
|
|
|
public Iterable<Report> getAllReports() {
|
|
return this.reportRepository.findAll();
|
|
}
|
|
|
|
public Report getReportById(Long id) {
|
|
Optional<Report> report = this.reportRepository.findById(id);
|
|
if (report.isEmpty()) {
|
|
logger.error("getReportById : No report found with id {}", id);
|
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce compte rendu n'existe pas");
|
|
}
|
|
return report.get();
|
|
}
|
|
|
|
// TODO: do some validation
|
|
public Report addNewReport(Report report) {
|
|
return this.reportRepository.save(report);
|
|
}
|
|
|
|
public void deleteReportById(Long id) {
|
|
this.reportRepository.deleteById(id);
|
|
}
|
|
|
|
public Report updateReport(Long id, String reportContent) {
|
|
Optional<Report> report = this.reportRepository.findById(id);
|
|
if (report.isEmpty()) {
|
|
logger.error("updateReport : No report found with id {}", id);
|
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Ce compte rendu n'existe pas");
|
|
}
|
|
if (reportContent != null) {
|
|
report.get().setReportContent(reportContent);
|
|
}
|
|
return this.reportRepository.save(report.get());
|
|
}
|
|
}
|