From 2f04ac03ee44388e109526ad89510aee31c1b618 Mon Sep 17 00:00:00 2001 From: Bogdan <97698639+don-bigdad@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:56:58 +0200 Subject: [PATCH] first pr create some entities and liquibase test --- .../controller/OrderController.java | 39 ++++++++++++ .../dto/order/RequestOrderDto.java | 6 ++ .../springbootbookshop/entity/Order.java | 60 +++++++++++++++++++ .../springbootbookshop/entity/OrderItem.java | 49 +++++++++++++++ .../springbootbookshop/entity/Status.java | 7 +++ .../service/OrderService.java | 8 +++ .../service/impl/OrderServiceImpl.java | 15 +++++ .../09-add-cart-id-to-users-table.yaml | 19 ------ .../changes/09-add-orders-table.yaml | 53 ++++++++++++++++ .../changes/10-create-orders-items-table.yaml | 55 +++++++++++++++++ .../db/changelog/db.changelog-master.yaml | 6 +- 11 files changed, 297 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/example/springbootbookshop/controller/OrderController.java create mode 100644 src/main/java/com/example/springbootbookshop/dto/order/RequestOrderDto.java create mode 100644 src/main/java/com/example/springbootbookshop/entity/Order.java create mode 100644 src/main/java/com/example/springbootbookshop/entity/OrderItem.java create mode 100644 src/main/java/com/example/springbootbookshop/entity/Status.java create mode 100644 src/main/java/com/example/springbootbookshop/service/OrderService.java create mode 100644 src/main/java/com/example/springbootbookshop/service/impl/OrderServiceImpl.java delete mode 100644 src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml create mode 100644 src/main/resources/db/changelog/changes/09-add-orders-table.yaml create mode 100644 src/main/resources/db/changelog/changes/10-create-orders-items-table.yaml diff --git a/src/main/java/com/example/springbootbookshop/controller/OrderController.java b/src/main/java/com/example/springbootbookshop/controller/OrderController.java new file mode 100644 index 0000000..d4e9535 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/controller/OrderController.java @@ -0,0 +1,39 @@ +package com.example.springbootbookshop.controller; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.order.RequestOrderDto; +import com.example.springbootbookshop.entity.User; +import com.example.springbootbookshop.service.OrderService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +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.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@Tag(name = "Order management",description = "Endpoints make orders") +@RestController +@Validated +@RequestMapping("/orders") +public class OrderController { + private final OrderService orderService; + + @PostMapping + @Operation(summary = "Make order") + @PreAuthorize("hasRole('USER')") + public CartDto makeOrder(Authentication authentication, + @RequestBody RequestOrderDto requestOrderDto) { + orderService.save(getUserId(authentication), requestOrderDto); + return null; + } + + private Long getUserId(Authentication authentication) { + User user = (User) authentication.getPrincipal(); + return user.getId(); + } +} diff --git a/src/main/java/com/example/springbootbookshop/dto/order/RequestOrderDto.java b/src/main/java/com/example/springbootbookshop/dto/order/RequestOrderDto.java new file mode 100644 index 0000000..356e49b --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/dto/order/RequestOrderDto.java @@ -0,0 +1,6 @@ +package com.example.springbootbookshop.dto.order; + +import jakarta.validation.constraints.NotBlank; + +public record RequestOrderDto(@NotBlank String shippingAddress) { +} diff --git a/src/main/java/com/example/springbootbookshop/entity/Order.java b/src/main/java/com/example/springbootbookshop/entity/Order.java new file mode 100644 index 0000000..11a0bba --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/entity/Order.java @@ -0,0 +1,60 @@ +package com.example.springbootbookshop.entity; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import java.time.LocalDateTime; +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 +@Table(name = "orders") +@SQLDelete(sql = "UPDATE orders SET is_deleted = TRUE WHERE id = ?") +@Where(clause = "is_deleted = false") +public class Order { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @ToString.Exclude + @EqualsAndHashCode.Exclude + private User user; + + @Enumerated(EnumType.STRING) + @Column(nullable = false) + private Status status; + + @Column(nullable = false) + private BigDecimal total; + + @Column(nullable = false) + private LocalDateTime orderDate; + + @Column(nullable = false) + private String shippingAddress; + + @OneToMany(mappedBy = "order", orphanRemoval = true, + cascade = CascadeType.REMOVE) + @Column(nullable = false) + private Set orderItems = new HashSet<>(); + + @Column(nullable = false) + private boolean isDeleted = false; +} diff --git a/src/main/java/com/example/springbootbookshop/entity/OrderItem.java b/src/main/java/com/example/springbootbookshop/entity/OrderItem.java new file mode 100644 index 0000000..3000bee --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/entity/OrderItem.java @@ -0,0 +1,49 @@ +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 java.math.BigDecimal; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +@Entity +@Data +@Table(name = "order_items") +@SQLDelete(sql = "UPDATE orders_items SET is_deleted = TRUE WHERE id = ?") +@Where(clause = "is_deleted = false") +public class OrderItem { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @ToString.Exclude + @EqualsAndHashCode.Exclude + @JoinColumn(name = "order_id") + private Order order; + + @ToString.Exclude + @EqualsAndHashCode.Exclude + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "book_id", nullable = false) + private Book book; + + @Column(nullable = false) + private Integer quantity; + + @Column(nullable = false) + private BigDecimal price; + + @Column(nullable = false) + private boolean isDeleted = false; +} diff --git a/src/main/java/com/example/springbootbookshop/entity/Status.java b/src/main/java/com/example/springbootbookshop/entity/Status.java new file mode 100644 index 0000000..29781e0 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/entity/Status.java @@ -0,0 +1,7 @@ +package com.example.springbootbookshop.entity; + +public enum Status { + PROCESSING, + COMPLETED, + CANCELLED +} diff --git a/src/main/java/com/example/springbootbookshop/service/OrderService.java b/src/main/java/com/example/springbootbookshop/service/OrderService.java new file mode 100644 index 0000000..4ca8819 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/service/OrderService.java @@ -0,0 +1,8 @@ +package com.example.springbootbookshop.service; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.order.RequestOrderDto; + +public interface OrderService { + CartDto save(Long userId, RequestOrderDto requestOrderDto); +} diff --git a/src/main/java/com/example/springbootbookshop/service/impl/OrderServiceImpl.java b/src/main/java/com/example/springbootbookshop/service/impl/OrderServiceImpl.java new file mode 100644 index 0000000..75d9c99 --- /dev/null +++ b/src/main/java/com/example/springbootbookshop/service/impl/OrderServiceImpl.java @@ -0,0 +1,15 @@ +package com.example.springbootbookshop.service.impl; + +import com.example.springbootbookshop.dto.cart.CartDto; +import com.example.springbootbookshop.dto.order.RequestOrderDto; +import com.example.springbootbookshop.service.OrderService; +import org.springframework.stereotype.Service; + +@Service +public class OrderServiceImpl implements OrderService { + @Override + public CartDto save(Long userId, RequestOrderDto requestOrderDto) { + + return null; + } +} 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 deleted file mode 100644 index dc34e87..0000000 --- a/src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml +++ /dev/null @@ -1,19 +0,0 @@ -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/changes/09-add-orders-table.yaml b/src/main/resources/db/changelog/changes/09-add-orders-table.yaml new file mode 100644 index 0000000..97721bf --- /dev/null +++ b/src/main/resources/db/changelog/changes/09-add-orders-table.yaml @@ -0,0 +1,53 @@ +databaseChangeLog: + - changeSet: + id: create-orders-table + author: bohdan-maksymenko + changes: + - createTable: + tableName: orders + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: user_id + type: BIGINT + constraints: + nullable: false + - column: + name: status + type: VARCHAR(255) + constraints: + nullable: false + - column: + name: total + type: DECIMAL(19, 2) + constraints: + nullable: false + - column: + name: order_date + type: TIMESTAMP + constraints: + nullable: false + - column: + name: shipping_address + type: VARCHAR(255) + constraints: + nullable: false + - column: + name: is_deleted + type: BOOLEAN + defaultValueBoolean: false + constraints: + nullable: false + - addForeignKeyConstraint: + baseTableName: orders + baseColumnNames: user_id + referencedTableName: users + referencedColumnNames: id + constraintName: FK_orders_user + onDelete: CASCADE \ No newline at end of file diff --git a/src/main/resources/db/changelog/changes/10-create-orders-items-table.yaml b/src/main/resources/db/changelog/changes/10-create-orders-items-table.yaml new file mode 100644 index 0000000..7bd3c30 --- /dev/null +++ b/src/main/resources/db/changelog/changes/10-create-orders-items-table.yaml @@ -0,0 +1,55 @@ +databaseChangeLog: + - changeSet: + id: create-order-items-table + author: bohdan-maksymenko + changes: + - createTable: + tableName: order_items + columns: + - column: + name: id + type: BIGINT + autoIncrement: true + constraints: + primaryKey: true + nullable: false + - column: + name: order_id + type: BIGINT + constraints: + nullable: false + - column: + name: book_id + type: BIGINT + constraints: + nullable: false + - column: + name: quantity + type: INTEGER + constraints: + nullable: false + - column: + name: price + type: DECIMAL(19, 2) + constraints: + nullable: false + - column: + name: is_deleted + type: BOOLEAN + defaultValueBoolean: false + constraints: + nullable: false + - addForeignKeyConstraint: + baseTableName: order_items + baseColumnNames: order_id + referencedTableName: orders + referencedColumnNames: id + constraintName: FK_order_items_order + onDelete: CASCADE + - addForeignKeyConstraint: + baseTableName: order_items + baseColumnNames: book_id + referencedTableName: books + referencedColumnNames: id + constraintName: FK_order_items_book + onDelete: CASCADE \ No newline at end of file diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index dec3f5c..7db6ddb 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -14,4 +14,8 @@ databaseChangeLog: - 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 + file: db/changelog/changes/08-create-cart-items-table.yaml + - include: + file: db/changelog/changes/09-add-orders-table.yaml + - include: + file: db/changelog/changes/10-create-orders-items-table.yaml \ No newline at end of file