Fakultas Ilmu Komputer UI

Commit fc25873e authored by MUHAMMAD AAQIL ABDULLAH's avatar MUHAMMAD AAQIL ABDULLAH
Browse files

Fix code smells

parent e876153e
Pipeline #134250 canceled with stages
in 10 minutes and 41 seconds
......@@ -54,13 +54,13 @@ public class AdminController {
@PostMapping({ "/register-admin"})
public ResponseEntity<String> registerAdmin(@RequestBody RegisterForm user) {
User admin = userService.createUser(user.getEmail(), user.getUsername(), user.getPassword(), "ADMIN");
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) {
User curator = userService.createUser(user.getEmail(), user.getUsername(), user.getPassword(), "CURATOR");
var curator = userService.createUser(user.getEmail(), user.getUsername(), user.getPassword(), "CURATOR");
return ResponseEntity.ok(curator.getUsername());
}
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Articles;
import id.ac.ui.cs.advprog.landiandfriends.model.Role;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
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.repository.UserRepository;
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.CartServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
......@@ -32,7 +28,7 @@ public class BaseController {
private final BookServiceImpl bookService;
private final ArticleServiceImpl articleService;
private final String USERNAME_ATTR = "username";
private final static String usernameAttr = "username";
private final Logger logger = LoggerFactory.getLogger(BaseController.class);
@GetMapping(path = "/")
......@@ -44,7 +40,7 @@ public class BaseController {
bookService.getRandomBooks(6).stream().map(BookProductCard::new),
articleService.getRandomArticles(6).stream().map(ArticleProductCard::new))
.collect(Collectors.toList()));
model.addAttribute(USERNAME_ATTR, (principal != null) ? principal.getName() : "");
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
return "home";
}
......@@ -52,7 +48,7 @@ public class BaseController {
@GetMapping(path = "/articles")
@Timed("allarticles")
public String getArticles(Model model,Principal principal) {
model.addAttribute(USERNAME_ATTR, (principal != null) ? principal.getName() : "");
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
List<Articles> allArticles = articleService.getAllArticles();
model.addAttribute("all_art",allArticles);
......@@ -67,7 +63,7 @@ public class BaseController {
}
@GetMapping( "/articles/{id}")
public String getArticle(@PathVariable("id") Integer id , Model model,Principal principal ){
model.addAttribute(USERNAME_ATTR, (principal != null) ? principal.getName() : "");
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
var art = articleService.getArticle(id);
model.addAttribute("article",art);
......@@ -78,7 +74,7 @@ public class BaseController {
@GetMapping(path = "/search/{title}")
@Timed("search")
public String getSearch(Principal principal, Model model, @PathVariable(name = "title")String title) {
model.addAttribute(USERNAME_ATTR, (principal != null) ? principal.getName() : "");
model.addAttribute(usernameAttr, (principal != null) ? principal.getName() : "");
model.addAttribute("searched",
Stream.concat(
bookService.getSearchedBooks(title)
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.productcard.BookProductCard;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
......@@ -10,7 +9,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.security.Principal;
import java.util.stream.Collectors;
@Controller
@RequiredArgsConstructor
......@@ -22,7 +20,7 @@ public class BookPageController {
public String getBookPage(Principal principal, Model model, @PathVariable(name = "bookId")String bookId) {
model.addAttribute("username", (principal != null) ? principal.getName() : "");
Book book = bookService.getBookById(bookId);
var book = bookService.getBookById(bookId);
if(book == null){
return "redirect:/";
}
......
......@@ -24,15 +24,17 @@ 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) {
if (principal != null) {
model.addAttribute("username", principal.getName());
model.addAttribute(usernameAttr, principal.getName());
} else {
model.addAttribute("username", "");
model.addAttribute(usernameAttr, "");
}
Cart cart = cartService.findCartByUsername(principal.getName());
var cart = cartService.findCartByUsername(principal.getName());
cartService.updateCart(cart);
model.addAttribute("cart", cart);
model.addAttribute("bookorders", cartService.getAllBookOrders(userService.getUserByName(principal.getName())));
......@@ -41,22 +43,22 @@ public class CartController {
@GetMapping(path = "/add/{bookId}")
public String addToCart(Principal principal, @PathVariable(name = "bookId") String bookId) {
User user = userService.getUserByName(principal.getName());
var user = userService.getUserByName(principal.getName());
boolean firstAdd = bookOrderService.getBookOrderByBookAndCartId(bookId, cartService.findCartByUsername(principal.getName()).getCartId()) == null;
BookOrder bookOrder = bookOrderService.getBookOrder(bookId, cartService.findCartByUsername(principal.getName()));
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) {
BookOrder bookOrder = bookOrderService.getBookOrderByBookOrderId(Long.parseLong(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) {
BookOrder bookOrder = bookOrderService.getBookOrderByBookOrderId(Long.parseLong(bookOrderId));
var bookOrder = bookOrderService.getBookOrderByBookOrderId(Long.parseLong(bookOrderId));
bookOrderService.decrementBookOrder(bookOrder);
Cart cart = cartService.findCartByUsername(principal.getName());
cartService.updateCart(cart);
......@@ -67,12 +69,12 @@ public class CartController {
@Timed("purchase")
public String purchaseCart(Principal principal, Model model) {
if (principal != null) {
model.addAttribute("username", principal.getName());
model.addAttribute(usernameAttr, principal.getName());
} else {
model.addAttribute("username", "");
model.addAttribute(usernameAttr, "");
}
Cart cart = cartService.findCartByUsername(principal.getName());
User user = userService.getUserByName(principal.getName());
var cart = cartService.findCartByUsername(principal.getName());
var user = userService.getUserByName(principal.getName());
cartService.updateCart(cart);
model.addAttribute("cart", cart);
model.addAttribute("bookorders", cartService.getAllBookOrders(userService.getUserByName(principal.getName())));
......@@ -83,9 +85,9 @@ public class CartController {
@GetMapping(path = "/purchase/flr")
public String purchaseCartFailure(Principal principal, Model model) {
if (principal != null) {
model.addAttribute("username", principal.getName());
model.addAttribute(usernameAttr, principal.getName());
} else {
model.addAttribute("username", "");
model.addAttribute(usernameAttr, "");
}
return "purchase-fail";
}
......@@ -93,9 +95,9 @@ public class CartController {
@GetMapping(path = "/purchase/scs")
public String purchaseCartSuccess(Principal principal, Model model) {
if (principal != null) {
model.addAttribute("username", principal.getName());
model.addAttribute(usernameAttr, principal.getName());
} else {
model.addAttribute("username", "");
model.addAttribute(usernameAttr, "");
}
cartService.clearCart(cartService.findCartByUsername(principal.getName()));
return "purchase-success";
......
......@@ -28,7 +28,8 @@ public class CuratorController {
private final BookServiceImpl bookService;
private final GenreServiceImpl genreService;
private final ArticleServiceImpl articleService;
private final String REDIRECT_ARTICLE = "redirect:/articles";
private final static String redirectArticle = "redirect:/articles";
private final static String genresAttr = "genres";
@GetMapping(path={"", "/"})
public String dashboard(Principal principal, Model model){
......@@ -48,13 +49,13 @@ public class CuratorController {
@GetMapping(path={"/create-book"})
public String createBook(Model model){
model.addAttribute("form", new CreateBookForm());
model.addAttribute("genres", genreService.getAllGenres());
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) {
CreateBookForm form = new CreateBookForm(title, author, description,stock,genres,price);
var form = new CreateBookForm(title, author, description,stock,genres,price);
try {
var book = bookService.createBook(form, image);
return ResponseEntity.ok(book.getBookId().toString());
......@@ -76,13 +77,13 @@ public class CuratorController {
public String editBookPage(@PathVariable String id, Model model) {
var book = bookService.getBookById(id);
model.addAttribute("book", book);
model.addAttribute("genres", genreService.getAllGenres());
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) {
CreateBookForm form = new CreateBookForm(title, author, description,stock,genres,price);
var form = new CreateBookForm(title, author, description,stock,genres,price);
try {
var book = bookService.updateBook(id, form, image);
return ResponseEntity.ok(book.getBookId().toString());
......@@ -101,7 +102,7 @@ public class CuratorController {
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 REDIRECT_ARTICLE;
return redirectArticle;
}
@GetMapping(path ={"/update/{id}"})
......@@ -114,7 +115,7 @@ public class CuratorController {
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 REDIRECT_ARTICLE;
return redirectArticle;
}
@PostMapping(path={"/delete/{id}"})
......@@ -122,7 +123,7 @@ public class CuratorController {
articleService.deleteArticle(id);
return REDIRECT_ARTICLE;
return redirectArticle;
}
......@@ -130,14 +131,14 @@ public class CuratorController {
@Timed("genrelist")
public String genreList(Model model, @PathVariable(required = false) Integer page){
if(page == null) page = 0;
model.addAttribute("genres", genreService.getGenrePage(page));
model.addAttribute(genresAttr, genreService.getGenrePage(page));
model.addAttribute("page", page);
return "genre-list";
}
@PostMapping(path={"/create-genre"})
public ResponseEntity<String> createGenre(@RequestBody CreateGenreForm form) {
Genre genre = genreService.createGenre(form.getName());
var genre = genreService.createGenre(form.getName());
return ResponseEntity.ok().body(genre.getName());
}
......
......@@ -23,7 +23,7 @@ import java.io.UnsupportedEncodingException;
@Controller
public class ForgotPasswordController {
private final String MESSAGE_ATTR = "message";
private final static String messageAttr = "message";
@Autowired
private JavaMailSender mailSender;
......@@ -46,7 +46,7 @@ public class ForgotPasswordController {
userService.updateResetPasswordToken(token, email);
String resetPasswordLink = Utility.getSiteURL(request) + "/reset_password?token=" + token;
sendEmail(email, resetPasswordLink);
model.addAttribute(MESSAGE_ATTR, "We have sent a reset password link to your email. Please check.");
model.addAttribute(messageAttr, "We have sent a reset password link to your email. Please check.");
} catch (UsernameNotFoundException ex) {
model.addAttribute("error", ex.getMessage());
} catch (Exception e) {
......@@ -89,8 +89,8 @@ public class ForgotPasswordController {
model.addAttribute("token", token);
if (userModel == null) {
model.addAttribute(MESSAGE_ATTR, "Invalid Token");
return MESSAGE_ATTR;
model.addAttribute(messageAttr, "Invalid Token");
return messageAttr;
}
return "reset_password_form";
......@@ -105,11 +105,11 @@ public class ForgotPasswordController {
model.addAttribute("title", "Reset your password");
if (user == null) {
model.addAttribute(MESSAGE_ATTR, "Invalid Token");
return MESSAGE_ATTR;
model.addAttribute(messageAttr, "Invalid Token");
return messageAttr;
}
userService.updatePassword(user, password);
model.addAttribute(MESSAGE_ATTR, "You have successfully changed your password.");
return MESSAGE_ATTR;
model.addAttribute(messageAttr, "You have successfully changed your password.");
return messageAttr;
}
}
......@@ -16,7 +16,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.exception.AccountAlreadyExistsException;
import id.ac.ui.cs.advprog.landiandfriends.forms.RegisterForm;
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.model.Wishlist;
import id.ac.ui.cs.advprog.landiandfriends.service.CartServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.UserServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.WishlistServiceImpl;
......
package id.ac.ui.cs.advprog.landiandfriends.controller;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import id.ac.ui.cs.advprog.landiandfriends.repository.WishlistRepository;
import id.ac.ui.cs.advprog.landiandfriends.security.RyanUserDetails;
import id.ac.ui.cs.advprog.landiandfriends.service.BookServiceImpl;
import id.ac.ui.cs.advprog.landiandfriends.service.WishlistServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.security.Principal;
@Controller
@RequiredArgsConstructor
......@@ -33,7 +25,7 @@ public class WishlistController {
@GetMapping(path= "/add-wishlist/{id}")
public String addWishlist(@PathVariable("id") String id) {
Book book = bookService.getBookById(id);
var book = bookService.getBookById(id);
wishlistService.addItems(book);
return "redirect:/book/{id}";
}
......
......@@ -5,19 +5,18 @@ 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.Order;
import id.ac.ui.cs.advprog.landiandfriends.repository.BookOrderRepository;
import id.ac.ui.cs.advprog.landiandfriends.repository.BookRepository;
import id.ac.ui.cs.advprog.landiandfriends.service.BookService;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class BookOrderFactory extends OrderFactory<Book> {
public class BookOrderFactory implements OrderFactory<Book> {
private final BookService bookService;
private final BookOrderRepository bookOrderRepository;
@Override
public Order<Book> generateOrder(Book book, Cart cart) {
BookOrder bookOrder = new BookOrder();
var bookOrder = new BookOrder();
bookOrder.setBook(book);
bookOrder.setAmount(1);
bookOrder.setTotalPrice(book.getPrice() * bookOrder.getAmount());
......
......@@ -3,8 +3,8 @@ package id.ac.ui.cs.advprog.landiandfriends.factory;
import id.ac.ui.cs.advprog.landiandfriends.model.Cart;
import id.ac.ui.cs.advprog.landiandfriends.model.Order;
public abstract class OrderFactory<T> {
public interface OrderFactory<T> {
public abstract Order<T> generateOrder(T item, Cart cart);
Order<T> generateOrder(T item, Cart cart);
}
......@@ -3,7 +3,6 @@ package id.ac.ui.cs.advprog.landiandfriends.forms;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.util.List;
import lombok.Generated;
......
......@@ -2,7 +2,6 @@ package id.ac.ui.cs.advprog.landiandfriends.model;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Type;
import javax.persistence.*;
......
......@@ -12,7 +12,7 @@ import java.util.List;
@Setter
@NoArgsConstructor
@Table(name = "user_model")
public class User implements Serializable {
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
......
......@@ -16,7 +16,7 @@ public class Wishlist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "wishlist_id")
private Long wishlist_id;
private Long wishlistId;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(
......
package id.ac.ui.cs.advprog.landiandfriends.repository;
import id.ac.ui.cs.advprog.landiandfriends.model.Book;
import id.ac.ui.cs.advprog.landiandfriends.model.BookOrder;
import id.ac.ui.cs.advprog.landiandfriends.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
......
package id.ac.ui.cs.advprog.landiandfriends.repository;
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 org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
......
......@@ -11,7 +11,7 @@ import javax.transaction.Transactional;
@Repository
public interface WishlistRepository extends JpaRepository<Wishlist, String> {
// get wishlist by id
@Query("SELECT w FROM Wishlist w WHERE w.wishlist_id = ?1")
@Query("SELECT w FROM Wishlist w WHERE w.wishlistId = ?1")
Wishlist getWishlistById(Long id);
@Query(value = "SELECT * FROM user_wishlist WHERE user_id = ?1", nativeQuery = true)
......
package id.ac.ui.cs.advprog.landiandfriends.security;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.context.annotation.Configuration;
......
......@@ -10,7 +10,7 @@ import java.util.Collection;
public class RyanUserDetails implements UserDetails {
private final User user;
private transient final User user;
public RyanUserDetails(User user){
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment