Fakultas Ilmu Komputer UI

Commit 7a80593e authored by RYAN PUTRA BUDIANTO's avatar RYAN PUTRA BUDIANTO
Browse files

Merge branch 'staging'

# Conflicts:
#	build.gradle
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/AdminController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/BaseController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/BookPageController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/CartController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/CuratorController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/ForgotPasswordController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/PaymentController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/ProfileController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/RegisterController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/controller/WishlistController.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/factory/BookOrderFactory.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/factory/OrderFactory.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/forms/CreateGenreForm.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/model/Articles.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/model/BookModel.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/model/Role.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/model/User.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/model/Wishlist.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/repository/BookOrderRepository.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/repository/CartRepository.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/repository/GenreRepository.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/repository/WishlistRepository.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/security/MvcConfig.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/security/RyanUserDetails.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/security/RyanUserDetailsService.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/security/SecurityConfig.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/AddressServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/ArticleService.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/ArticleServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/BookOrderServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/BookService.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/BookServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/CartServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/GenreService.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/GenreServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/PaymentServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/UserServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/WishlistService.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/service/WishlistServiceImpl.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/util/ImageConverter.java
#	src/main/java/id/ac/ui/cs/advprog/landiandfriends/util/Utility.java
#	src/main/resources/application.properties
#	src/main/resources/static/js/create-book.js
#	src/main/resources/static/js/edit-book.js
#	src/main/resources/static/js/styleart.css
#	src/main/resources/templates/address-create.html
#	src/main/resources/templates/address-update.html
#	src/main/resources/templates/admin-payment-list.html
#	src/main/resources/templates/article-create.html
#	src/main/resources/templates/articles.html
#	src/main/resources/templates/book-create.html
#	src/main/resources/templates/book-edit.html
#	src/main/resources/templates/book-list.html
#	src/main/resources/templates/book-page.html
#	src/main/resources/templates/curator-dashboard.html
#	src/main/resources/templates/home.html
#	src/main/resources/templates/my-address.html
#	src/main/resources/templates/payment-methods.html
#	src/main/resources/templates/profile-edit.html
#	src/main/resources/templates/profile.html
#	src/main/resources/templates/successBan.html
#	src/main/resources/templates/the-article.html
#	src/main/resources/templates/updateArticle.html
#	src/main/resources/templates/wishlist.html
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/controller/AdminControllerTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/controller/BaseControllerTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/controller/BookPageControllerTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/controller/CartControllerTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/controller/CuratorControllerTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/model/ArticlesTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/model/CartTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/ArticleServiceImplTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/BookOrderServiceTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/BookServiceTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/CartServiceTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/GenreServiceTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/PaymentServiceImplTest.java
#	src/test/java/id/ac/ui/cs/advprog/landiandfriends/service/UserServiceImplTest.java
parents df363ab2 65454c75
Pipeline #134372 failed with stages
in 4 minutes and 9 seconds
plugins {
id 'org.springframework.boot' version '2.6.4'
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'jacoco'
id "org.sonarqube" version "2.7"
}
apply plugin: "jacoco"
group = 'id.ac.ui.cs.advprog'
version = '0.0.1-SNAPSHOT'
......@@ -23,15 +26,45 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.security:spring-security-test'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'commons-fileupload:commons-fileupload:1.4'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-mail'
}
tasks.named('test') {
def allTestCoverageFile = "$buildDir/jacoco/allTestCoverage.exec"
sonarqube {
properties {
property "sonar.projectKey", "landi-and-friends_landi-and-friends-adpro_AYD9cPHVmTzPxwcesJDn"
property "sonar.jacoco.reportPaths", allTestCoverageFile
property "sonar.host.url", "https://sonarqube.cs.ui.ac.id"
property("sonar.coverage.exclusions",
[
"**/*Application**'"
])
}
}
test {
useJUnitPlatform()
}
jacocoTestReport {
reports {
xml.enabled true
}
}
plugins.withType(JacocoPlugin) {
tasks["test"].finalizedBy 'jacocoTestReport'
}
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.forms.RegisterForm;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.service.PaymentServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.security.Principal;
import java.util.List;
@Controller
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {
private final UserServiceImpl userService;
private final PaymentServiceImpl paymentService;
String usernameAttr = "username";
@GetMapping(path={"/list-user"})
public String allUser(Principal principal, Model model){
model.addAttribute(usernameAttr,principal != null ? principal.getName() : "");
List<User> allUsers = userService.allUsers();
model.addAttribute("all_users",allUsers);
return "listuser";
}
@PostMapping({"/ban/{username}"})
public String updateUser(@PathVariable("username") String username, RedirectAttributes redirectAttributes ){
userService.banUser(username);
redirectAttributes.addFlashAttribute(usernameAttr, username);
return "redirect:/admin/success";
}
@GetMapping({ "/success"})
public String success(){
return "successBan";
}
@GetMapping({"/register-admin", "/register-curator"})
public String registerWithRole(Model model){
model.addAttribute("form", new RegisterForm());
return "registerAdmin";
}
@PostMapping({ "/register-admin"})
public ResponseEntity<String> registerAdmin(@RequestBody RegisterForm user) {
var admin = userService.createUser(user.getEmail(), user.getUsername(), user.getPassword(), "ADMIN");
return ResponseEntity.ok(admin.getUsername());
}
@PostMapping({ "/register-curator"})
public ResponseEntity<String> registerCurator(@RequestBody RegisterForm user) {
var curator = userService.createUser(user.getEmail(), user.getUsername(), user.getPassword(), "CURATOR");
return ResponseEntity.ok(curator.getUsername());
}
@GetMapping(path="/list-payment")
public String paymentList(Model model, Principal principal){
model.addAttribute("username",principal.getName());
model.addAttribute("user", userService.getUserByName(principal.getName()));
model.addAttribute("payments", paymentService.getAllPayments());
return "admin-payment-list";
}
@PostMapping(path="/admin-delete-payment/{id}")
public String deletePayment(@PathVariable("id") int id) {
paymentService.deletePayment(id);
return "redirect:/admin/list-payment";
}
}
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateBookForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.RegisterForm;
import id.ac.ui.cs.advprog.landiandfriends.model.BookModel;
import id.ac.ui.cs.advprog.landiandfriends.model.UserModel;
import id.ac.ui.cs.advprog.landiandfriends.service.BookStoreServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserService;
import id.ac.ui.cs.advprog.landiandfriends.model.Articles;
import id.ac.ui.cs.advprog.landiandfriends.model.productcard.ArticleProductCard;
import id.ac.ui.cs.advprog.landiandfriends.model.productcard.BookProductCard;
import id.ac.ui.cs.advprog.landiandfriends.service.ArticleServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.security.Principal;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Controller
@RequiredArgsConstructor
public class BaseController {
private final BookStoreServiceImpl bookStoreService;
private final UserServiceImpl userService;
private final BookServiceImpl bookService;
private final ArticleServiceImpl articleService;
@Autowired
public BaseController(BookStoreServiceImpl bookStoreService, UserServiceImpl userService) {
this.bookStoreService = bookStoreService;
this.userService = userService;
}
private final static String usernameAttr = "username";
private final Logger logger = LoggerFactory.getLogger(BaseController.class);
@GetMapping(path = "/")
public String index(Model model) {
model.addAttribute("books", bookStoreService.getAllBookTitles());
public String getHomePage(Principal principal, Model model) {
model.addAttribute("products",
Stream.concat(
bookService.getRandomBooks(6).stream().map(BookProductCard::new),
articleService.getRandomArticles(6).stream().map(ArticleProductCard::new))
.collect(Collectors.toList()));
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
return "home";
}
@GetMapping(path="/create-book")
public String createBook(Model model){
model.addAttribute("form", new CreateBookForm());
return "book-create";
}
@GetMapping(path = "/articles")
public String getArticles(Model model,Principal principal) {
String username;
if (principal != null) {
model.addAttribute(usernameAttr, principal.getName());
username = principal.getName();
} else {
model.addAttribute(usernameAttr, "");
username = "";
}
List<Articles> allArticles = articleService.getAllArticles();
model.addAttribute("all_art",allArticles);
try{
model.addAttribute("user" , userService.getUserByName(username));
}
catch(Exception e){
logger.info("Not logged in.");
}
@GetMapping(path="/register")
public String register(Model model){
model.addAttribute("form", new RegisterForm());
return "register";
return "articles";
}
@GetMapping( "/articles/{id}")
public String getArticle(@PathVariable("id") Integer id , Model model,Principal principal ){
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
@PostMapping("/register")
public ResponseEntity<?> registerUser(@RequestBody RegisterForm user) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode(user.getPassword());
user.setPassword(encodedPassword);
var art = articleService.getArticle(id);
model.addAttribute("article",art);
return "the-article";
}
userService.createUser(user);
return ResponseEntity.ok(user);
@GetMapping(path = "/search/{title}")
public String getSearch(Principal principal, Model model, @PathVariable(name = "title")String title) {
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
model.addAttribute("searched",
Stream.concat(
bookService.getSearchedBooks(title)
.stream().map(BookProductCard::new),
articleService.getSearchedArticles(title)
.stream().map(ArticleProductCard::new)
).collect(Collectors.toList()));
return "home";
}
@PostMapping(path="/create-book")
public ResponseEntity<?> addBook(@RequestBody CreateBookForm form) {
BookModel book = bookStoreService.createBook(form.getTitle(),
form.getAuthor(), form.getDescription(), form.getStock());
bookStoreService.saveBook(book);
return ResponseEntity.ok(book);
}
}
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.security.Principal;
@Controller
@RequiredArgsConstructor
public class BookPageController {
private final BookServiceImpl bookService;
@GetMapping(path = "/book/{bookId}")
public String getBookPage(Principal principal, Model model, @PathVariable(name = "bookId")String bookId) {
model.addAttribute("username", (principal != null) ? principal.getName() : "");
var book = bookService.getBookById(bookId);
if(book == null){
return "redirect:/";
}
model.addAttribute("book", book);
return "book-page";
}
}
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.BookOrder;
import id.ac.ui.cs.advprog.landiandfriends.model.Cart;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.service.BookOrderServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.CartServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.security.Principal;
@Controller
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {
private final BookOrderServiceImpl bookOrderService;
private final CartServiceImpl cartService;
private final UserServiceImpl userService;
private final static String usernameAttr = "username";
@GetMapping(path = {"", "/"})
public String getCartPage(Principal principal, Model model) {
String username;
if (principal != null) {
model.addAttribute(usernameAttr, principal.getName());
username = principal.getName();
} else {
model.addAttribute(usernameAttr, "");
username = "";
}
var cart = cartService.findCartByUsername(username);
cartService.updateCart(cart);
model.addAttribute("cart", cart);
model.addAttribute("bookorders", cartService.getAllBookOrders(userService.getUserByName(username)));
return "cart";
}
@GetMapping(path = "/add/{bookId}")
public String addToCart(Principal principal, @PathVariable(name = "bookId") String bookId) {
var user = userService.getUserByName(principal.getName());
boolean firstAdd = bookOrderService.getBookOrderByBookAndCartId(bookId, cartService.findCartByUsername(principal.getName()).getCartId()) == null;
var bookOrder = bookOrderService.getBookOrder(bookId, cartService.findCartByUsername(principal.getName()));
cartService.addToCart(user,bookOrder,firstAdd);
return "redirect:/cart";
}
@GetMapping(path = "/increment/{bookOrderId}")
public String incrementCart(@PathVariable(name = "bookOrderId") String bookOrderId) {
var bookOrder = bookOrderService.getBookOrderByBookOrderId(Long.parseLong(bookOrderId));
return "redirect:/cart/add/" + Long.toString(bookOrder.getBook().getBookId());
}
@GetMapping(path = "/decrement/{bookOrderId}")
public String decrementCart(Principal principal, @PathVariable(name = "bookOrderId") String bookOrderId) {
var bookOrder = bookOrderService.getBookOrderByBookOrderId(Long.parseLong(bookOrderId));
bookOrderService.decrementBookOrder(bookOrder);
Cart cart = cartService.findCartByUsername(principal.getName());
cartService.updateCart(cart);
return "redirect:/cart";
}
@GetMapping(path = "/purchase")
public String purchaseCart(Principal principal, Model model) {
String username;
if (principal != null) {
model.addAttribute(usernameAttr, principal.getName());
username = principal.getName();
} else {
model.addAttribute(usernameAttr, "");
username = "";
}
var cart = cartService.findCartByUsername(username);
var user = userService.getUserByName(username);
cartService.updateCart(cart);
model.addAttribute("cart", cart);
model.addAttribute("bookorders", cartService.getAllBookOrders(userService.getUserByName(username)));
model.addAttribute("payments", userService.getPaymentMethods(user));
return "purchase";
}
@GetMapping(path = "/purchase/flr")
public String purchaseCartFailure(Principal principal, Model model) {
if (principal != null) {
model.addAttribute(usernameAttr, principal.getName());
} else {
model.addAttribute(usernameAttr, "");
}
return "purchase-fail";
}
@GetMapping(path = "/purchase/scs")
public String purchaseCartSuccess(Principal principal, Model model) {
String username;
if (principal != null) {
model.addAttribute(usernameAttr, principal.getName());
username = principal.getName();
} else {
model.addAttribute(usernameAttr, "");
username = "";
}
cartService.clearCart(cartService.findCartByUsername(username));
return "purchase-success";
}
}
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateArticleForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateBookForm;
import id.ac.ui.cs.advprog.landiandfriends.forms.CreateGenreForm;
import id.ac.ui.cs.advprog.landiandfriends.service.ArticleServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.GenreServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.Principal;
import java.util.List;
@Controller
@RequestMapping("/curator")
@RequiredArgsConstructor
public class CuratorController {
private final BookServiceImpl bookService;
private final GenreServiceImpl genreService;
private final ArticleServiceImpl articleService;
private final static String redirectArticle = "redirect:/articles";
private final static String genresAttr = "genres";
@GetMapping(path={"", "/"})
public String dashboard(Principal principal, Model model){
model.addAttribute("username", (principal != null) ? principal.getName() : "");
return "curator-dashboard";
}
@GetMapping(path={"/book-list/{page}", "/book-list"})
public String bookList(Principal principal, Model model, @PathVariable(required = false) Integer page){
model.addAttribute("username", (principal != null) ? principal.getName() : "");
if(page == null) page = 0;
model.addAttribute("books", bookService.getBookPage(page));
model.addAttribute("page", page);
return "book-list";
}
@GetMapping(path={"/create-book"})
public String createBook(Model model){
model.addAttribute("form", new CreateBookForm());
model.addAttribute(genresAttr, genreService.getAllGenres());
return "book-create";
}
@PostMapping(path={"/create-book"})
public ResponseEntity<String> addBook(@RequestParam MultipartFile image, @RequestParam String title, @RequestParam String author, @RequestParam String description, @RequestParam String stock, @RequestParam float price, @RequestParam List<String> genres) {
var form = new CreateBookForm(title, author, description,stock,genres,price);
try {
var book = bookService.createBook(form, image);
return ResponseEntity.ok(book.getBookId().toString());
} catch (Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@PostMapping(path={"/remove-book/{id}"})
public ResponseEntity<String> removeBook(@PathVariable String id) {
var book = bookService.removeBook(id);
if(book != null){
return ResponseEntity.ok(book.getBookId().toString());
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Book not found.");
}
@GetMapping(path={"/edit-book/{id}"})
public String editBookPage(@PathVariable String id, Model model) {
var book = bookService.getBookById(id);
model.addAttribute("book", book);
model.addAttribute(genresAttr, genreService.getAllGenres());
return "book-edit";
}
@PutMapping(path={"/edit-book/{id}"})
public ResponseEntity<String> modifyBook(@PathVariable String id, @RequestParam MultipartFile image, @RequestParam String title, @RequestParam String author, @RequestParam String description, @RequestParam String stock, @RequestParam float price, @RequestParam List<String> genres) {
var form = new CreateBookForm(title, author, description,stock,genres,price);
try {
var book = bookService.updateBook(id, form, image);
return ResponseEntity.ok(book.getBookId().toString());
} catch (Exception e){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
@GetMapping(path={ "/create-article"})
public String createArticle(Principal principal, Model model){
model.addAttribute("username", (principal != null) ? principal.getName() : "");
model.addAttribute("form", new CreateArticleForm());
return "article-create";
}
@PostMapping(path={"/create-article"})
public String addArticle(@RequestParam MultipartFile image,@RequestParam String title,@RequestParam String author,@RequestParam String desc) throws IOException {
articleService.createArticle(title,author,desc,image.getBytes());
return redirectArticle;
}
@GetMapping(path ={"/update/{id}"})
public String updateItem(@PathVariable("id") Integer id, Model model) {
model.addAttribute("article",articleService.getArticle(id));
return "updateArticle";
}
@PostMapping(path={"/update/{id}"})
public String updateArticle(@PathVariable(value = "id") Integer id, @RequestParam("image") final MultipartFile image,@RequestParam String title,@RequestParam String author,@RequestParam String desc) throws IOException {
articleService.updateArticle(id, title,author,desc,image.getBytes());
return redirectArticle;
}
@PostMapping(path={"/delete/{id}"})
public String deleteArticle(@PathVariable(value = "id") Integer id) {
articleService.deleteArticle(id);
return redirectArticle;
}
@GetMapping(path={"/genre-list/{page}", "/genre-list"})
public String genreList(Principal principal, Model model, @PathVariable(required = false) Integer page){
if(page == null) page = 0;
model.addAttribute("username", (principal != null) ? principal.getName() : "");
model.addAttribute(genresAttr, genreService.getGenrePage(page));
model.addAttribute("page", page);
return "genre-list";
}