Skip to content

Commit

Permalink
first pr create some entities and liquibase test
Browse files Browse the repository at this point in the history
  • Loading branch information
don-bigdad committed Nov 14, 2023
1 parent 003c992 commit 2f04ac0
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.springbootbookshop.dto.order;

import jakarta.validation.constraints.NotBlank;

public record RequestOrderDto(@NotBlank String shippingAddress) {
}
60 changes: 60 additions & 0 deletions src/main/java/com/example/springbootbookshop/entity/Order.java
Original file line number Diff line number Diff line change
@@ -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<OrderItem> orderItems = new HashSet<>();

@Column(nullable = false)
private boolean isDeleted = false;
}
49 changes: 49 additions & 0 deletions src/main/java/com/example/springbootbookshop/entity/OrderItem.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.springbootbookshop.entity;

public enum Status {
PROCESSING,
COMPLETED,
CANCELLED
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

53 changes: 53 additions & 0 deletions src/main/resources/db/changelog/changes/09-add-orders-table.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit 2f04ac0

Please sign in to comment.