Skip to content
Snippets Groups Projects

Add list of featured puzzles to user's profile

Files

@@ -17,7 +17,13 @@
*/
package nl.tudelft.pupple.controller;
import java.time.LocalDate;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
@@ -29,23 +35,33 @@ import nl.tudelft.labracore.api.PersonControllerApi;
import nl.tudelft.labracore.api.dto.PersonDetailsDTO;
import nl.tudelft.labracore.lib.security.user.AuthenticatedPerson;
import nl.tudelft.labracore.lib.security.user.Person;
import nl.tudelft.pupple.controller.utility.PageUtil;
import nl.tudelft.pupple.model.Puzzle;
import nl.tudelft.pupple.model.labracore.PupplePerson;
import nl.tudelft.pupple.repository.labracore.*;
import nl.tudelft.pupple.repository.PuzzleRepository;
import nl.tudelft.pupple.repository.labracore.PersonRepository;
import nl.tudelft.pupple.security.AuthorisationService;
import nl.tudelft.pupple.service.PuzzleAttemptService;
@Controller
@RequestMapping("profile")
public class PersonController {
private final PersonRepository personRepository;
private final PuzzleRepository puzzleRepository;
private final AuthorisationService authorisationService;
private final PuzzleAttemptService puzzleAttemptService;
private final PersonControllerApi personControllerApi;
@Autowired
public PersonController(PersonRepository personRepository,
PuzzleRepository puzzleRepository,
AuthorisationService authorisationService,
PuzzleAttemptService puzzleAttemptService,
PersonControllerApi personControllerApi) {
this.personRepository = personRepository;
this.puzzleRepository = puzzleRepository;
this.authorisationService = authorisationService;
this.puzzleAttemptService = puzzleAttemptService;
this.personControllerApi = personControllerApi;
}
@@ -57,18 +73,51 @@ public class PersonController {
@GetMapping("{id}")
@PreAuthorize("@authorisationService.isAuthenticated()")
public String getProfile(@AuthenticatedPerson Person person, @PathVariable("id") long personId,
public String getProfile(@AuthenticatedPerson Person person,
@PageableDefault(value = 7) Pageable pageable,
@PathVariable("id") long personId,
@RequestParam(name = "unassigned", required = false) String unassigned,
Model model) {
PupplePerson pupplePerson = personRepository.findByIdOrThrow(personId);
if (authorisationService.isAdmin() || person.getId() == personId || pupplePerson.getPublicProfile()) {
PersonDetailsDTO otherPerson = personControllerApi.getPersonById(personId).block();
if (!(authorisationService.isAdmin() || person.getId() == personId
|| pupplePerson.getPublicProfile())) {
return "profile/forbidden";
}
PersonDetailsDTO otherPerson = personControllerApi.getPersonById(personId).block();
model.addAttribute("displayName", otherPerson.getDisplayName());
model.addAttribute("pupplePerson", pupplePerson);
model.addAttribute("isOwner", person.getId() == personId);
return "profile/view";
List<Puzzle> puzzlesFeatured;
boolean isOwner = person.getId() == personId;
boolean canViewFuturePuzzles = isOwner || authorisationService.isAdmin();
if (unassigned != null) {
if (canViewFuturePuzzles) {
puzzlesFeatured = puzzleRepository.findByDayUsedIsNullAndCreator(pupplePerson);
} else {
puzzlesFeatured = new ArrayList<>();
}
} else {
if (canViewFuturePuzzles) {
puzzlesFeatured = puzzleRepository.findByCreator(pupplePerson);
} else {
puzzlesFeatured = puzzleRepository.findByDayUsedIsBeforeAndCreator(LocalDate
.now().plusDays(1), pupplePerson);
}
}
return "profile/forbidden";
puzzlesFeatured.sort(Comparator
.comparing(Puzzle::getDayUsed, Comparator.nullsFirst(Comparator.naturalOrder()))
.reversed());
Page<Puzzle> puzzlePage = PageUtil.pageFromList(puzzlesFeatured, pageable);
Set<Puzzle> puzzlesSolved = new HashSet<>(puzzleAttemptService
.filterSolvedPuzzles(person, puzzlesFeatured));
model.addAttribute("displayName", otherPerson.getDisplayName());
model.addAttribute("pupplePerson", pupplePerson);
model.addAttribute("solved", puzzlesSolved);
model.addAttribute("puzzles", puzzlePage);
model.addAttribute("isOwner", isOwner);
return "profile/view";
}
@PostMapping("{id}")
Loading