Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/IL-77/Add-OpenAPI-for-cart-package #100

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@
</configOptions>
</configuration>
</execution>
<execution>
<id>generate-from-cart-openapi-spec</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api-specs/cart-openapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>com.zufar.onlinestore.openapi.cart.api</apiPackage>
<modelPackage>com.zufar.onlinestore.cart.dto</modelPackage>
<generateModels>false</generateModels>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<useTags>true</useTags>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.zufar.onlinestore.cart.dto.NewShoppingSessionItemDto;
import com.zufar.onlinestore.cart.dto.ShoppingSessionDto;
import com.zufar.onlinestore.cart.dto.UpdateProductsQuantityInShoppingSessionItemRequest;
import com.zufar.onlinestore.openapi.cart.api.CartsApi;
import com.zufar.onlinestore.user.entity.UserEntity;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -30,14 +31,14 @@
@RequiredArgsConstructor
@Validated
@RequestMapping(value = CartEndpoint.CART_URL)
public class CartEndpoint {
public class CartEndpoint implements CartsApi {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CartApi sounds better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already have CartApi in this class (import com.zufar.onlinestore.cart.api.CartApi). Is it ok to use same names?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is no problem


public static final String CART_URL = "/api/v1/cart";

private final CartApi cartApi;

@PostMapping(value = "/items")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to @OverRide the method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are overrided automaticly, isn't it?

Copy link
Owner

@Sunagatov Sunagatov Sep 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using this annotation is like a best practice
it is really good idea to add it

public ResponseEntity<ShoppingSessionDto> addNewItemToShoppingSession(@RequestBody @Valid final AddNewItemsToShoppingSessionRequest request) {
public ResponseEntity<ShoppingSessionDto> addNewItemToShoppingSession(@RequestBody final AddNewItemsToShoppingSessionRequest request) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you delete ‘@Valid'?
Rollback it, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this annotation we have a trouble in case when we try to use our API not in IDE (i use Postman): Hibernate validator HV000151 "A method overriding another method must not alter the parameter constraint configuration").
In our generated interface we have the same annotation, its incorrect.
More info: https://forum.hibernate.org/viewtopic.php?p=2469903

log.warn("Received the request to add a new items to the shoppingSession");
Set<NewShoppingSessionItemDto> items = request.items();
ShoppingSessionDto shoppingSessionDto = cartApi.addItemsToShoppingSession(items);
Expand All @@ -57,7 +58,7 @@ public ResponseEntity<ShoppingSessionDto> getShoppingSession(@AuthenticationPrin
}

@PatchMapping(value = "/items")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to @OverRide the method

public ResponseEntity<ShoppingSessionDto> updateProductsQuantityInShoppingSessionItem(@RequestBody @Valid final UpdateProductsQuantityInShoppingSessionItemRequest request) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rollback ‘@Valid' annotation, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

public ResponseEntity<ShoppingSessionDto> updateProductsQuantityInShoppingSessionItem(@RequestBody final UpdateProductsQuantityInShoppingSessionItemRequest request) {
UUID shoppingSessionItemId = request.shoppingSessionItemId();
Integer productsQuantityChange = request.productsQuantityChange();
log.warn("Received the request to update the productsQuantity with the change = {} in the shoppingSessionItem with id: {}.",
Expand All @@ -69,7 +70,7 @@ public ResponseEntity<ShoppingSessionDto> updateProductsQuantityInShoppingSessio
}

@DeleteMapping(value = "/items")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to @OverRide the method

public ResponseEntity<ShoppingSessionDto> deleteItemsFromShoppingSession(@RequestBody @Valid final DeleteItemsFromShoppingSessionRequest request) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rollback ‘@Valid' annotation, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

public ResponseEntity<ShoppingSessionDto> deleteItemsFromShoppingSession(@RequestBody final DeleteItemsFromShoppingSessionRequest request) {
log.info("Received the request to delete the shopping session items with ids: {}.", request.shoppingSessionItemIds());
ShoppingSessionDto shoppingSessionDto = cartApi.deleteItemsFromShoppingSession(request);
log.info("The shopping session items with ids = {} were deleted.", request.shoppingSessionItemIds());
Expand Down
192 changes: 192 additions & 0 deletions src/main/resources/api-specs/cart-openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
openapi: "3.0.3"

info:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to write in a common style as in Zufar's example in product-openapi.yaml

info:
title: "Online-Store API"
version: "1.0.0"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

title: "Online_Store API"
description: "Online_Store API for Cart package"
version: "1.0.0"

servers:
- url: "http://localhost:8083"

tags:
- name: "Carts"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cart is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

description: "An API for managing and retrieving cart information"

paths:
/api/v1/cart/items:
post:
tags:
- "Carts"
summary: "Add a list of new products to the cart"
operationId: "addNewItemToShoppingSession"
requestBody:
description: "Add a list of new products to the cart by productIds and productsQuantity"
content:
application/json:
schema:
$ref: "#/components/schemas/AddNewItemsToShoppingSessionRequest"
required: true
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/ShoppingSessionDto"

patch:
tags:
- "Carts"
summary: "Update the item in the cart"
operationId: "updateProductsQuantityInShoppingSessionItem"
requestBody:
description: "Change the quantity of the item in the cart by shoppingSessionItemId and productsQuantityChange (example: '1' will increase the value by one, '-1' will reduce by one)"
content:
application/json:
schema:
$ref: "#/components/schemas/UpdateProductsQuantityInShoppingSessionItemRequest"
required: true
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/ShoppingSessionDto"

delete:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to write path as in a common style

tags:
- "Carts"
summary: "Delete list of items from the cart"
operationId: "deleteItemsFromShoppingSession"
requestBody:
description: "Delete list of items from the cart by list of shoppingSessionItemIds"
content:
application/json:
schema:
$ref: "#/components/schemas/DeleteItemsFromShoppingSessionRequest"
required: true
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/ShoppingSessionDto"

/api/v1/cart:
get:
tags:
- "Carts"
summary: "Get the cart of authorized user"
description: "Get the cart of authorized user"
operationId: "getShoppingSession"
responses:
"200":
description: "OK"
content:
'*/*':
schema:
$ref: "#/components/schemas/ShoppingSessionDto"

components:
schemas:
NewShoppingSessionItemDto:
type: "object"
properties:
productId:
type: "string"
format: "uuid"
productsQuantity:
type: "integer"
format: "int32"

AddNewItemsToShoppingSessionRequest:
type: "object"
properties:
items:
type: "array"
items:
$ref: "#/components/schemas/NewShoppingSessionItemDto"

ShoppingSessionDto:
type: "object"
properties:
id:
type: "string"
format: "uuid"
userId:
type: "string"
format: "uuid"
items:
type: "array"
items:
$ref: "#/components/schemas/ShoppingSessionItemDto"
itemsQuantity:
type: "integer"
format: "int32"
itemsTotalPrice:
type: "number"
format: "decimal"
productsQuantity:
type: "integer"
format: "int32"
createdAt:
type: "string"
format: "date-time"
closedAt:
type: "string"
format: "date-time"

ShoppingSessionItemDto:
type: "object"
properties:
id:
type: "string"
format: "uuid"
productInfo:
type: "array"
items:
$ref: "#/components/schemas/ProductInfoFullDto"
productsQuantity:
type: "integer"
format: "int32"

ProductInfoFullDto:
type: "object"
properties:
id:
type: "string"
format: "uuid"
name:
type: "string"
description:
type: "string"
price:
type: "number"
format: "decimal"
quantity:
type: "integer"
format: "int32"
active:
type: "boolean"
format: "yes"

UpdateProductsQuantityInShoppingSessionItemRequest:
type: "object"
properties:
shoppingSessionItemId:
type: "string"
format: "uuid"
productsQuantityChange:
type: "integer"
format: "int32"

DeleteItemsFromShoppingSessionRequest:
type: "object"
properties:
shoppingSessionItemIds:
type: "array"
items:
type: "string"
format: "uuid"