diff --git a/src/main/java/com/example/springbootbookshop/controller/BookController.java b/src/main/java/com/example/springbootbookshop/controller/BookController.java index 920dcfe..46d665e 100644 --- a/src/main/java/com/example/springbootbookshop/controller/BookController.java +++ b/src/main/java/com/example/springbootbookshop/controller/BookController.java @@ -28,7 +28,7 @@ @Tag(name = "Book management",description = "Endpoints for managing books") @RestController @Validated -@RequestMapping(value = "/books") +@RequestMapping("/books") public class BookController { private final BookService bookService; @@ -41,7 +41,7 @@ public List getAll(@PageableDefault(size = 5, page = 0) return bookService.findAll(pageable); } - @DeleteMapping(value = "/{id}") + @DeleteMapping("/{id}") @PreAuthorize("hasRole('ADMIN')") @Operation(summary = "Delete book by id") @ResponseStatus(HttpStatus.NO_CONTENT) @@ -49,7 +49,7 @@ public void deleteBookById(@PathVariable @Positive Long id) { bookService.deleteById(id); } - @GetMapping(value = "/{id}") + @GetMapping("/{id}") @Operation(summary = "Get book by Id from DB") @PreAuthorize("hasRole('USER')") @ResponseStatus(HttpStatus.OK) @@ -57,7 +57,7 @@ public BookDto getBookById(@PathVariable @Positive Long id) { return bookService.getBookById(id); } - @PutMapping(value = "/{id}") + @PutMapping("/{id}") @PreAuthorize("hasRole('ADMIN')") @Operation(summary = "Update book by Id in DB") @ResponseStatus(HttpStatus.OK) diff --git a/src/main/java/com/example/springbootbookshop/controller/CartController.java b/src/main/java/com/example/springbootbookshop/controller/CartController.java new file mode 100644 index 0000000..4e22c87 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/controller/CartController.java @@ -0,0 +1,73 @@ +package com.example.springbootbookshop.controller; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.cart.item.CartItemDto; +import com.example.springbootbookshop.dto.cart.item.RequestCartItemDto; +import com.example.springbootbookshop.dto.cart.item.UpdateRequestCartItemDto; +import com.example.springbootbookshop.entity.User; +import com.example.springbootbookshop.service.CartService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import jakarta.validation.constraints.Positive; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +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.PutMapping; +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; + +@RequiredArgsConstructor +@Tag(name = "Cart management",description = "Endpoints for managing carts") +@RestController +@Validated +@RequestMapping("/cart") +public class CartController { + private final CartService cartService; + + @GetMapping + @Operation(summary = "Get cart by user id from DB") + @PreAuthorize("hasRole('USER')") + public CartDto getUserCart(Authentication authentication) { + return cartService.findById(getUserId(authentication)); + } + + @PostMapping + @Operation(summary = "Add books to the user cart") + @PreAuthorize("hasRole('USER')") + public CartItemDto addItemToCart(Authentication authentication, + @Valid @RequestBody RequestCartItemDto cartItem) { + return cartService.addItemToCart(getUserId(authentication), cartItem); + } + + @DeleteMapping("cart-items/{id}") + @Operation(summary = "Delete cart item from the user cart") + @PreAuthorize("hasRole('USER')") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void removeCartItem(Authentication authentication, + @PathVariable @Positive Long id) { + cartService.removeItem(getUserId(authentication), id); + } + + @PutMapping("cart-items/{id}") + @Operation(summary = "Update cart item in the user cart") + @PreAuthorize("hasRole('USER')") + @ResponseStatus(HttpStatus.OK) + public CartItemDto updateItemQuantity(@PathVariable @Positive Long id, + @RequestBody @Valid UpdateRequestCartItemDto quantity) { + return cartService.updateCartItem(id, quantity); + } + + private Long getUserId(Authentication authentication) { + User user = (User) authentication.getPrincipal(); + return user.getId(); + } +} diff --git a/src/main/java/com/example/springbootbookshop/dto/cart/CartDto.java b/src/main/java/com/example/springbootbookshop/dto/cart/CartDto.java new file mode 100644 index 0000000..c679204 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/dto/cart/CartDto.java @@ -0,0 +1,9 @@ +package com.example.springbootbookshop.dto.cart; + +import com.example.springbootbookshop.dto.cart.item.CartItemDto; +import java.util.Set; + +public record CartDto(Long id, + Long userId, + Set cartItems) { +} diff --git a/src/main/java/com/example/springbootbookshop/dto/cart/item/CartItemDto.java b/src/main/java/com/example/springbootbookshop/dto/cart/item/CartItemDto.java new file mode 100644 index 0000000..1637b28 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/dto/cart/item/CartItemDto.java @@ -0,0 +1,7 @@ +package com.example.springbootbookshop.dto.cart.item; + +public record CartItemDto(Long id, + Long bookId, + String bookTitle, + Integer quantity){ +} diff --git a/src/main/java/com/example/springbootbookshop/dto/cart/item/RequestCartItemDto.java b/src/main/java/com/example/springbootbookshop/dto/cart/item/RequestCartItemDto.java new file mode 100644 index 0000000..a4bbb51 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/dto/cart/item/RequestCartItemDto.java @@ -0,0 +1,7 @@ +package com.example.springbootbookshop.dto.cart.item; + +import jakarta.validation.constraints.Positive; + +public record RequestCartItemDto(@Positive Long bookId, + @Positive Integer quantity) { +} diff --git a/src/main/java/com/example/springbootbookshop/dto/cart/item/UpdateRequestCartItemDto.java b/src/main/java/com/example/springbootbookshop/dto/cart/item/UpdateRequestCartItemDto.java new file mode 100644 index 0000000..274ea6f --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/dto/cart/item/UpdateRequestCartItemDto.java @@ -0,0 +1,6 @@ +package com.example.springbootbookshop.dto.cart.item; + +import jakarta.validation.constraints.Positive; + +public record UpdateRequestCartItemDto(@Positive Integer quantity) { +} diff --git a/src/main/java/com/example/springbootbookshop/entity/Book.java b/src/main/java/com/example/springbootbookshop/entity/Book.java index 1030e11..897dcf5 100644 --- a/src/main/java/com/example/springbootbookshop/entity/Book.java +++ b/src/main/java/com/example/springbootbookshop/entity/Book.java @@ -1,5 +1,6 @@ package com.example.springbootbookshop.entity; +import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -8,9 +9,12 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import lombok.Data; import lombok.EqualsAndHashCode; @@ -49,4 +53,7 @@ public class Book { @ToString.Exclude @EqualsAndHashCode.Exclude private Set categories = new HashSet<>(); + + @OneToMany(mappedBy = "book",orphanRemoval = true, cascade = CascadeType.REMOVE) + private List cartItems = new ArrayList<>(); } diff --git a/src/main/java/com/example/springbootbookshop/entity/Cart.java b/src/main/java/com/example/springbootbookshop/entity/Cart.java new file mode 100644 index 0000000..6c3fafe --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/entity/Cart.java @@ -0,0 +1,42 @@ +package com.example.springbootbookshop.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; +import java.util.HashSet; +import java.util.Set; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +@Entity +@Data +@SQLDelete(sql = "UPDATE carts SET is_deleted = true WHERE id = ?") +@Where(clause = "is_deleted=false") +@Table(name = "carts") +public class Cart { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToOne(fetch = FetchType.LAZY, optional = false) + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "user_id", nullable = false) + private User user; + + @OneToMany(mappedBy = "cart") + private Set cartItems = new HashSet<>(); + + @Column(nullable = false) + private boolean isDeleted = false; +} diff --git a/src/main/java/com/example/springbootbookshop/entity/CartItem.java b/src/main/java/com/example/springbootbookshop/entity/CartItem.java new file mode 100644 index 0000000..1b7360f --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/entity/CartItem.java @@ -0,0 +1,38 @@ +package com.example.springbootbookshop.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import jakarta.validation.constraints.NotNull; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@Entity +@Data +@Table(name = "cart_items") +public class CartItem { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "cart_id", nullable = false) + private Cart cart; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "book_id", nullable = false) + private Book book; + + @Column(nullable = false) + @NotNull + private int quantity; +} diff --git a/src/main/java/com/example/springbootbookshop/mapper/CartItemMapper.java b/src/main/java/com/example/springbootbookshop/mapper/CartItemMapper.java new file mode 100644 index 0000000..bada316 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/mapper/CartItemMapper.java @@ -0,0 +1,21 @@ +package com.example.springbootbookshop.mapper; + +import com.example.springbootbookshop.dto.cart.item.CartItemDto; +import com.example.springbootbookshop.dto.cart.item.RequestCartItemDto; +import com.example.springbootbookshop.entity.CartItem; +import org.mapstruct.InjectionStrategy; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.NullValueCheckStrategy; + +@Mapper(componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = ".impl") +public interface CartItemMapper { + @Mapping(source = "book.id", target = "bookId") + @Mapping(source = "book.title", target = "bookTitle") + CartItemDto toDto(CartItem cartItem); + + CartItem toEntity(RequestCartItemDto cartItemDto); +} diff --git a/src/main/java/com/example/springbootbookshop/mapper/CartMapper.java b/src/main/java/com/example/springbootbookshop/mapper/CartMapper.java new file mode 100644 index 0000000..e45fc9c --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/mapper/CartMapper.java @@ -0,0 +1,22 @@ +package com.example.springbootbookshop.mapper; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.cart.item.RequestCartItemDto; +import com.example.springbootbookshop.entity.Cart; +import org.mapstruct.InjectionStrategy; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.NullValueCheckStrategy; + +@Mapper(componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = ".impl", + uses = CartItemMapper.class +) +public interface CartMapper { + @Mapping(source = "user.id", target = "userId") + CartDto toDto(Cart cart); + + Cart toEntity(RequestCartItemDto cart); +} diff --git a/src/main/java/com/example/springbootbookshop/repository/CartItemsRepository.java b/src/main/java/com/example/springbootbookshop/repository/CartItemsRepository.java new file mode 100644 index 0000000..e3c9460 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/repository/CartItemsRepository.java @@ -0,0 +1,17 @@ +package com.example.springbootbookshop.repository; + +import com.example.springbootbookshop.entity.Book; +import com.example.springbootbookshop.entity.Cart; +import com.example.springbootbookshop.entity.CartItem; +import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CartItemsRepository extends JpaRepository { + @EntityGraph(attributePaths = "book") + Optional findByCartAndBook(Cart cart, Book book); + + Optional getCartItemsByIdAndCartId(Long itemId,Long cartId); +} diff --git a/src/main/java/com/example/springbootbookshop/repository/CartRepository.java b/src/main/java/com/example/springbootbookshop/repository/CartRepository.java new file mode 100644 index 0000000..8ae3ed4 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/repository/CartRepository.java @@ -0,0 +1,13 @@ +package com.example.springbootbookshop.repository; + +import com.example.springbootbookshop.entity.Cart; +import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CartRepository extends JpaRepository { + @EntityGraph(attributePaths = {"user","cartItems"}) + Optional getCartByUserId(Long id); +} diff --git a/src/main/java/com/example/springbootbookshop/service/CartService.java b/src/main/java/com/example/springbootbookshop/service/CartService.java new file mode 100644 index 0000000..e0b8b05 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/service/CartService.java @@ -0,0 +1,16 @@ +package com.example.springbootbookshop.service; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.cart.item.CartItemDto; +import com.example.springbootbookshop.dto.cart.item.RequestCartItemDto; +import com.example.springbootbookshop.dto.cart.item.UpdateRequestCartItemDto; + +public interface CartService { + CartDto findById(Long id); + + CartItemDto addItemToCart(Long id, RequestCartItemDto requestCartItemDto); + + void removeItem(Long userId, Long itemId); + + CartItemDto updateCartItem(Long id, UpdateRequestCartItemDto quantity); +} diff --git a/src/main/java/com/example/springbootbookshop/service/impl/CartServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/CartServiceImpl.java new file mode 100644 index 0000000..7ed7888 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/service/impl/CartServiceImpl.java @@ -0,0 +1,80 @@ +package com.example.springbootbookshop.service.impl; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.cart.item.CartItemDto; +import com.example.springbootbookshop.dto.cart.item.RequestCartItemDto; +import com.example.springbootbookshop.dto.cart.item.UpdateRequestCartItemDto; +import com.example.springbootbookshop.entity.Book; +import com.example.springbootbookshop.entity.Cart; +import com.example.springbootbookshop.entity.CartItem; +import com.example.springbootbookshop.exception.EntityNotFoundException; +import com.example.springbootbookshop.mapper.CartItemMapper; +import com.example.springbootbookshop.mapper.CartMapper; +import com.example.springbootbookshop.repository.BookRepository; +import com.example.springbootbookshop.repository.CartItemsRepository; +import com.example.springbootbookshop.repository.CartRepository; +import com.example.springbootbookshop.service.CartService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class CartServiceImpl implements CartService { + private final CartRepository cartRepository; + private final CartMapper cartMapper; + private final BookRepository bookRepository; + private final CartItemMapper cartItemMapper; + private final CartItemsRepository cartItemsRepository; + + @Transactional(readOnly = true) + @Override + public CartDto findById(Long id) { + Cart cart = cartRepository.getCartByUserId(id).orElseThrow( + () -> new EntityNotFoundException("User with id " + + id + "don`t have a cart") + ); + return cartMapper.toDto(cart); + } + + @Override + @Transactional + public CartItemDto addItemToCart(Long userId, RequestCartItemDto requestCartItemDto) { + Cart cart = cartRepository.getCartByUserId(userId).get(); + Book book = bookRepository.findById(requestCartItemDto.bookId()).orElseThrow( + () -> new EntityNotFoundException("Book with id: " + requestCartItemDto.bookId() + + "doesn`t exist")); + int quantityToAdd = requestCartItemDto.quantity(); + CartItem cartItem = cartItemsRepository.findByCartAndBook(cart, book) + .orElseGet(() -> { + CartItem newCartItem = cartItemMapper.toEntity(requestCartItemDto); + newCartItem.setBook(book); + newCartItem.setCart(cart); + return newCartItem; + }); + cartItem.setQuantity(quantityToAdd + cartItem.getQuantity()); + return cartItemMapper.toDto(cartItemsRepository.save(cartItem)); + } + + @Override + @Transactional + public void removeItem(Long userId, Long itemId) { + Cart cart = cartRepository.getCartByUserId(userId) + .orElseThrow(() -> new EntityNotFoundException( + "Shopping cart not found for user with id: " + userId)); + CartItem cartItem = cartItemsRepository.getCartItemsByIdAndCartId(itemId, + cart.getId()) + .orElseThrow(() -> new EntityNotFoundException( + "Cart item not found in the user's shopping cart")); + cartItemsRepository.deleteById(itemId); + } + + @Override + public CartItemDto updateCartItem(Long id, UpdateRequestCartItemDto quantity) { + CartItem cartItemToUpdate = cartItemsRepository.findById(id).orElseThrow( + () -> new EntityNotFoundException("Nothing to update, item with id: " + + id + " doesn`t exist")); + cartItemToUpdate.setQuantity(quantity.quantity()); + return cartItemMapper.toDto(cartItemsRepository.save(cartItemToUpdate)); + } +} diff --git a/src/main/java/com/example/springbootbookshop/service/impl/CategoryServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/CategoryServiceImpl.java index cacc592..357b7ef 100644 --- a/src/main/java/com/example/springbootbookshop/service/impl/CategoryServiceImpl.java +++ b/src/main/java/com/example/springbootbookshop/service/impl/CategoryServiceImpl.java @@ -50,8 +50,8 @@ public CategoryDto update(Long id, CategoryRequestDto categoryRequestDtoDto) { @Override public void deleteById(Long id) { if (!categoryRepository.existsById(id)) { - throw new EntityNotFoundException("Category with id " + id - + "doesn`t exist"); + throw new EntityNotFoundException("Category with id: " + id + + " doesn`t exist"); } categoryRepository.deleteById(id); } diff --git a/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java index 974e4f1..aea1101 100644 --- a/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/springbootbookshop/service/impl/UserServiceImpl.java @@ -2,11 +2,13 @@ import com.example.springbootbookshop.dto.user.UserRegistrationRequestDto; import com.example.springbootbookshop.dto.user.UserResponseDto; +import com.example.springbootbookshop.entity.Cart; import com.example.springbootbookshop.entity.Role; import com.example.springbootbookshop.entity.RoleName; import com.example.springbootbookshop.entity.User; import com.example.springbootbookshop.exception.RegistrationException; import com.example.springbootbookshop.mapper.UserMapper; +import com.example.springbootbookshop.repository.CartRepository; import com.example.springbootbookshop.repository.RoleRepository; import com.example.springbootbookshop.repository.UserRepository; import com.example.springbootbookshop.service.UserService; @@ -22,6 +24,7 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final RoleRepository roleRepository; + private final CartRepository cartRepository; private final UserMapper userMapper; private final PasswordEncoder passwordEncoder; @@ -35,7 +38,14 @@ public UserResponseDto register(UserRegistrationRequestDto requestDto) User user = userMapper.toUser(requestDto); user.setPassword(passwordEncoder.encode(requestDto.password())); user.setRoles(getUserRole()); - return userMapper.toUserDto(userRepository.save(user)); + setShoppingCartForUser(userRepository.save(user)); + return userMapper.toUserDto(user); + } + + private void setShoppingCartForUser(User user) { + Cart cart = new Cart(); + cart.setUser(user); + cartRepository.save(cart); } private Set getUserRole() { diff --git a/src/main/resources/db/changelog/changes/07-create-carts-table.yaml b/src/main/resources/db/changelog/changes/07-create-carts-table.yaml new file mode 100644 index 0000000..4e8ba3c --- /dev/null +++ b/src/main/resources/db/changelog/changes/07-create-carts-table.yaml @@ -0,0 +1,33 @@ +databaseChangeLog: + - changeSet: + id: create-carts-table + author: bohdan-maksymenko + changes: + - createTable: + tableName: carts + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: user_id + type: BIGINT + constraints: + nullable: false + - column: + name: is_deleted + type: BOOLEAN + defaultValueBoolean: false + constraints: + nullable: false + - addForeignKeyConstraint: + baseTableName: carts + baseColumnNames: user_id + referencedTableName: users + referencedColumnNames: id + constraintName: FK_carts_user + onDelete: CASCADE diff --git a/src/main/resources/db/changelog/changes/08-create-cart-items-table.yaml b/src/main/resources/db/changelog/changes/08-create-cart-items-table.yaml new file mode 100644 index 0000000..c8b9ddf --- /dev/null +++ b/src/main/resources/db/changelog/changes/08-create-cart-items-table.yaml @@ -0,0 +1,30 @@ +databaseChangeLog: + - changeSet: + id: create-cart-items-table + author: bohdan-maksymenko + changes: + - createTable: + tableName: cart_items + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: cart_id + type: BIGINT + constraints: + nullable: false + - column: + name: book_id + type: BIGINT + constraints: + nullable: false + - column: + name: quantity + type: INTEGER + constraints: + nullable: false \ No newline at end of file diff --git a/src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml b/src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml new file mode 100644 index 0000000..dc34e87 --- /dev/null +++ b/src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml @@ -0,0 +1,19 @@ +databaseChangeLog: + - changeSet: + id: add-cart-id-column-to-users + author: bohdan-maksymenko + changes: + - addColumn: + tableName: users + columns: + - column: + name: cart_id + type: BIGINT + constraints: + nullable: true + - addForeignKeyConstraint: + baseTableName: users + baseColumnNames: cart_id + referencedTableName: carts + referencedColumnNames: id + constraintName: FK_users_cart diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 471d7d5..dec3f5c 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -10,4 +10,8 @@ databaseChangeLog: - include: file: db/changelog/changes/05-create-category-table.yaml - include: - file: db/changelog/changes/06-create-books-categories-table.yaml \ No newline at end of file + file: db/changelog/changes/06-create-books-categories-table.yaml + - include: + file: db/changelog/changes/07-create-carts-table.yaml + - include: + file: db/changelog/changes/08-create-cart-items-table.yaml \ No newline at end of file