Skip to content

Commit

Permalink
Teams - Beta 2
Browse files Browse the repository at this point in the history
  • Loading branch information
michele6000 committed Sep 28, 2020
2 parents 1167e83 + b5b3a09 commit b174bd5
Show file tree
Hide file tree
Showing 104 changed files with 585 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.springframework.http.ResponseEntity.ok;
Expand Down Expand Up @@ -94,23 +95,28 @@ public void showImage(@RequestParam("id") String username, HttpServletResponse r


@PostMapping("/addProfessor")
public ProfessorDTO addProfessor(@RequestPart("user") ProfessorDTO professor,
public ProfessorDTO addProfessor(@RequestPart("user") UserDTO professor,
@RequestPart("file") MultipartFile file) {
if (!professor.getEmail().endsWith("@polito.it"))
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "user not allowed");

if (!service.addProfessor(professor, file))
throw new ResponseStatusException(HttpStatus.CONFLICT, professor.getId());
return professor;
try{
return service.addProfessor(professor, file);
}catch (Exception e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}

@PostMapping("/addStudent")
public StudentDTO addProfessor(@RequestPart("user") StudentDTO student,
public StudentDTO addStudent(@RequestPart("user") UserDTO student,
@RequestPart("file") MultipartFile file) {
if (!student.getEmail().endsWith("@studenti.polito.it"))
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "user not allowed");

if (!service.addStudent(student, file)) throw new ResponseStatusException(HttpStatus.CONFLICT, student.getId());
return student;
try{
return service.addStudent(student, file);
}catch (Exception e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.polito.ai.project.services.NotificationService;
import it.polito.ai.project.services.TeamService;
import it.polito.ai.project.services.VmService;
import it.polito.ai.project.wrappers.GenericResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.GrantedAuthority;
Expand Down Expand Up @@ -51,9 +52,11 @@ public List<VMDTO> getTeamVMs(@PathVariable Long teamId) {
}

@GetMapping("/{teamId}/stats")
public String getTeamStats(@PathVariable Long teamId) {
public GenericResponse getTeamStats(@PathVariable Long teamId) {
try {
return vmService.getTeamStat(teamId);
GenericResponse response = new GenericResponse();
response.setGenericResponse(vmService.getTeamStat(teamId));
return response;
} catch (TeamServiceException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package it.polito.ai.project.controllers;

import it.polito.ai.project.dtos.ProfessorDTO;
import it.polito.ai.project.exceptions.TeamServiceException;
import it.polito.ai.project.services.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -35,4 +40,13 @@ public ResponseEntity currentUser(
model.put("myId", userDetails.getUsername().split("@")[0]);
return ResponseEntity.ok(model);
}

@GetMapping("/API/professor/getAll")
public List<ProfessorDTO> getAllProfessor(){
try {
return service.getAllProfessors();
}catch (Exception e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public class ProfessorDTO {
private String firstName;
private String name;
private String email;
private String password;
private Byte[] image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ public class StudentDTO extends RepresentationModel<StudentDTO> {
private String firstName;

private String email;
private String password;
private Byte[] image;
}
11 changes: 8 additions & 3 deletions back-end/src/main/java/it/polito/ai/project/dtos/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Lob;

@Data
public class UserDTO {
private Long id;
private String username;
private String username; //matricola
private String firstName;
private String name;
private String email; //email completa
private String password;
private String role;
private Byte[] image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class Professor {
private String firstName;
private String name;
private String email;
private String password;
@Lob
private Byte[] image;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class Student {
private String name;
private String firstName;
private String email;
private String password;
@Lob
private Byte[] image;

Expand Down
10 changes: 6 additions & 4 deletions back-end/src/main/java/it/polito/ai/project/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
@AllArgsConstructor
public class User implements UserDetails {
@Id
@NotEmpty
private String username;

@NotEmpty
private String username; //matricola
// private String name;
// private String firstName;
// private String email; // email completa
private String password;
// @Lob
// private Byte[] image;

@ElementCollection(fetch = FetchType.EAGER)
@Builder.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public SubmissionDTO addSubmission(SubmissionDTO submissionDTO, String courseNam
if (!course.isEnabled())
throw new CourseDisabledException("Course not enabled!");

// l'altro non andava bene va fatto prima di fare le modifiche sul repo, e non avendo l'id quello non andava bene
if (!course.getProfessors().contains(profRepo.getOne(profId))) {
submissionRepo.delete(submissionEntity);
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "You are not a professor of this course!");
Expand Down Expand Up @@ -120,22 +119,23 @@ public SubmissionDTO getSubmission(String courseName, Long submissionId, String
throw new SubmissionNotFoundException("Submission not found!");

if (!profRepo.existsById(studentId)) { //a student is requiring submission
if (studentRepo.getOne(studentId).getSolutions().stream()
.filter(solution -> solution.getVersion() >= 0)
.noneMatch(sol -> sol.getSubmission().getId().equals(submissionId))) {
//no solution for this submission and this student-->create an empty solution with status "READ"
List<Solution> ReadSol = submissionRepo.getOne(submissionId).getSolutions().stream()
.filter(sol -> sol.getStudent().getId().equals(studentId))
.filter(sol -> sol.getVersion() == -1)
.filter(sol -> sol.getSubmission().getId().equals(submissionId))
.collect(Collectors.toList() );
if (ReadSol.size() > 0){
submissionRepo.getOne(submissionId).getSolutions().remove(ReadSol.get(0));
solutionRepo.delete(ReadSol.get(0));
if(getLastSolVersion(submissionId,studentId).isRevisable())
if (studentRepo.getOne(studentId).getSolutions().stream()
.filter(solution -> solution.getVersion() >= 0)
.noneMatch(sol -> sol.getSubmission().getId().equals(submissionId))) {
//no solution for this submission and this student-->create an empty solution with status "READ"
List<Solution> ReadSol = submissionRepo.getOne(submissionId).getSolutions().stream()
.filter(sol -> sol.getStudent().getId().equals(studentId))
.filter(sol -> sol.getVersion() == -1)
.filter(sol -> sol.getSubmission().getId().equals(submissionId))
.collect(Collectors.toList() );
if (ReadSol.size() > 0){
submissionRepo.getOne(submissionId).getSolutions().remove(ReadSol.get(0));
solutionRepo.delete(ReadSol.get(0));
}
if (!submissionRepo.getOne(submissionId).getExpiryDate().before(new Date()))
createNewSol(studentId, submissionId);
}
if (!submissionRepo.getOne(submissionId).getExpiryDate().before(new Date()))
createNewSol(studentId, submissionId);
}
}

return modelMapper.map(submissionRepo.getOne(submissionId), SubmissionDTO.class);
Expand Down Expand Up @@ -170,7 +170,7 @@ public SolutionDTO addSolution(Long submissionId, SolutionDTO solutionDTO, Strin

List<Solution> ReadSol = submission.getSolutions().stream()
.filter(sol -> sol.getStudent().getId().equals(studentId))
.filter(sol -> sol.getVersion() == 0)
.filter(sol -> sol.getVersion() == 0 || sol.getVersion() == -1)
.filter(sol -> sol.getSubmission().getId().equals(submissionId))
.collect(Collectors.toList() );
if (ReadSol.size() > 0){
Expand All @@ -181,6 +181,8 @@ public SolutionDTO addSolution(Long submissionId, SolutionDTO solutionDTO, Strin
long version = student.getSolutions()
.stream()
.filter(sol -> sol.getSubmission().getId().equals(submissionId))
.filter(sol -> sol.getVersion() != 0)
.filter(sol -> sol.getVersion() != -1)
.count();

solution.setSubmission(submission);
Expand Down Expand Up @@ -441,6 +443,7 @@ private Solution getLastSolVersion(Long submissionId, String studentId) {
blank.setStudent(studentRepo.getOne(studentId));
blank.setSubmission(submissionRepo.getOne(submissionId));
blank.setVersion(-1);
blank.setRevisable(true);
return solutionRepo.save(blank);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package it.polito.ai.project.services;

import it.polito.ai.project.dtos.CourseDTO;
import it.polito.ai.project.dtos.ProfessorDTO;
import it.polito.ai.project.dtos.StudentDTO;
import it.polito.ai.project.dtos.TeamDTO;
import it.polito.ai.project.dtos.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -22,7 +19,7 @@ public interface TeamService {
List<CourseDTO> getAllCourses();

// @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')")
boolean addStudent(StudentDTO student, MultipartFile file);
StudentDTO addStudent(UserDTO user, MultipartFile file);

@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROFESSOR')")
Optional<StudentDTO> getStudent(String studentId);
Expand Down Expand Up @@ -95,7 +92,7 @@ public interface TeamService {
void evictTeam(Long id);

// @PreAuthorize("hasRole('ROLE_ADMIN')")
boolean addProfessor(ProfessorDTO professor, MultipartFile file);
ProfessorDTO addProfessor(UserDTO user, MultipartFile file);

byte[] getImage(String username);

Expand All @@ -114,4 +111,6 @@ public interface TeamService {
@PreAuthorize("hasAnyRole('ROLE_STUDENT','ROLE_PROFESSOR','ROLE_ADMIN')")
TeamDTO getTeam(Long teamId);

@PreAuthorize("hasAnyRole('ROLE_STUDENT','ROLE_ADMIN', 'ROLE_PROFESSOR')")
List<ProfessorDTO> getAllProfessors();
}
Loading

0 comments on commit b174bd5

Please sign in to comment.