Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "User management", description = "Endpoints for managing user")
Expand All @@ -24,14 +26,15 @@ public class AuthenticationController {

private final AuthenticationService authenticationService;

@PostMapping("/registration")
@Operation(summary = "Create new user", description = "Create a new user")
@PostMapping("/registration")
@ResponseStatus(HttpStatus.CREATED)
public UserResponseDto register(@RequestBody @Valid UserRegistrationRequestDto request) {
return userService.save(request);
}

@PostMapping("/login")
@Operation(summary = "User login", description = "Endpoint for user login")
@PostMapping("/login")
public UserLoginResponseDto login(@RequestBody @Valid UserLoginRequestDto request) {
return authenticationService.authenticate(request);
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/origin/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,47 @@
public class BookController {
private final BookService bookService;

@PreAuthorize("hasRole('USER')")
@GetMapping
@Operation(summary = "Get all books", description = "Get a list of all available books")
@GetMapping
@PreAuthorize("hasRole('USER')")
public Page<BookDto> getAll(Pageable pageable) {
return bookService.findAll(pageable);
}

@PreAuthorize("hasRole('USER')")
@GetMapping("/{id}")
@Operation(summary = "Get book by id", description = "Get a book by id")
@GetMapping("/{id}")
@PreAuthorize("hasRole('USER')")
public BookDto getBookById(@PathVariable Long id) {
return bookService.findById(id);
}

@PreAuthorize("hasRole('ADMIN')")
@PostMapping
@Operation(summary = "Create book", description = "Create a new book")
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
@ResponseStatus(HttpStatus.CREATED)
public BookDto createBook(@RequestBody @Valid CreateBookRequestDto bookDto) {
return bookService.save(bookDto);
}

@Operation(summary = "Delete book by id", description = "Delete a book by id")
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
@Operation(summary = "Delete book by id", description = "Delete a book by id")
public void deleteBookById(@PathVariable Long id) {
bookService.deleteById(id);
}

@PreAuthorize("hasRole('ADMIN')")
@PutMapping("/{id}")
@Operation(summary = "Update book by id", description = "Update a book by id")
@PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public BookDto updateBook(@PathVariable Long id,
@RequestBody @Valid CreateBookRequestDto bookDto) {
return bookService.updateBook(id, bookDto);
}

@PreAuthorize("hasRole('USER')")
@GetMapping("/search")
@Operation(summary = "Search books by parameters", description = "Search books by parameters")
@GetMapping("/search")
@PreAuthorize("hasRole('USER')")
public Page<BookDto> searchBooks(BookSearchParameters searchParameters, Pageable pageable) {
return bookService.search(searchParameters, pageable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,50 @@
public class CategoryController {
private final CategoryService categoryService;

@PreAuthorize("hasRole('ADMIN')")
@Operation(summary = "Create a category", description = "Create a category")
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Create a category", description = "Create a category")
public CategoryDto createCategory(
@RequestBody @Valid
CreateCategoryRequestDto createCategoryRequestDto) {
return categoryService.save(createCategoryRequestDto);
}

@PreAuthorize("hasRole('USER')")
@GetMapping
@Operation(summary = "Get all categories", description = "Get all categories")
@GetMapping
@PreAuthorize("hasRole('USER')")
public Page<CategoryDto> getAll(Pageable pageable) {
return categoryService.findAll(pageable);
}

@PreAuthorize("hasRole('USER')")
@GetMapping("/{id}")
@Operation(summary = "Get category by id", description = "Get category by id")
@GetMapping("/{id}")
@PreAuthorize("hasRole('USER')")
public CategoryDto getCategoryById(@PathVariable Long id) {
return categoryService.getById(id);
}

@PreAuthorize("hasRole('ADMIN')")
@PutMapping("/{id}")
@Operation(summary = "Update category by id", description = "Update category by id")
@PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public CategoryDto updateCategory(@PathVariable Long id,
@RequestBody @Valid
CreateCategoryRequestDto createCategoryRequestDto) {
return categoryService.update(id, createCategoryRequestDto);
}

@PreAuthorize("hasRole('ADMIN')")
@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
@Operation(summary = "Delete category by id", description = "Delete category by id")
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasRole('ADMIN')")
public void deleteCategory(@PathVariable Long id) {
categoryService.deleteById(id);
}

@PreAuthorize("hasRole('USER')")
@GetMapping("/{id}/books")
@Operation(summary = "Get books by category id", description = "Get books by category id")
@GetMapping("/{id}/books")
@PreAuthorize("hasRole('USER')")
public Page<BookDto> getBooksByCategoryId(@PathVariable Long id, Pageable pageable) {
return categoryService.getBooksByCategoryId(id, pageable);
}
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/origin/bookstore/controller/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -21,60 +22,60 @@
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Orders managing", description = "Endpoints for managing orders")
@Tag(name = "Orders management", description = "Endpoints for managing orders")
@RestController
@RequiredArgsConstructor
@RequestMapping("/orders")
public class OrderController {
private final OrderService orderService;

@PreAuthorize("hasRole('USER')")
@Operation(summary = "Create order", description = "Create order")
@PostMapping
@PreAuthorize("hasRole('USER')")
@ResponseStatus(HttpStatus.CREATED)
public OrderResponseDto createOrder(
@AuthenticationPrincipal User user,
@RequestBody @Valid OrderRequestDto orderRequestDto
) {
return orderService.save(user, orderRequestDto);
@RequestBody @Valid OrderRequestDto requestDto) {
return orderService.save(user, requestDto);
}

@PreAuthorize("hasRole('USER')")
@Operation(summary = "Get all orders", description = "Get all orders")
@GetMapping
@PreAuthorize("hasRole('USER')")
public Page<OrderResponseDto> getAllOrders(
@AuthenticationPrincipal User user, Pageable pageable) {
return orderService.getAllOrders(user, pageable);
}

@PreAuthorize("hasRole('USER')")
@Operation(summary = "Get all order items in order",
description = "Get all order items in order")
@GetMapping("/{orderId}/items")
@PreAuthorize("hasRole('USER')")
public List<OrderItemResponseDto> getAllOrderItemsInOrder(
@AuthenticationPrincipal User user,
@PathVariable Long orderId) {
return orderService.getAllOrderItems(user, orderId);
}

@PreAuthorize("hasRole('USER')")
@Operation(summary = "Get order item by id", description = "Get order item by id")
@GetMapping("/{orderId}/items/{id}")
@PreAuthorize("hasRole('USER')")
public OrderItemResponseDto getOrderItemById(
@AuthenticationPrincipal User user,
@PathVariable Long orderId,
@PathVariable(name = "id") Long itemId) {
return orderService.getOrderItemById(user, orderId, itemId);
}

@PreAuthorize("hasRole('ADMIN')")
@Operation(summary = "Update order status by id", description = "Update order status by id")
@PatchMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public OrderResponseDto updateOrderStatusById(
@PathVariable Long id,
@RequestBody
@Valid UpdateOrderStatusRequestDto updateOrderStatusRequestDto) {
@RequestBody @Valid UpdateOrderStatusRequestDto updateOrderStatusRequestDto) {
return orderService.updateOrderStatus(id, updateOrderStatusRequestDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@
public class ShoppingCartController {
private final ShoppingCartService shoppingCartService;

@Operation(summary = "Add book to cart", description = "Add book to shopping cart")
@PostMapping
@PreAuthorize("hasRole('USER')")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Add book to cart", description = "Add book to shopping cart")
public ShoppingCartResponseDto addBookToCart(
@RequestBody @Valid CartItemRequestDto cartItemRequestDto,
@AuthenticationPrincipal User user) {
return shoppingCartService.addBookToCart(user,
cartItemRequestDto);
}

@GetMapping
@PreAuthorize("hasRole('USER')")
@Operation(summary = "Get items from shopping cart",
description = "Get item list from shopping cart")
@GetMapping
@PreAuthorize("hasRole('USER')")
public ShoppingCartResponseDto getCartItems(
@AuthenticationPrincipal User user) {
return shoppingCartService.getShoppingCartByUserId(user);
}

@PutMapping("/items/{cartItemId}")
@PreAuthorize("hasRole('USER')")
@Operation(summary = "Update the books quantity in shopping cart",
description = "Update the books quantity in shopping cart")
@PutMapping("/items/{cartItemId}")
@PreAuthorize("hasRole('USER')")
public ShoppingCartResponseDto updateBookQuantityInCart(
@PathVariable Long cartItemId,
@RequestBody @Valid UpdateCartItemRequestDto updateCartItemRequestDto,
Expand All @@ -61,11 +61,11 @@ public ShoppingCartResponseDto updateBookQuantityInCart(
cartItemId, updateCartItemRequestDto);
}

@Operation(summary = "Delete a book from shopping cart",
description = "Delete a book from shopping cart")
@DeleteMapping("/items/{cartItemId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasRole('USER')")
@Operation(summary = "Delete a book from shopping cart",
description = "Delete a book from shopping cart")
public void deleteBookFromCart(@PathVariable Long cartItemId,
@AuthenticationPrincipal User user) {
shoppingCartService.deleteBookFromCart(user, cartItemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.URL;

@Getter
@Setter
@Builder
public class CreateBookRequestDto {
@NotBlank
@Size(min = 1, max = 100)
@NotBlank(message = "Title cannot be blank")
@Size(min = 1, max = 100, message = "Title must be between 1 and 100 characters")
private String title;
@NotBlank
@Size(min = 1, max = 100)
@NotBlank(message = "Author cannot be blank")
@Size(min = 1, max = 10, message = "Author must be between 1 and 100 characters")
private String author;
@NotBlank
@Pattern(regexp = "\\d{3}-\\d{10}")
@NotBlank(message = "ISBN cannot be blank")
@Pattern(regexp = "\\d{3}-\\d{10}", message = "Invalid ISBN format")
private String isbn;
@NotNull
@DecimalMin(value = "0", inclusive = true)
@NotNull(message = "Price cannot be null")
@DecimalMin(value = "0", inclusive = true, message = "Price must be positive")
private BigDecimal price;
@Size(max = 1000, message = "Description is too long")
private String description;
@URL(message = "Invalid cover image URL format")
private String coverImage;
@NotEmpty
@NotEmpty(message = "At least one category ID must be provided")
private Set<Long> categoryIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import jakarta.validation.constraints.Positive;

public record CartItemRequestDto(
@NotNull
@Positive
@NotNull(message = "Book id cannot be null")
@Positive(message = "Book id must be positive")
Long bookId,
@Positive
@Positive(message = "Book quantity must be positive")
int quantity
) {}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import jakarta.validation.constraints.Min;

public record UpdateCartItemRequestDto(
@Min(1)
@Min(value = 1, message = "Quantity must be in minimum of 1")
int quantity
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import jakarta.validation.constraints.Size;

public record CreateCategoryRequestDto(
@NotBlank
@NotBlank(message = "Category name cannot be null")
@Size(min = 1, max = 100)
String name,
String description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import jakarta.validation.constraints.NotBlank;

public record OrderRequestDto(
@NotBlank
@NotBlank(message = "Order shipping address cannot be null")
String shippingAddress
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import jakarta.validation.constraints.NotEmpty;

public record UpdateOrderStatusRequestDto(
@NotEmpty
@NotEmpty(message = "Order status cannot be null")
String status
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import jakarta.validation.constraints.Size;

public record UserLoginRequestDto(
@NotBlank
@Size(min = 5, max = 20)
@Email
@NotBlank(message = "Email cannot be null")
@Size(min = 5, max = 100, message = "Email must be between 5 and 20 characters")
@Email(message = "Incorrect email format")
String email,
@NotBlank
@Size(min = 3, max = 20)
@NotBlank(message = "Password cannot be null")
@Size(min = 8, max = 30, message = "Password must be between 8 and 20 characters")
String password
) { }
Loading
Loading