Pierre Tellier 80b2d087e4
Some checks failed
Format / formatting (push) Failing after 7s
CI / build (push) Successful in 11s
feat: implemented date filtration and a utils service to prevent code ducplication
2025-02-28 11:45:55 +01:00

94 lines
3.6 KiB
Java

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<SectionCell> getAllSectionCells() {
return this.sectionCellRepository.findAll();
}
public SectionCell getSectionCellById(Long id) {
Optional<SectionCell> 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> 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<SectionCell> 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<Appointment> getAppointmentsBySectionCellId(Long sectionCellId) {
SectionCell sectionCell = getSectionCellById(sectionCellId);
return sectionCell.getAppointmentSectionCell();
}
public Iterable<SectionCell> getSectionCellsByProjectAndSectionIdBeforeDate(
Project project, long sectionId, LocalDateTime date) {
return sectionCellRepository.findByProjectSectionCellAndSectionIdAndModificationDateBefore(
project, sectionId, date);
}
}