initial commit
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Lurkars
|
||||
*
|
||||
*/
|
||||
@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;
|
||||
|
||||
@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.directFetchByRanking(
|
||||
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;
|
||||
}
|
||||
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("ranked")
|
||||
public Page<Entry> rankedEntries(@RequestParam("page") Optional<Integer> pageParameter,
|
||||
@RequestParam("size") Optional<Integer> sizeParameter) {
|
||||
|
||||
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
|
||||
sizeParameter = Optional.of(100);
|
||||
}
|
||||
|
||||
Page<Entry> entries = entryManager.fetchByRanking(pageParameter.orElse(0),
|
||||
sizeParameter.orElse(SIZE));
|
||||
|
||||
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
|
||||
return entries;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user