Skip to content

Commit

Permalink
Merge branch 'main' into feat-new-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan0dyatlyukkk authored Oct 17, 2023
2 parents bd96014 + fe482d1 commit f0ea400
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
EXPOSE 8080
EXPOSE 8080
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.diatliuk.bookstore.service.ShoppingCartService;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -32,33 +33,35 @@ public class CartController {
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Get a shopping cart", description = "Allow to get all info "
+ "about a user's shopping cart")
public ShoppingCartDto get() {
return shoppingCartService.get();
public ShoppingCartDto get(Authentication authentication) {
return shoppingCartService.get(authentication);
}

@PostMapping
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Save a new cart item to the shopping cart", description = "Allow to save"
+ " a new cart item to the shopping cart")
public CartItemDto saveItem(@RequestBody @Valid CreateCartItemRequestDto createDto) {
return shoppingCartService.save(createDto);
public CartItemDto saveItem(Authentication authentication,
@RequestBody @Valid CreateCartItemRequestDto createDto) {
return shoppingCartService.save(authentication, createDto);
}

@PutMapping("cart-items/{cartItemId}")
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Update a cart item", description = "Allow to update a quantity of "
+ "the cart item by cart item id")
public CartItemDto updateItemQuantity(@PathVariable Long cartItemId,
@RequestBody @Valid UpdateCartItemDto updateDto) {
return shoppingCartService.update(cartItemId, updateDto);
public CartItemDto updateItemQuantity(Authentication authentication,
@PathVariable Long cartItemId,
@RequestBody @Valid UpdateCartItemDto updateDto) {
return shoppingCartService.update(authentication, cartItemId, updateDto);
}

@DeleteMapping("cart-items/{cartItemId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Delete a cart item", description = "Allow to delete "
+ "a particular cart item by its id")
public void deleteItemById(@PathVariable Long cartItemId) {
shoppingCartService.deleteById(cartItemId);
public void deleteItemById(Authentication authentication, @PathVariable Long cartItemId) {
shoppingCartService.deleteById(authentication, cartItemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.diatliuk.bookstore.dto.order.OrderDto;
import org.diatliuk.bookstore.dto.order.UpdateOrderStatusRequestDto;
import org.diatliuk.bookstore.dto.order.item.OrderItemDto;
import org.diatliuk.bookstore.service.OrderItemService;
import org.diatliuk.bookstore.service.OrderService;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -26,6 +28,7 @@
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
private final OrderItemService orderItemService;

@GetMapping
@PreAuthorize("hasRole('ROLE_USER')")
Expand All @@ -37,16 +40,17 @@ public List<OrderDto> getAll(Pageable pageable) {
@PostMapping
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Place an order", description = "Allow to place an order")
public OrderDto save(@RequestBody @Valid CreateOrderRequestDto requestDto) {
return orderService.save(requestDto);
public OrderDto save(Authentication authentication,
@RequestBody @Valid CreateOrderRequestDto requestDto) {
return orderService.save(authentication, requestDto);
}

@GetMapping("/{id}/items")
@PreAuthorize("hasRole('ROLE_USER')")
@Operation(summary = "Get order items by order id", description = "Allow to get all order "
+ "items in a certain order by an order id")
public List<OrderItemDto> getOrderItemsByOrderId(@PathVariable Long id) {
return orderService.getItemsByOrderId(id);
return orderItemService.getItemsByOrderId(id);
}

@GetMapping("/{orderId}/items/{itemId}")
Expand All @@ -55,7 +59,7 @@ public List<OrderItemDto> getOrderItemsByOrderId(@PathVariable Long id) {
description = "Allow to get a certain order item by specifying "
+ "an order id and an item id")
public OrderItemDto getItemInOrderById(@PathVariable Long orderId, @PathVariable Long itemId) {
return orderService.getItemInOrderById(orderId, itemId);
return orderItemService.getItemInOrderById(orderId, itemId);
}

@PatchMapping("/{id}")
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/diatliuk/bookstore/dto/book/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
@Data
public class BookDto {
private Long id;

@Schema(example = "Moby-Dick")
private String title;

@Schema(example = "Joseph Heller")
private String author;

@Schema(example = "978-161-729-045-9")
private String isbn;

private BigDecimal price;
private String description;
private String coverImage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
public class BookSearchParametersDto {
@Schema(example = "The Great Gatsby")
private String[] title;

@Schema(example = "F. Scott Fitzgerald")
private String[] author;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@ public class CreateBookRequestDto {
@NotBlank
@Schema(example = "The Clean Code")
private String title;

@NotNull
@NotBlank
@Schema(example = "Robert C. Martin")
private String author;

@NotNull
@ISBN
@Schema(example = "978-161-729-045-9")
private String isbn;

@NotNull
@Min(0)
@Schema(example = "210.0")
private BigDecimal price;

@Schema(example = "The book description")
private String description;

@Schema(example = "https://example.com/updated-cover-image.jpg")
private String coverImage;

private Set<Long> categoryIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
public class CreateCartItemRequestDto {
@NotNull
private Long bookId;

@NotNull
@Min(1)
private Integer quantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class CategoryDto {
@NotBlank
@Schema(example = "Fiction")
private String name;

@Schema(example = "Fiction books")
private String description;
}
5 changes: 3 additions & 2 deletions src/main/java/org/diatliuk/bookstore/model/CartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -24,13 +25,13 @@ public class CartItem {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shopping_cart_id", nullable = false)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private ShoppingCart shoppingCart;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", nullable = false)
@ToString.Exclude
@EqualsAndHashCode.Exclude
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/diatliuk/bookstore/model/OrderItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -25,13 +26,13 @@ public class OrderItem {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id", nullable = false)
@ToString.Exclude
@EqualsAndHashCode.Exclude
private Order order;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", nullable = false)
@ToString.Exclude
@EqualsAndHashCode.Exclude
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package org.diatliuk.bookstore.repository.cart;

import java.util.List;
import java.util.Optional;
import org.diatliuk.bookstore.model.CartItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CartItemRepository extends JpaRepository<CartItem, Long> {
@Query("FROM CartItem ci LEFT JOIN FETCH ci.book "
+ "LEFT JOIN FETCH ci.shoppingCart WHERE ci.id = :id")
Optional<CartItem> findById(Long id);

@Query("FROM CartItem ci LEFT JOIN FETCH ci.book "
+ "LEFT JOIN FETCH ci.shoppingCart WHERE ci.shoppingCart.id = :id")
List<CartItem> findAllByShoppingCartId(Long id);

boolean existsByIdAndShoppingCartId(Long id, Long shoppingCartId);
}
10 changes: 10 additions & 0 deletions src/main/java/org/diatliuk/bookstore/service/OrderItemService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.diatliuk.bookstore.service;

import java.util.List;
import org.diatliuk.bookstore.dto.order.item.OrderItemDto;

public interface OrderItemService {
List<OrderItemDto> getItemsByOrderId(Long id);

OrderItemDto getItemInOrderById(Long orderId, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import org.diatliuk.bookstore.dto.order.CreateOrderRequestDto;
import org.diatliuk.bookstore.dto.order.OrderDto;
import org.diatliuk.bookstore.dto.order.UpdateOrderStatusRequestDto;
import org.diatliuk.bookstore.dto.order.item.OrderItemDto;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;

public interface OrderService {
OrderDto save(CreateOrderRequestDto requestDto);
OrderDto save(Authentication authentication, CreateOrderRequestDto requestDto);

List<OrderDto> getAll(Pageable pageable);

OrderDto update(Long id, UpdateOrderStatusRequestDto requestDto);

List<OrderItemDto> getItemsByOrderId(Long id);

OrderItemDto getItemInOrderById(Long orderId, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
import org.diatliuk.bookstore.dto.cart.item.UpdateCartItemDto;
import org.diatliuk.bookstore.model.ShoppingCart;
import org.diatliuk.bookstore.model.User;
import org.springframework.security.core.Authentication;

public interface ShoppingCartService {
ShoppingCartDto get();
ShoppingCartDto get(Authentication authentication);

CartItemDto save(CreateCartItemRequestDto requestDto);
CartItemDto save(Authentication authentication, CreateCartItemRequestDto requestDto);

CartItemDto update(Long cartItemId, UpdateCartItemDto quantity);
CartItemDto update(Authentication authentication, Long cartItemId, UpdateCartItemDto quantity);

void deleteById(Long cartItemId);
void deleteById(Authentication authentication, Long cartItemId);

ShoppingCart create(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import org.diatliuk.bookstore.dto.user.UserResponseDto;
import org.diatliuk.bookstore.exception.RegistrationException;
import org.diatliuk.bookstore.model.User;
import org.springframework.security.core.Authentication;

public interface UserService {
UserResponseDto register(UserRegistrationRequestDto requestDto) throws RegistrationException;

User getAuthenticatedUser();
User getAuthenticatedUser(Authentication authentication);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.diatliuk.bookstore.service.impl;

import jakarta.transaction.Transactional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.diatliuk.bookstore.dto.book.BookDto;
Expand All @@ -22,6 +23,7 @@ public class BookServiceImpl implements BookService {
private final BookMapper bookMapper;
private final SpecificationBuilder<Book> specificationBuilder;

@Transactional
@Override
public BookDto save(CreateBookRequestDto requestDto) {
Book book = bookMapper.toModel(requestDto);
Expand All @@ -42,6 +44,7 @@ public BookDto getById(Long id) {
return bookMapper.toDto(book);
}

@Transactional
@Override
public BookDto update(Long id, CreateBookRequestDto bookDto) {
Book updatedBook = bookMapper.toModel(bookDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.diatliuk.bookstore.service.impl;

import jakarta.transaction.Transactional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.diatliuk.bookstore.dto.book.BookDtoWithoutCategoryIds;
Expand All @@ -23,6 +24,7 @@ public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;
private final CategoryMapper categoryMapper;

@Transactional
@Override
public CategoryResponseDto save(CategoryDto categoryDto) {
Category category = categoryMapper.toModel(categoryDto);
Expand All @@ -44,6 +46,7 @@ public CategoryDto getById(Long id) {
return categoryMapper.toDto(category);
}

@Transactional
@Override
public CategoryDto update(Long id, CategoryDto categoryDto) {
Category category = categoryMapper.toModel(categoryDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.diatliuk.bookstore.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.diatliuk.bookstore.dto.order.item.OrderItemDto;
import org.diatliuk.bookstore.exception.EntityNotFoundException;
import org.diatliuk.bookstore.mapper.OrderItemMapper;
import org.diatliuk.bookstore.model.OrderItem;
import org.diatliuk.bookstore.repository.order.OrderItemRepository;
import org.diatliuk.bookstore.service.OrderItemService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class OrderItemServiceImpl implements OrderItemService {
private final OrderItemRepository orderItemRepository;
private final OrderItemMapper orderItemMapper;

@Override
public List<OrderItemDto> getItemsByOrderId(Long id) {
return orderItemRepository.getOrderItemsByOrder_Id(id)
.stream()
.map(orderItemMapper::toDto)
.collect(Collectors.toList());
}

@Override
public OrderItemDto getItemInOrderById(Long orderId, Long id) {
OrderItem item = orderItemRepository.getOrderItemsByOrder_Id(orderId)
.stream().filter(orderItem -> orderItem.getId().equals(id))
.findAny()
.orElseThrow(() -> new EntityNotFoundException("Can't find an orderItem with id: "
+ id + " in the order with id: " + orderId));
return orderItemMapper.toDto(item);
}
}
Loading

0 comments on commit f0ea400

Please sign in to comment.