Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
8 changes: 7 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Basket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.EqualsAndHashCode;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
Expand All @@ -22,6 +24,9 @@ public class Basket extends UriEntity<Long> {
@JsonIdentityReference(alwaysAsId = true)
private Customer customer;

@OneToMany(mappedBy = "basket", cascade = CascadeType.ALL, orphanRemoval = true)
private List<BasketItem> items = new ArrayList<>();

@Column(nullable = false, updatable = false)
private ZonedDateTime createdAt;

Expand All @@ -39,4 +44,5 @@ protected void onCreate() {
protected void onUpdate() {
this.updatedAt = ZonedDateTime.now();
}
}
}

33 changes: 33 additions & 0 deletions src/main/java/cat/udl/eps/softarch/demo/domain/BasketItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cat.udl.eps.softarch.demo.domain;

import com.fasterxml.jackson.annotation.JsonIdentityReference;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Entity
@Data
@EqualsAndHashCode(callSuper = true)
public class BasketItem extends UriEntity<Long> {

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

@NotNull
@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
private Basket basket;

@NotNull
@ManyToOne
@JsonIdentityReference(alwaysAsId = true)
private Product product;

@NotNull
@Min(1)
private Integer quantity = 1;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cat.udl.eps.softarch.demo.handler;

import cat.udl.eps.softarch.demo.domain.BasketItem;
import cat.udl.eps.softarch.demo.repository.BasketRepository;
import org.springframework.data.rest.core.annotation.*;
import org.springframework.stereotype.Component;

@Component
@RepositoryEventHandler
public class BasketItemEventHandler {

private final BasketRepository basketRepository;

public BasketItemEventHandler(BasketRepository basketRepository) {
this.basketRepository = basketRepository;
}

@HandleBeforeCreate
@HandleBeforeSave
public void handleBasketItemSave(BasketItem item) {
if (item.getBasket() != null) {
basketRepository.save(item.getBasket()); // Updates basket updatedAt
}
}

@HandleBeforeDelete
public void handleBasketItemDelete(BasketItem item) {
if (item.getBasket() != null) {
basketRepository.save(item.getBasket());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cat.udl.eps.softarch.demo.repository;

import cat.udl.eps.softarch.demo.domain.Basket;
import cat.udl.eps.softarch.demo.domain.BasketItem;
import cat.udl.eps.softarch.demo.domain.Product;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;
import java.util.Optional;

public interface BasketItemRepository extends CrudRepository<BasketItem, Long>, PagingAndSortingRepository<BasketItem, Long> {

List<BasketItem> findByBasket(Basket basket);
Optional<BasketItem> findByBasketAndProduct(Basket basket, Product product);
}
Binary file added src/test/.DS_Store
Binary file not shown.
Binary file added src/test/resources/.DS_Store
Binary file not shown.