@RequestMapping(value = "/add", method = RequestMethod.POST) public ResponseEntity<?> addBook( Principal principal, @RequestParam("title") String title, @RequestParam("intro") String intro, @RequestParam("categories") String[] categories, @RequestParam("file") MultipartFile file) { BookDTO bookDTO = new BookDTO(title, intro, file, categories); Set<ConstraintViolation<BookDTO>> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(bookDTO); if (violations.size() > 0 || !file.getContentType().equals("application/pdf")) return new ResponseEntity<>(HttpStatus.BAD_REQUEST); Book book = new Book(); book.setTitle(title); book.setIntroduction(intro); book.setCategories(categories); try { book.setContent(file.getBytes()); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } book.setPublished_date(new Date()); book.setTotal_pages(100); book.setUser(userRepo.findOneByEmail(principal.getName())); if (bookRepo.addBook(book) && bookRepo.addBookHasCategory(book)) return new ResponseEntity<>(HttpStatus.ACCEPTED); else return new ResponseEntity<>(HttpStatus.BAD_REQUEST); }
@RequestMapping(value = "/downvote/{id}") public ResponseEntity<?> downvote(Principal principal, @PathVariable int id) { if (bookRepo.addVote(id, userRepo.findOneByEmail(principal.getName()).getId(), false)) return new ResponseEntity<>(HttpStatus.ACCEPTED); else return new ResponseEntity<>(HttpStatus.BAD_REQUEST); }
@RequestMapping(value = "updatePage", method = RequestMethod.GET) public ResponseEntity<?> changePage( Principal principal, @RequestParam(value = "bookId", required = true) int bookId, @RequestParam(value = "pageNum", required = true) int pageNum) { String email = principal.getName(); User user = userRepo.findOneByEmail(email); int id = user.getId(); if (bookRepo.checkUserReadsStatus(id, bookId)) return bookRepo.updateUserReads(pageNum, bookId, id) ? new ResponseEntity<Object>(HttpStatus.CREATED) : new ResponseEntity<Object>(HttpStatus.BAD_REQUEST); else if (bookRepo.insertUserReadsBook(id, bookId, pageNum)) return new ResponseEntity<>(HttpStatus.CREATED); else return new ResponseEntity<>(HttpStatus.BAD_REQUEST); }
@RequestMapping(value = "/my", method = RequestMethod.GET) public List<Book> myBooks(Principal principal) { return bookRepo.findBooksByUserId(userRepo.findOneByEmail(principal.getName()).getId()); }
@RequestMapping(value = "/favs", method = RequestMethod.GET) public List<Book> myFavs(Principal principal) { return bookRepo.findBooksByUserVote(userRepo.findOneByEmail(principal.getName()).getId(), true); }
@RequestMapping(value = "/lastPage/{bookId}", method = RequestMethod.GET) public @ResponseBody int getLastPageRead(Principal principal, @PathVariable int bookId) { return bookRepo.lastPageRead(bookId, userRepo.findOneByEmail(principal.getName()).getId()); }