-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pr create some entities and liquibase test
- Loading branch information
1 parent
003c992
commit 2f04ac0
Showing
11 changed files
with
297 additions
and
20 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/springbootbookshop/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/example/springbootbookshop/dto/order/RequestOrderDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
src/main/java/com/example/springbootbookshop/entity/Order.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/main/java/com/example/springbootbookshop/entity/OrderItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/springbootbookshop/entity/Status.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/springbootbookshop/service/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/springbootbookshop/service/impl/OrderServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/main/resources/db/changelog/changes/09-add-cart-id-to-users-table.yaml
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/main/resources/db/changelog/changes/09-add-orders-table.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
55 changes: 55 additions & 0 deletions
55
src/main/resources/db/changelog/changes/10-create-orders-items-table.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters