backend-api #6
@@ -21,22 +21,18 @@ public class SharedApi {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * TODO: It does not looks like a good id to have the title and the date in the url. What even
 | 
			
		||||
     * TODO: is the title btw ? if this is the LC section, wouldn't it be better to use an ID ?
 | 
			
		||||
     * TODO: choose return type, cf comment in LCSection
 | 
			
		||||
     *
 | 
			
		||||
     * <p>Endpoint used to get the data inside the lean canvas
 | 
			
		||||
     * Endpoint used to get the data inside the lean canvas
 | 
			
		||||
     *
 | 
			
		||||
     * @return a list of lean canvas sections
 | 
			
		||||
     */
 | 
			
		||||
    @GetMapping("/shared/project/lcsection/{projectId}/{title}/{date}")
 | 
			
		||||
    @GetMapping("/shared/project/lcsection/{projectId}/{sectionId}/{date}")
 | 
			
		||||
    public Iterable<SectionCell> getLCSection(
 | 
			
		||||
            @PathVariable("projectId") String projectId,
 | 
			
		||||
            @PathVariable("title") String title,
 | 
			
		||||
            @PathVariable("projectId") Long projectId,
 | 
			
		||||
            @PathVariable("sectionId") Long sectionId,
 | 
			
		||||
            @PathVariable("date") String date,
 | 
			
		||||
            @AuthenticationPrincipal Jwt principal) {
 | 
			
		||||
        return sharedApiService.getLCSection(
 | 
			
		||||
                projectId, title, date, principal.getClaimAsString("email"));
 | 
			
		||||
        return sharedApiService.getSectionCells(
 | 
			
		||||
                projectId, sectionId, date, principal.getClaimAsString("email"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -11,42 +11,39 @@ import java.util.List;
 | 
			
		||||
@Table(name = "section_cell")
 | 
			
		||||
public class SectionCell {
 | 
			
		||||
 | 
			
		||||
    @ManyToMany(mappedBy = "listSectionCell")
 | 
			
		||||
    private final List<Appointment> appointment = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    @OneToMany(mappedBy = "sectionCellAnnotation", fetch = FetchType.LAZY, orphanRemoval = true)
 | 
			
		||||
    private final List<Annotation> listAnnotation = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    @Id
 | 
			
		||||
    @NotNull
 | 
			
		||||
    @GeneratedValue(strategy = GenerationType.IDENTITY)
 | 
			
		||||
    private Long idSectionCell;
 | 
			
		||||
 | 
			
		||||
    @Column(length = 255)
 | 
			
		||||
    private String title;
 | 
			
		||||
 | 
			
		||||
    @Column() private long sectionId;
 | 
			
		||||
    private String contentSectionCell;
 | 
			
		||||
 | 
			
		||||
    /*@ManyToOne(fetch = FetchType.LAZY)
 | 
			
		||||
    @JoinColumn(name = "idAdministrator")
 | 
			
		||||
    private Administrator administratorSectionCell;*/
 | 
			
		||||
    // should now be useless
 | 
			
		||||
    private LocalDateTime modificationDate;
 | 
			
		||||
 | 
			
		||||
    @ManyToOne(fetch = FetchType.LAZY)
 | 
			
		||||
    @JoinColumn(name = "idProject")
 | 
			
		||||
    private Project projectSectionCell;
 | 
			
		||||
 | 
			
		||||
    /*@ManyToOne(fetch = FetchType.LAZY)
 | 
			
		||||
    @JoinColumn(name = "idAdministrator")
 | 
			
		||||
    private Administrator administratorSectionCell;*/
 | 
			
		||||
    // should now be useless
 | 
			
		||||
 | 
			
		||||
    @ManyToMany(mappedBy = "listSectionCell")
 | 
			
		||||
    private List<Appointment> appointment = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    @OneToMany(mappedBy = "sectionCellAnnotation", fetch = FetchType.LAZY, orphanRemoval = true)
 | 
			
		||||
    private List<Annotation> listAnnotation = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    public SectionCell() {}
 | 
			
		||||
 | 
			
		||||
    public SectionCell(
 | 
			
		||||
            Long idSectionCell,
 | 
			
		||||
            String title,
 | 
			
		||||
            Long sectionId,
 | 
			
		||||
            String contentSectionCell,
 | 
			
		||||
            LocalDateTime modificationDate) {
 | 
			
		||||
        this.idSectionCell = idSectionCell;
 | 
			
		||||
        this.title = title;
 | 
			
		||||
        this.sectionId = sectionId;
 | 
			
		||||
        this.contentSectionCell = contentSectionCell;
 | 
			
		||||
        this.modificationDate = modificationDate;
 | 
			
		||||
    }
 | 
			
		||||
@@ -59,12 +56,12 @@ public class SectionCell {
 | 
			
		||||
        this.idSectionCell = idSectionCell;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getTitle() {
 | 
			
		||||
        return title;
 | 
			
		||||
    public Long getSectionId() {
 | 
			
		||||
        return sectionId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setTitle(String title) {
 | 
			
		||||
        this.title = title;
 | 
			
		||||
    public void setSectionId(Long sectionId) {
 | 
			
		||||
        this.sectionId = sectionId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getContentSectionCell() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,13 @@
 | 
			
		||||
package enseirb.myinpulse.repository;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.Project;
 | 
			
		||||
import enseirb.myinpulse.model.SectionCell;
 | 
			
		||||
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
 | 
			
		||||
 | 
			
		||||
@RepositoryRestResource
 | 
			
		||||
public interface SectionCellRepository extends JpaRepository<SectionCell, Long> {}
 | 
			
		||||
public interface SectionCellRepository extends JpaRepository<SectionCell, Long> {
 | 
			
		||||
 | 
			
		||||
    Iterable<SectionCell> findByProjectSectionCellAndSectionId(Project project, long sectionId);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,9 +2,9 @@ package enseirb.myinpulse.service;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.Project;
 | 
			
		||||
import enseirb.myinpulse.model.SectionCell;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.service.database.ProjectService;
 | 
			
		||||
import enseirb.myinpulse.service.database.SectionCellService;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
@@ -31,7 +31,7 @@ public class EntrepreneurApiService {
 | 
			
		||||
        }
 | 
			
		||||
        sectionCellService.updateSectionCell(
 | 
			
		||||
                sectionCellId,
 | 
			
		||||
                sectionCell.getTitle(),
 | 
			
		||||
                sectionCell.getSectionId(),
 | 
			
		||||
                sectionCell.getContentSectionCell(),
 | 
			
		||||
                sectionCell.getModificationDate());
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,7 @@
 | 
			
		||||
package enseirb.myinpulse.service;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.*;
 | 
			
		||||
import enseirb.myinpulse.service.database.AdministratorService;
 | 
			
		||||
import enseirb.myinpulse.service.database.EntrepreneurService;
 | 
			
		||||
import enseirb.myinpulse.service.database.ProjectService;
 | 
			
		||||
import enseirb.myinpulse.service.database.UserService;
 | 
			
		||||
import enseirb.myinpulse.service.database.*;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
@@ -22,17 +19,20 @@ public class SharedApiService {
 | 
			
		||||
    private final UserService userService;
 | 
			
		||||
    private final ProjectService projectService;
 | 
			
		||||
    private final EntrepreneurService entrepreneurService;
 | 
			
		||||
    private final SectionCellService sectionCellService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    SharedApiService(
 | 
			
		||||
            AdministratorService administratorService,
 | 
			
		||||
            UserService userService,
 | 
			
		||||
            ProjectService projectService,
 | 
			
		||||
            EntrepreneurService entrepreneurService) {
 | 
			
		||||
            EntrepreneurService entrepreneurService,
 | 
			
		||||
            SectionCellService sectionCellService) {
 | 
			
		||||
        this.administratorService = administratorService;
 | 
			
		||||
        this.userService = userService;
 | 
			
		||||
        this.projectService = projectService;
 | 
			
		||||
        this.entrepreneurService = entrepreneurService;
 | 
			
		||||
        this.sectionCellService = sectionCellService;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: test
 | 
			
		||||
@@ -58,13 +58,23 @@ public class SharedApiService {
 | 
			
		||||
        return entrepreneur.getProjectParticipation() == project;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public Iterable<SectionCell> getLCSection(
 | 
			
		||||
            String projectId, String title, String date, String mail) {
 | 
			
		||||
        throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
 | 
			
		||||
    // TODO filter this with date
 | 
			
		||||
    public Iterable<SectionCell> getSectionCells(
 | 
			
		||||
            long projectId, long sectionId, String date, String mail) {
 | 
			
		||||
 | 
			
		||||
        if (!isAllowedToCheckProject(mail, projectId)) {
 | 
			
		||||
            logger.warn(
 | 
			
		||||
                    "User {} tried to check section cells of the project {} but is not allowed to.",
 | 
			
		||||
                    mail,
 | 
			
		||||
                    projectId);
 | 
			
		||||
            throw new ResponseStatusException(
 | 
			
		||||
                    HttpStatus.UNAUTHORIZED, "You're not allowed to check this project");
 | 
			
		||||
        }
 | 
			
		||||
        Project project = this.projectService.getProjectById(projectId);
 | 
			
		||||
        return this.sectionCellService.getSectionCellsByProject(project, sectionId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: test, protect via email
 | 
			
		||||
    // TODO: test
 | 
			
		||||
    public Iterable<Entrepreneur> getEntrepreneursByProjectId(long projectId, String mail) {
 | 
			
		||||
        if (!isAllowedToCheckProject(mail, projectId)) {
 | 
			
		||||
            logger.warn(
 | 
			
		||||
@@ -92,14 +102,17 @@ public class SharedApiService {
 | 
			
		||||
        return project.getAdministrator();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public Iterable<Appointment> getAppointmentsByProjectId(int projectId, String mail) {
 | 
			
		||||
        throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public void getPDFReport(int appointmentId, String mail) {
 | 
			
		||||
        throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public void createAppointmentRequest(Appointment appointment, String mail) {
 | 
			
		||||
        throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package enseirb.myinpulse.service.database;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.Project;
 | 
			
		||||
import enseirb.myinpulse.model.SectionCell;
 | 
			
		||||
import enseirb.myinpulse.repository.SectionCellRepository;
 | 
			
		||||
 | 
			
		||||
@@ -43,14 +44,14 @@ public class SectionCellService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public SectionCell updateSectionCell(
 | 
			
		||||
            Long id, String title, String contentSectionCell, LocalDateTime modificationDate) {
 | 
			
		||||
            Long id, Long sectionId, String contentSectionCell, LocalDateTime modificationDate) {
 | 
			
		||||
        Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
 | 
			
		||||
        if (sectionCell.isEmpty()) {
 | 
			
		||||
            throw new ResponseStatusException(
 | 
			
		||||
                    HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
 | 
			
		||||
        }
 | 
			
		||||
        if (title != null) {
 | 
			
		||||
            sectionCell.get().setTitle(title);
 | 
			
		||||
        if (sectionId != null) {
 | 
			
		||||
            sectionCell.get().setSectionId(sectionId);
 | 
			
		||||
        }
 | 
			
		||||
        if (contentSectionCell != null) {
 | 
			
		||||
            sectionCell.get().setContentSectionCell(contentSectionCell);
 | 
			
		||||
@@ -60,4 +61,8 @@ public class SectionCellService {
 | 
			
		||||
        }
 | 
			
		||||
        return this.sectionCellRepository.save(sectionCell.get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Iterable<SectionCell> getSectionCellsByProject(Project project, Long sectionId) {
 | 
			
		||||
        return this.sectionCellRepository.findByProjectSectionCellAndSectionId(project, sectionId);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,62 +0,0 @@
 | 
			
		||||
package enseirb.myinpulse.service.database.old_controllers_to_convert_to_services;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.SectionCell;
 | 
			
		||||
import enseirb.myinpulse.repository.SectionCellRepository;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
import org.springframework.web.server.ResponseStatusException;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
public class SectionCellController {
 | 
			
		||||
 | 
			
		||||
    @Autowired SectionCellRepository sectionCellRepository;
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/SectionCell")
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Iterable<SectionCell> allSectionCells() {
 | 
			
		||||
        return this.sectionCellRepository.findAll();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping("/SectionCell/{id}")
 | 
			
		||||
    public SectionCell getSectionCellById(@PathVariable Long id) {
 | 
			
		||||
        Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
 | 
			
		||||
        if (sectionCell.isEmpty()) {
 | 
			
		||||
            throw new ResponseStatusException(
 | 
			
		||||
                    HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
 | 
			
		||||
        }
 | 
			
		||||
        return sectionCell.get();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/SectionCell")
 | 
			
		||||
    public SectionCell addSectionCell(@RequestBody SectionCell sectionCell) {
 | 
			
		||||
        return this.sectionCellRepository.save(sectionCell);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/SectionCell/{id}")
 | 
			
		||||
    public SectionCell updateSectionCell(
 | 
			
		||||
            @PathVariable Long id,
 | 
			
		||||
            String title,
 | 
			
		||||
            String contentSectionCell,
 | 
			
		||||
            LocalDateTime modificationDate) {
 | 
			
		||||
        Optional<SectionCell> sectionCell = this.sectionCellRepository.findById(id);
 | 
			
		||||
        if (sectionCell.isEmpty()) {
 | 
			
		||||
            throw new ResponseStatusException(
 | 
			
		||||
                    HttpStatus.NOT_FOUND, "Cette cellule de section n'existe pas");
 | 
			
		||||
        }
 | 
			
		||||
        if (title != null) {
 | 
			
		||||
            sectionCell.get().setTitle(title);
 | 
			
		||||
        }
 | 
			
		||||
        if (contentSectionCell != null) {
 | 
			
		||||
            sectionCell.get().setContentSectionCell(contentSectionCell);
 | 
			
		||||
        }
 | 
			
		||||
        if (modificationDate != null) {
 | 
			
		||||
            sectionCell.get().setModificationDate(modificationDate);
 | 
			
		||||
        }
 | 
			
		||||
        return this.sectionCellRepository.save(sectionCell.get());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user