package enseirb.myinpulse.service.database; import enseirb.myinpulse.model.Appointment; import enseirb.myinpulse.model.Project; import enseirb.myinpulse.model.SectionCell; import enseirb.myinpulse.repository.SectionCellRepository; 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.time.LocalDateTime; import java.util.List; import java.util.Optional; @Service public class SectionCellService { protected static final Logger logger = LogManager.getLogger(); private final SectionCellRepository sectionCellRepository; @Autowired SectionCellService(SectionCellRepository sectionCellRepository) { this.sectionCellRepository = sectionCellRepository; } public Iterable getAllSectionCells() { return this.sectionCellRepository.findAll(); } public SectionCell getSectionCellById(Long id) { Optional sectionCell = this.sectionCellRepository.findById(id); if (sectionCell.isEmpty()) { logger.error("getSectionCellById : No sectionCell found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } return sectionCell.get(); } public SectionCell addNewSectionCell(SectionCell sectionCell) { return this.sectionCellRepository.save(sectionCell); } public void removeSectionCellById(Long id) { this.sectionCellRepository.deleteById(id); } public SectionCell updateSectionCell( Long id, Long sectionId, String contentSectionCell, LocalDateTime modificationDate) { Optional sectionCell = this.sectionCellRepository.findById(id); if (sectionCell.isEmpty()) { logger.error("updateSectionCell : No sectionCell found with id {}", id); throw new ResponseStatusException( HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas"); } if (sectionId != null) { sectionCell.get().setSectionId(sectionId); } if (contentSectionCell != null) { sectionCell.get().setContentSectionCell(contentSectionCell); } if (modificationDate != null) { sectionCell.get().setModificationDate(modificationDate); } return this.sectionCellRepository.save(sectionCell.get()); } public Iterable getSectionCellsByProject(Project project, Long sectionId) { return this.sectionCellRepository.findByProjectSectionCellAndSectionId(project, sectionId); } public Long getProjectId(Long sectionCellId) { SectionCell sectionCell = getSectionCellById(sectionCellId); Project sectionProject = sectionCell.getProjectSectionCell(); return sectionProject.getIdProject(); } public List getAppointmentsBySectionCellId(Long sectionCellId) { SectionCell sectionCell = getSectionCellById(sectionCellId); return sectionCell.getAppointmentSectionCell(); } public Iterable getSectionCellsByProjectAndSectionIdBeforeDate( Project project, long sectionId, LocalDateTime date) { return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore( project, sectionId, date); } }