From 12a51ed02c56b6a769d6cab77f05b96ea9874c48 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Sun, 15 Dec 2024 12:08:08 -0500 Subject: [PATCH] Fix removeCategory method in Beer class Related to #13 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/springframeworkguru/spring-6-rest-mvc/issues/13?shareId=XXXX-XXXX-XXXX-XXXX). --- .devcontainer/devcontainer.json | 7 +++ .../spring6restmvc/entities/Beer.java | 53 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 src/main/java/guru/springframework/spring6restmvc/entities/Beer.java diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..28e850793 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,7 @@ +{ + "tasks": { + "test": "./mvnw test", + "build": "./mvnw clean verify", + "launch": "mvn spring-boot:run" + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring6restmvc/entities/Beer.java b/src/main/java/guru/springframework/spring6restmvc/entities/Beer.java new file mode 100644 index 000000000..e38491ec3 --- /dev/null +++ b/src/main/java/guru/springframework/spring6restmvc/entities/Beer.java @@ -0,0 +1,53 @@ +package guru.springframework.spring6restmvc.entities; + +import lombok.*; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import jakarta.persistence.*; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Entity +public class Beer { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private UUID id; + + private String name; + private String style; + private String upc; + private Integer quantityOnHand; + private BigDecimal price; + + @CreationTimestamp + private LocalDateTime createdDate; + + @UpdateTimestamp + private LocalDateTime lastModifiedDate; + + @ManyToMany + @JoinTable(name = "beer_category", + joinColumns = @JoinColumn(name = "beer_id"), + inverseJoinColumns = @JoinColumn(name = "category_id")) + private Set categories = new HashSet<>(); + + public void addCategory(Category category){ + this.categories.add(category); + category.getBeers().add(this); + } + + public void removeCategory(Category category){ + this.categories.remove(category); + category.getBeers().remove(this); + } +}