168 lines
4.9 KiB
Java
168 lines
4.9 KiB
Java
/**
|
|
*
|
|
*/
|
|
package de.bstly.board.controller;
|
|
|
|
import java.time.Instant;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageImpl;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import de.bstly.board.businesslogic.EntryManager;
|
|
import de.bstly.board.businesslogic.VoteManager;
|
|
import de.bstly.board.controller.support.EntityResponseStatusException;
|
|
import de.bstly.board.controller.support.RequestBodyErrors;
|
|
import de.bstly.board.controller.validation.EntryValidator;
|
|
import de.bstly.board.model.Entry;
|
|
import de.bstly.board.model.EntryStatus;
|
|
import de.bstly.board.model.RankedEntry;
|
|
import de.bstly.board.model.Types;
|
|
import de.bstly.board.model.Vote;
|
|
import de.bstly.board.model.VoteType;
|
|
|
|
/**
|
|
* The Class EntryController.
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/e")
|
|
public class EntryController extends BaseController {
|
|
|
|
@Autowired
|
|
private EntryManager entryManager;
|
|
|
|
@Autowired
|
|
private EntryValidator entryValidator;
|
|
|
|
@Autowired
|
|
private VoteManager voteManager;
|
|
|
|
@Value("${bstly.board.size:30}")
|
|
private int SIZE;
|
|
|
|
@Value("${bstly.board.ranking.gravity:1.8}")
|
|
private double GRAVITY;
|
|
|
|
/**
|
|
* Ranked entries.
|
|
*
|
|
* @param pageParameter the page parameter
|
|
* @param sizeParameter the size parameter
|
|
* @param dateParameter the date parameter
|
|
* @param gravityParameter the gravity parameter
|
|
* @return the page
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping()
|
|
public Page<Entry> rankedEntries(@RequestParam("page") Optional<Integer> pageParameter,
|
|
@RequestParam("size") Optional<Integer> sizeParameter,
|
|
@RequestParam("date") Optional<Instant> dateParameter,
|
|
@RequestParam("gravity") Optional<Double> gravityParameter) {
|
|
|
|
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
|
|
sizeParameter = Optional.of(100);
|
|
}
|
|
|
|
Page<RankedEntry> entries = entryManager.fetchByRanking(dateParameter.orElse(Instant.now()),
|
|
gravityParameter.orElse(GRAVITY), pageParameter.orElse(0),
|
|
sizeParameter.orElse(SIZE));
|
|
|
|
Page<Entry> transformed = new PageImpl<Entry>(
|
|
entries.getContent().stream().map(rankedEntry -> Entry.fromRankedEntry(rankedEntry))
|
|
.collect(Collectors.toList()),
|
|
entries.getPageable(), entries.getTotalElements());
|
|
|
|
entryManager.applyMetadata(getCurrentUsername(), transformed.getContent());
|
|
return transformed;
|
|
}
|
|
|
|
/**
|
|
* New entries.
|
|
*
|
|
* @param pageParameter the page parameter
|
|
* @param sizeParameter the size parameter
|
|
* @param dateParameter the date parameter
|
|
* @return the page
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping("/new")
|
|
public Page<Entry> newEntries(@RequestParam("page") Optional<Integer> pageParameter,
|
|
@RequestParam("size") Optional<Integer> sizeParameter,
|
|
@RequestParam("date") Optional<Instant> dateParameter) {
|
|
|
|
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
|
|
sizeParameter = Optional.of(100);
|
|
}
|
|
|
|
Page<Entry> entries = entryManager.fetchByDate(dateParameter.orElse(Instant.now()),
|
|
pageParameter.orElse(0), sizeParameter.orElse(SIZE));
|
|
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
|
|
return entries;
|
|
}
|
|
|
|
/**
|
|
* Gets the entry.
|
|
*
|
|
* @param id the id
|
|
* @return the entry
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@GetMapping("/{id}")
|
|
public Entry getEntry(@PathVariable("id") Long id) {
|
|
Entry entry = entryManager.get(id);
|
|
|
|
if (entry == null) {
|
|
throw new EntityResponseStatusException(HttpStatus.NOT_FOUND);
|
|
}
|
|
|
|
entryManager.applyMetadata(getCurrentUsername(), entry);
|
|
|
|
return entry;
|
|
}
|
|
|
|
/**
|
|
* Creates the entry.
|
|
*
|
|
* @param entry the entry
|
|
* @return the entry
|
|
*/
|
|
@PreAuthorize("isAuthenticated()")
|
|
@PostMapping()
|
|
public Entry createEntry(@RequestBody Entry entry) {
|
|
RequestBodyErrors bindingResult = new RequestBodyErrors(entry);
|
|
entryValidator.validate(entry, bindingResult);
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
throw new EntityResponseStatusException(bindingResult.getAllErrors(),
|
|
HttpStatus.UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
entry.setCreated(Instant.now());
|
|
entry.setAuthor(getCurrentUsername());
|
|
entry.setEntryStatus(EntryStatus.NORMAL);
|
|
entry = entryManager.save(entry);
|
|
|
|
Vote vote = new Vote();
|
|
vote.setTarget(entry.getId());
|
|
vote.setType(VoteType.up);
|
|
vote.setTargetType(Types.entry);
|
|
vote.setAuthor(getCurrentUsername());
|
|
voteManager.save(vote);
|
|
|
|
return entry;
|
|
}
|
|
|
|
}
|