Skip to content

Commit

Permalink
feat(readme): completed readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Vol committed Sep 6, 2023
1 parent ce479e5 commit ef6c337
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 5 deletions.
163 changes: 159 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [x] :door: Secure User Registration and Login
- [x] :busts_in_silhouette: User Profile Management
- [x] :closed_lock_with_key: Token-based Authentication
- [x] :open_file_folder: Product Catalog Management
- [x] :open_file_folder: Product Catalog Management & Search
- [x] :shopping_cart: Add, Update and Remove Products in Shopping Cart
- [x] :page_facing_up: Order Checkout, Shipping and Payment

Expand All @@ -20,11 +20,10 @@ git clone https://github.com/Michael-Vol/ecommerce-api.git
```

2. Create a MySQL database and
run [this SQL script](https://github.com/Michael-Vol/ecommerce-api/blob/main/utils/create_ecommerce_db.sql) to
run [this SQL script](https://github.com/Michael-Vol/ecommerce-api/blob/main/utils/create_ecommerce_db.sql) to
create
the data structure.


3. Add the following env variables to the application.properties file as follows:

```properties
Expand All @@ -43,4 +42,160 @@ ecommerce.api.jwtSecret={YOUR_JWT_SECRET}
./mvnw spring-boot:run
```

### API Endpoints
## :globe_with_meridians: API Endpoints

### :closed_lock_with_key: Authentication

| Method | Endpoint | Description | Request Body |
| ------ | -------------------- | -------------------- | ---------------------- |
| POST | api/v1/auth/register | Registers a new user | [JSON](#auth_register) |
| POST | api/v1/auth/login | Authenticates a user | [JSON](#auth_login) |
| POST | api/v1/auth/logout | Logs out a user | |

### :busts_in_silhouette: User Profile Management

| Method | Endpoint | Description | Request Body |
| ------ | --------------- | ------------------------------- | ------------ |
| GET | api/v1/users/me | Gets the current user's profile | |

### :open_file_folder: Product Catalog Management

| Method | Endpoint | Description | Request Body |
| ------ | ---------------------------- | --------------------------- | -------------------------- |
| POST | /api/v1/products | Creates a new product | [JSON](#products_create) |
| GET | /api/v1/products | Gets all products | [Params](#products_get) |
| GET | /api/v1/products/{productId} | Gets a product by id | |
| GET | /api/v1/products/search | Searches in product catalog | [Params](#products_search) |
| PATCH | /api/v1/products/{productId} | Updates a product by id | [JSON](#products_update) |
| DELETE | /api/v1/products/{productId} | Deletes a product by id | |

### :shopping_cart: Shopping Cart Management

| Method | Endpoint | Description | Request Body |
| ------ | ---------------------------- | ------------------------------------ | ---------------------- |
| POST | /api/v1/carts/addToCart | Adds a product to cart | [JSON](#cart_add) |
| GET | /api/v1/carts | Gets the user's cart info | |
| PATCH | /api/v1/carts/updateQuantity | Updates an item's quantity | [JSON](#cart_updateq) |
| DELETE | /api/v1/carts/removeFromCart | Removes an item from cart | [JSON](#cart_remove) |
| DELETE | /api/v1/carts/clearCart | Empties the user's cart | |
| POST | /api/v1/carts/checkout | Creates an order from the cart items | [JSON](#cart_checkout) |

### :page_facing_up: Order Management

| Method | Endpoint | Description | Request Body |
| ------ | ------------------------------- | --------------------------------- | ------------ |
| GET | /api/v1/orders/{orderId} | Gets an order by id | |
| GET | /api/v1/orders/{orderId}/status | Gets the status of an order by id | |
| PATCH | /api/v1/orders/{orderId}/status | Updates an order's status | |

## Sample JSON Request Bodies

##### <a id="auth_register">User Registration</a>

```json
{
"firstName": "John",
"lastName": "Appleseed",
"username": "john",
"email": "[email protected]",
"password": "password",
"address": "225 Brickyard Lane Racine, WI 53402"
}
```

##### <a id="auth_login">User Login</a>

```json
{
"email": "[email protected]",
"password": "password"
}
```

##### <a id="products_create"> Create Product</a>

```json
{
"name": "Apple iPhone 12 Pro Max",
"description": "Apple iPhone 12 Pro Max 128GB Graphite",
"price": 1099.99,
"quantity": 10,
"category": "Electronics",
"brand": "Apple",
"image": "https://i.imgur.com/0oY8F0X.jpg"
}
```

##### <a id="products_get"> Get Products</a>

```
Query Params:
- page
- size
- sortBy
- direction
```

##### <a id="products_search"> Search Products</a>

```
Query Params:
- category
- minPrice
- maxPrice
- sortDirection
- sortBy
- page
- pageSize
```

##### <a id="products_update"> Update Product by Id</a>

```json
{
"name": "Apple iPhone 11 Pro Max",
"description": "Apple iPhone 1 Pro Max 128GB Graphite",
"price": 899.99,
"quantity": 10
}
```

##### <a id="cart_add"> Add Product to Cart</a>

```json
{
"productId": 1,
"quantity": 2
}
```

##### <a id="cart_updateq"> Update Cart Item Quantity</a>

```json
{
"productId": 1,
"quantity": 3
}
```

##### <a id="cart_remove"> Remove Product From Cart</a>

```json
{
"productId": 1
}
```

##### <a id="cart_checkout"> Checkout & Create Order</a>

```json
{
"cartId": 6,
"shippingAddress": "169 Tallwood Drive Jackson, NJ 08527",
"billingAddress": "169 Tallwood Drive Jackson, NJ 08527",
"paymentMethod": "CREDIT_CARD"
}
```
2 changes: 2 additions & 0 deletions src/main/java/com/michaelvol/ecommerceapi/cart/Cart.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.michaelvol.ecommerceapi.cart;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.michaelvol.ecommerceapi.cart.cartitem.CartItem;
import com.michaelvol.ecommerceapi.user.User;
import jakarta.persistence.*;
Expand All @@ -26,6 +27,7 @@ public class Cart {

@OneToOne
@JoinColumn(name = "user_id", nullable = false)
@JsonIgnore
private User user;

@OneToMany(mappedBy = "cart")
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/michaelvol/ecommerceapi/cart/CartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public ResponseEntity<AddToCartResponse> addToCart(@Valid @RequestBody AddToCart
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("")
public ResponseEntity<GetCartResponse> getCart() {
Long cartId = getCartIdFromAuthenticatedUser();
Cart cart = cartService.getCartById(cartId);
GetCartResponse response = GetCartResponse
.builder()
.cart(cart)
.build();
return new ResponseEntity<>(response, HttpStatus.OK);
}


@DeleteMapping("/removeFromCart")
public ResponseEntity<RemoveFromCartResponse> removeFromCart(@Valid @RequestBody RemoveFromCartRequest request) {
Long cartId = getCartIdFromAuthenticatedUser();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.michaelvol.ecommerceapi.cart.cartitem;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.michaelvol.ecommerceapi.cart.Cart;
import com.michaelvol.ecommerceapi.product.Product;
import jakarta.persistence.*;
Expand All @@ -25,6 +26,7 @@ public class CartItem {

@ManyToOne
@JoinColumn(name = "cart_id", nullable = false)
@JsonIgnore
private Cart cart;

@OneToOne
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.michaelvol.ecommerceapi.cart.dto;

import com.michaelvol.ecommerceapi.cart.Cart;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GetCartResponse {
private Cart cart;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class ProductController {
private final ProductService productService;

@PostMapping("/new")
@PostMapping("")
public ResponseEntity<CreateProductResponse> createProduct(@Valid @RequestBody CreateProductRequest createProductRequest) {
Product product = productService.create(createProductRequest);
CreateProductResponse response = CreateProductResponse
Expand Down

0 comments on commit ef6c337

Please sign in to comment.