Skip to content
Open
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.orderflow.ecommerce.controllers;

import com.orderflow.ecommerce.dtos.AddItemRequest;
import com.orderflow.ecommerce.dtos.CartItemResponse;
import com.orderflow.ecommerce.dtos.UpdateQuantityRequest;
import com.orderflow.ecommerce.entities.Cart;
import com.orderflow.ecommerce.entities.CartItem;
import com.orderflow.ecommerce.repositories.CartItemRepository;
import com.orderflow.ecommerce.repositories.CartRepository;
import com.orderflow.ecommerce.services.CartService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;


import java.util.Optional;

@RestController
@RequestMapping("/api/cart")
public class CartController {

@Autowired
private CartService cartService;

@Autowired
private CartRepository repository;

@Autowired
private CartItemRepository itemRepository;

@GetMapping
public Optional<Cart> listCart() {
return repository.findByUserId(1L);
}

@PostMapping
public CartItemResponse addItem(@Valid @RequestBody AddItemRequest request) {
return cartService.addItemToCart(request);
}


@DeleteMapping("/{id}")
public ResponseEntity<Void> removeItem(@PathVariable Long id) {
repository.deleteById(id);
return ResponseEntity.noContent().build();
}

@PutMapping("/items/{id}")
public CartItemResponse updateItem(@PathVariable Long id, @Valid @RequestBody UpdateQuantityRequest request) {

CartItem item = itemRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Cart item not found with id: " + id));

item.setQuantity(request.quantity());

return CartItemResponse.from(itemRepository.save(item));
}

@DeleteMapping("/clear")
public ResponseEntity<Void> clearCart() {
cartService.clearCurrentCart();
return ResponseEntity.noContent().build();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/com/orderflow/ecommerce/dtos/AddItemRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.orderflow.ecommerce.dtos;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

public record AddItemRequest(
@NotNull Long productId,
@NotNull @Positive Integer quantity,
@NotNull @Positive Double price
) {

}
21 changes: 21 additions & 0 deletions src/main/java/com/orderflow/ecommerce/dtos/CartItemResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.orderflow.ecommerce.dtos;

import com.orderflow.ecommerce.entities.CartItem;

public record CartItemResponse(
Long id,
Long productId,
String productName,
Integer quantity,
Double price
) {
public static CartItemResponse from(CartItem item) {
return new CartItemResponse(
item.getId(),
item.getProductId(),
"Produto #" + item.getProductId(),
item.getQuantity(),
item.getPrice()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.orderflow.ecommerce.dtos;

import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

public record UpdateQuantityRequest(
@NotNull(message = "A quantidade não pode ser nula")
@Min(value = 1, message = "A quantidade deve ser no mínimo 1")
Integer quantity
) {}
39 changes: 39 additions & 0 deletions src/main/java/com/orderflow/ecommerce/entities/Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.orderflow.ecommerce.entities;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "tb_cart")
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Cart implements Serializable {
@Serial
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;

private Instant createdAt = Instant.now();

@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL)
private List<CartItem> items = new ArrayList<>();

@Column(name = "user_id")
private Long userId;
}
29 changes: 29 additions & 0 deletions src/main/java/com/orderflow/ecommerce/entities/CartItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.orderflow.ecommerce.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import java.io.Serializable;

@Entity
@Table(name = "tb_cart_item")
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class CartItem implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Integer quantity;
private Double price;

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

@Column(name = "product_id_temp")
private Long productId;
}
20 changes: 0 additions & 20 deletions src/main/java/com/orderflow/ecommerce/entities/Category.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/main/java/com/orderflow/ecommerce/entities/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,4 @@ public class Product {

private Integer stockQuantity;

@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.orderflow.ecommerce.repositories;

import com.orderflow.ecommerce.entities.CartItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface CartItemRepository extends JpaRepository<CartItem, Long> {

@Modifying
@Transactional
@Query("DELETE FROM CartItem c WHERE c.cart.userId = :userId")
void deleteByCartUserId(Long userId);
}
Loading