refactor naming + user

This commit is contained in:
_Bastler 2021-10-04 16:57:20 +02:00
parent 29cd81b093
commit 59321f4a3d
13 changed files with 218 additions and 125 deletions

View File

@ -71,20 +71,40 @@ public class CommentManager {
* @param date the date
* @param page the page
* @param size the size
* @param desc the desc
* @return the page
*/
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size) {
public Page<Comment> fetchByDate(Long target, Long parent, Instant date, int page, int size,
boolean desc) {
Sort sort = Sort.by(desc ? Order.desc("created") : Order.asc("created"));
if (parent == null) {
return commentRepository.findAll(
return commentRepository
.findAll(
qComment.target.eq(target).and(qComment.parent.isNull())
.and(qComment.created.before(date)),
PageRequest.of(page, size, Sort.by(Order.asc("created"))));
PageRequest.of(page, size, sort));
}
return commentRepository.findAll(
qComment.target.eq(target).and(qComment.parent.eq(parent))
.and(qComment.created.before(date)),
PageRequest.of(page, size, Sort.by(Order.asc("created"))));
return commentRepository.findAll(qComment.target.eq(target).and(qComment.parent.eq(parent))
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
}
/**
* Fetch by username.
*
* @param username the username
* @param orElse the or else
* @param date the date
* @param page the page
* @param size the size
* @param asc the asc
* @return the page
*/
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return commentRepository.findAll(qComment.author.eq(username).and(qComment.parent.isNull())
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
}
/**

View File

@ -50,10 +50,8 @@ public class EntryManager {
@Value("${bstly.board.ranking.gravity:1.8}")
private double GRAVITY;
/**
* Direct fetch by ranking.
* Fetch by ranking.
*
* @param date the date
* @param gravity the gravity
@ -61,8 +59,7 @@ public class EntryManager {
* @param size the size
* @return the page
*/
public Page<RankedEntry> fetchByRanking(Instant date, double gravity, int page,
int size) {
public Page<RankedEntry> fetchByRanking(Instant date, double gravity, int page, int size) {
return entryRepository.findAllByRanking(date, gravity, PageRequest.of(page, size));
}
@ -80,12 +77,28 @@ public class EntryManager {
}
/**
* Gets the entries.
* Fetch by user.
*
* @param username the username
* @param date the date
* @param page the page
* @param size the size
* @param asc the asc
* @return the page
*/
public Page<Entry> fetchByUser(String username, Instant date, int page, int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return entryRepository.findAll(qEntry.author.eq(username).and(qEntry.created.before(date)),
PageRequest.of(page, size, sort));
}
/**
* Fetch by bookmarks.
*
* @param username the username
* @param page the page
* @param size the size
* @return the entries
* @return the page
*/
public Page<Entry> fetchByBookmarks(String username, int page, int size) {
Bookmarks bookmarks = bookmarksManager.get(username);

View File

@ -64,6 +64,12 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @see org.springframework.security.core.userdetails.UserDetailsService#
* loadUserByUsername(java.lang.String)
*/
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
*/
/*
* @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
*/
/*
* @see
* de.bstly.board.businesslogic.LocalUserManager#loadUserByUsername(java.lang.
@ -91,6 +97,12 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
/*
*
* @see org.springframework.beans.factory.SmartInitializingSingleton#

View File

@ -27,7 +27,7 @@ import de.bstly.board.model.Entry;
* The Class BookmarksController.
*/
@RestController
@RequestMapping("/b")
@RequestMapping("/bookmarks")
public class BookmarksController extends BaseController {
@Autowired

View File

@ -29,36 +29,30 @@ import de.bstly.board.model.Types;
import de.bstly.board.model.Vote;
import de.bstly.board.model.VoteType;
/**
* The Class CommentController.
*/
@RestController
@RequestMapping("/c")
@RequestMapping("/comments")
public class CommentController extends BaseController {
@Autowired
private CommentManager commentManager;
@Autowired
private CommentValidator commentValidator;
@Autowired
private VoteManager voteManager;
@Value("${bstly.board.size:30}")
private int SIZE;
@Value("${bstly.board.ranking.gravity:1.8}")
private double GRAVITY;
/**
* Ranked comments.
* Fetch by rank.
*
* @param target the target
* @param parent the parent
@ -69,23 +63,70 @@ public class CommentController extends BaseController {
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/e/{target}", "/e/{target}/{parent}" })
public Page<Comment> rankedComments(@PathVariable("target") Long target,
@GetMapping({ "/{target}", "/{target}/{parent}" })
public Page<Comment> fetchByRank(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter) {
Page<Comment> comments = newComments(target, parent, pageParameter, sizeParameter,
dateParameter);
Page<Comment> comments = fetchByDate(target, parent, pageParameter, sizeParameter,
dateParameter, Optional.of(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
return comments;
}
// Page<Comment> comments = commentManager.fetchByRanking(target, parent.orElse(null),
// dateParameter.orElse(Instant.now()), gravityParameter.orElse(GRAVITY),
// pageParameter.orElse(0), sizeParameter.orElse(SIZE));
// return comments;
/**
* Fetch by date.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param descParameter the desc parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/new/{target}", "/new/{target}/{parent}" })
public Page<Comment> fetchByDate(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("desc") Optional<Boolean> descParameter) {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), descParameter.orElse(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
return comments;
}
/**
* Fetch by username.
*
* @param username the username
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ascParameter the asc parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/byuser/{username}" })
public Page<Comment> fetchByUsername(@PathVariable("username") String username,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("asc") Optional<Boolean> ascParameter) {
Page<Comment> comments = commentManager.fetchByUsername(username, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
return comments;
}
/**
@ -96,36 +137,12 @@ public class CommentController extends BaseController {
* @return the long
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/c/{target}", "/c/{target}/{parent}" })
@GetMapping({ "/count/{target}", "/count/{target}/{parent}" })
public Long countComments(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent) {
return commentManager.count(target, parent.orElse(null));
}
/**
* New comments.
*
* @param target the target
* @param parent the parent
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "/e/new/{target}", "/e/new/{target}/{parent}" })
public Page<Comment> newComments(@PathVariable("target") Long target,
@PathVariable("parent") Optional<Long> parent,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter) {
Page<Comment> comments = commentManager.fetchByDate(target, parent.orElse(null),
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE));
commentManager.applyMetadata(getCurrentUsername(), comments.getContent());
return comments;
}
/**
* Gets the comment.
*
@ -133,7 +150,7 @@ public class CommentController extends BaseController {
* @return the comment
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/{id}")
@GetMapping("/comment/{id}")
public Comment getComment(@PathVariable("id") Long id) {
Comment comment = commentManager.get(id);
@ -153,7 +170,7 @@ public class CommentController extends BaseController {
* @return the comment
*/
@PreAuthorize("isAuthenticated()")
@PostMapping()
@PostMapping
public Comment createComment(@RequestBody Comment comment) {
RequestBodyErrors bindingResult = new RequestBodyErrors(comment);
commentValidator.validate(comment, bindingResult);

View File

@ -37,7 +37,7 @@ import de.bstly.board.model.VoteType;
* The Class EntryController.
*/
@RestController
@RequestMapping("/e")
@RequestMapping("/entries")
public class EntryController extends BaseController {
@Autowired
@ -56,7 +56,7 @@ public class EntryController extends BaseController {
private double GRAVITY;
/**
* Ranked entries.
* Fetch by ranking.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
@ -66,7 +66,7 @@ public class EntryController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@GetMapping()
public Page<Entry> rankedEntries(@RequestParam("page") Optional<Integer> pageParameter,
public Page<Entry> fetchByRanking(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("gravity") Optional<Double> gravityParameter) {
@ -89,7 +89,7 @@ public class EntryController extends BaseController {
}
/**
* New entries.
* Fetch by date.
*
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
@ -98,7 +98,7 @@ public class EntryController extends BaseController {
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/new")
public Page<Entry> newEntries(@RequestParam("page") Optional<Integer> pageParameter,
public Page<Entry> fetchByDate(@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter) {
@ -112,6 +112,35 @@ public class EntryController extends BaseController {
return entries;
}
/**
* Fetch by user.
*
* @param username the username
* @param pageParameter the page parameter
* @param sizeParameter the size parameter
* @param dateParameter the date parameter
* @param ascParameter the asc parameter
* @return the page
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/byuser/{username}")
public Page<Entry> fetchByUser(@PathVariable("username") String username,
@RequestParam("page") Optional<Integer> pageParameter,
@RequestParam("size") Optional<Integer> sizeParameter,
@RequestParam("date") Optional<Instant> dateParameter,
@RequestParam("asc") Optional<Boolean> ascParameter) {
if (sizeParameter.isPresent() && sizeParameter.get() > 100) {
sizeParameter = Optional.of(100);
}
Page<Entry> entries = entryManager.fetchByUser(username,
dateParameter.orElse(Instant.now()), pageParameter.orElse(0),
sizeParameter.orElse(SIZE), ascParameter.orElse(false));
entryManager.applyMetadata(getCurrentUsername(), entries.getContent());
return entries;
}
/**
* Gets the entry.
*
@ -119,7 +148,7 @@ public class EntryController extends BaseController {
* @return the entry
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/{id}")
@GetMapping("/entry/{id}")
public Entry getEntry(@PathVariable("id") Long id) {
Entry entry = entryManager.get(id);

View File

@ -25,7 +25,7 @@ import de.bstly.board.model.LocalUser;
* The Class ModerationController.
*/
@RestController
@RequestMapping("/m")
@RequestMapping("/moderation")
public class ModerationController {
@ -46,7 +46,7 @@ public class ModerationController {
* @param id the id
*/
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
@DeleteMapping("/c/{id}")
@DeleteMapping("/comment/{id}")
public void deleteComment(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -61,7 +61,7 @@ public class ModerationController {
* @param id the id
*/
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
@DeleteMapping("/e/{id}")
@DeleteMapping("/entry/{id}")
public void deleteEntry(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -76,7 +76,7 @@ public class ModerationController {
* @param username the username
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("/u/{username}")
@PutMapping("/user/{username}")
public void makeMod(@PathVariable("username") String username) {
LocalUser user = userManager.getByUsername(username);
@ -103,7 +103,7 @@ public class ModerationController {
* @param username the username
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("/u/{username}")
@DeleteMapping("/user/{username}")
public void unmakeMode(@PathVariable("username") String username) {
LocalUser user = userManager.getByUsername(username);

View File

@ -24,7 +24,7 @@ import de.bstly.board.model.LocalUser;
* The Class UserController.
*/
@RestController
@RequestMapping("/u")
@RequestMapping("/users")
public class UserController extends BaseController {
@ -38,7 +38,7 @@ public class UserController extends BaseController {
* @return the user
*/
@PreAuthorize("isAuthenticated()")
@GetMapping({ "", "/{username}" })
@GetMapping({ "/user", "/user/{username}" })
public LocalUser getUser(@PathVariable("username") Optional<String> usernameParameter) {
String username = usernameParameter.orElse(getCurrentUsername());
@ -68,7 +68,7 @@ public class UserController extends BaseController {
* @return the local user
*/
@PreAuthorize("isAuthenticated()")
@PostMapping()
@PostMapping("/user")
public LocalUser updateUser(@RequestBody LocalUser user) {
if (!getCurrentUsername().equals(user.getUsername())) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);

View File

@ -26,7 +26,7 @@ import de.bstly.board.model.VoteType;
* The Class VoteController.
*/
@RestController
@RequestMapping("/v")
@RequestMapping("/votes")
public class VoteController extends BaseController {
@ -48,7 +48,7 @@ public class VoteController extends BaseController {
* @return the entry points
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/e/{id}")
@GetMapping("/entry/{id}")
public long getEntryPoints(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -63,7 +63,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@PutMapping("/e/{id}/up")
@PutMapping("/entry/{id}/up")
public void voteEntryUp(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -89,7 +89,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@PutMapping("/e/{id}/down")
@PutMapping("/entry/{id}/down")
public void voteEntryDown(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -115,7 +115,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@DeleteMapping("/e/{id}")
@DeleteMapping("/entry/{id}")
public void unvoteEntry(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -136,7 +136,7 @@ public class VoteController extends BaseController {
* @return the comment points
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/c/{id}")
@GetMapping("/comment/{id}")
public long getCommentPoints(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -151,7 +151,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@PutMapping("/c/{id}/up")
@PutMapping("/comment/{id}/up")
public void voteCommentUp(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -177,7 +177,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@PutMapping("/c/{id}/down")
@PutMapping("/comment/{id}/down")
public void voteCommentDown(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
@ -203,7 +203,7 @@ public class VoteController extends BaseController {
* @param id the id
*/
@PreAuthorize("isAuthenticated()")
@DeleteMapping("/c/{id}")
@DeleteMapping("/comment/{id}")
public void unvoteComment(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);

View File

@ -36,14 +36,16 @@ public class Bookmarks {
private List<Long> entries;
/**
* @param username
* Instantiates a new bookmarks.
*/
public Bookmarks() {
super();
}
/**
* @param username
* Instantiates a new bookmarks.
*
* @param username the username
*/
public Bookmarks(String username) {
super();