From 033fc5851cf02d9c15f784ca8cdd1ec07904451c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 12 May 2022 19:03:51 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20d=C3=A9but=20d'un=20backend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/pixels/beans/Groupe.java | 4 ++ .../java/com/pixels/beans/Permission.java | 6 +++ src/main/java/com/pixels/beans/Pixel.java | 25 +++++++++- src/main/java/com/pixels/beans/Role.java | 6 +++ .../java/com/pixels/beans/Transaction.java | 18 ++++++- src/main/java/com/pixels/beans/User.java | 4 ++ src/main/java/com/pixels/beans/Wallet.java | 4 ++ .../com/pixels/services/PixelService.java | 48 ++++++++++++++----- src/main/webapp/WEB-INF/jboss-web.xml | 3 ++ 9 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jboss-web.xml diff --git a/src/main/java/com/pixels/beans/Groupe.java b/src/main/java/com/pixels/beans/Groupe.java index d8b0e2e..a95addd 100644 --- a/src/main/java/com/pixels/beans/Groupe.java +++ b/src/main/java/com/pixels/beans/Groupe.java @@ -4,12 +4,16 @@ import javax.persistence.*; import javax.validation.constraints.NotBlank; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Groupe.list", query = "SELECT g FROM Groupe g") public class Groupe implements Serializable { diff --git a/src/main/java/com/pixels/beans/Permission.java b/src/main/java/com/pixels/beans/Permission.java index addcea7..b5cddc3 100644 --- a/src/main/java/com/pixels/beans/Permission.java +++ b/src/main/java/com/pixels/beans/Permission.java @@ -1,14 +1,19 @@ package com.pixels.beans; import javax.persistence.*; +import javax.validation.constraints.NotNull; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Permission.list", query = "SELECT pe FROM Permission pe") public class Permission implements Serializable { @@ -18,5 +23,6 @@ public class Permission implements Serializable { // type de la permission @NonNull + @NotNull private Integer type; } diff --git a/src/main/java/com/pixels/beans/Pixel.java b/src/main/java/com/pixels/beans/Pixel.java index 94e02d4..0d0a541 100644 --- a/src/main/java/com/pixels/beans/Pixel.java +++ b/src/main/java/com/pixels/beans/Pixel.java @@ -1,9 +1,13 @@ package com.pixels.beans; import javax.persistence.*; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.awt.Color; @@ -12,6 +16,8 @@ import java.util.List; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Pixel.list", query = "SELECT p FROM Pixel p") public class Pixel implements Serializable { @@ -21,14 +27,17 @@ public class Pixel implements Serializable { // prix du pixel à l'achat @NonNull + @NotNull private Integer price; // couleur du pixel sur la peinture @NonNull - private Color color; + @NotNull + private Integer color; // petit mot écrit par le user @NonNull + @NotBlank private String description; // propriétaire actuel du pixel @@ -38,4 +47,18 @@ public class Pixel implements Serializable { // liste des transactions associées au pixel @OneToMany(mappedBy = "pixel") private List transactions; + + Color getColor() { + return new Color(color); + } + + void setColor(Color color) { + this.color = color.getRGB(); + } + + public Pixel(Integer price, Color color, String description) { + this.price = price; + this.color = color.getRGB(); + this.description = description; + } } diff --git a/src/main/java/com/pixels/beans/Role.java b/src/main/java/com/pixels/beans/Role.java index 97b55f2..7756303 100644 --- a/src/main/java/com/pixels/beans/Role.java +++ b/src/main/java/com/pixels/beans/Role.java @@ -1,14 +1,19 @@ package com.pixels.beans; import javax.persistence.*; +import javax.validation.constraints.NotNull; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Role.list", query = "SELECT r FROM Role r") public class Role implements Serializable { @@ -18,5 +23,6 @@ public class Role implements Serializable { // type du role @NonNull + @NotNull private Integer type; } diff --git a/src/main/java/com/pixels/beans/Transaction.java b/src/main/java/com/pixels/beans/Transaction.java index eab4a0c..c0cab07 100644 --- a/src/main/java/com/pixels/beans/Transaction.java +++ b/src/main/java/com/pixels/beans/Transaction.java @@ -1,17 +1,22 @@ package com.pixels.beans; import javax.persistence.*; +import javax.validation.constraints.NotNull; import java.awt.Color; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; import java.util.Date; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Transaction.list", query = "SELECT t FROM Transaction t order by t.time desc") public class Transaction implements Serializable { @@ -21,14 +26,17 @@ public class Transaction implements Serializable { // prix de la transaction* @NonNull + @NotNull private Integer price; // couleur du pixel de la transaction @NonNull - private Color color; + @NotNull + private Integer color; // date de la transaction @NonNull + @NotNull private Date time; @ManyToOne // wallet acheteur @@ -36,4 +44,12 @@ public class Transaction implements Serializable { @ManyToOne // pixel acheté private Pixel pixel; + + Color getColor() { + return new Color(color); + } + + void setColor(Color color) { + this.color = color.getRGB(); + } } diff --git a/src/main/java/com/pixels/beans/User.java b/src/main/java/com/pixels/beans/User.java index 40811bf..4913517 100644 --- a/src/main/java/com/pixels/beans/User.java +++ b/src/main/java/com/pixels/beans/User.java @@ -6,13 +6,17 @@ import javax.validation.constraints.NotBlank; import com.pixels.utils.Hash; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; import java.util.List; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "User.list", query = "SELECT u FROM User u") public class User implements Serializable { diff --git a/src/main/java/com/pixels/beans/Wallet.java b/src/main/java/com/pixels/beans/Wallet.java index 62fedbe..950af16 100644 --- a/src/main/java/com/pixels/beans/Wallet.java +++ b/src/main/java/com/pixels/beans/Wallet.java @@ -4,13 +4,17 @@ import javax.persistence.*; import javax.validation.constraints.NotBlank; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.NonNull; +import lombok.RequiredArgsConstructor; import java.io.Serializable; import java.util.List; @Data @Entity +@NoArgsConstructor +@RequiredArgsConstructor @NamedQuery(name = "Wallet.list", query = "SELECT w FROM Wallet w") public class Wallet implements Serializable { diff --git a/src/main/java/com/pixels/services/PixelService.java b/src/main/java/com/pixels/services/PixelService.java index 52b73e7..2eb4eae 100644 --- a/src/main/java/com/pixels/services/PixelService.java +++ b/src/main/java/com/pixels/services/PixelService.java @@ -1,7 +1,5 @@ package com.pixels.services; -import java.util.List; - import java.awt.Color; import javax.ejb.Singleton; @@ -10,10 +8,14 @@ import javax.json.JsonObject; import javax.json.JsonObjectBuilder; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @@ -32,22 +34,44 @@ public class PixelService { @GET @Produces(MediaType.APPLICATION_JSON) public String list() { - List query = em.createNamedQuery("Pixel.list", Pixel.class).getResultList(); - return gson.toJson(query); + TypedQuery query = em.createNamedQuery("Pixel.list", Pixel.class); + return gson.toJson(query.getResultList()); + } + + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public String single(@PathParam("id") Long id) { + Pixel pixel = em.find(Pixel.class, id); + return gson.toJson(pixel); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public JsonObject add() { - - JsonObjectBuilder response = Json.createObjectBuilder(); - - Pixel pixel = new Pixel(10, new Color(255, 0, 0), "un pixel Poog"); + public String add(String json) { + Pixel pixel = gson.fromJson(json, Pixel.class); em.persist(pixel); - - response.add("status", "pixel created !"); - return response.build(); + return gson.toJson(pixel); } + @PUT + @Path("{id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public String update(@PathParam("id") Long id, String json) { + Pixel pixel = gson.fromJson(json, Pixel.class); + pixel.setId(id); + em.merge(pixel); + return gson.toJson(pixel); + } + + @DELETE + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public String remove(@PathParam("id") Long id) { + Pixel pixel = em.find(Pixel.class, id); + em.remove(pixel); + return gson.toJson(pixel); + } } diff --git a/src/main/webapp/WEB-INF/jboss-web.xml b/src/main/webapp/WEB-INF/jboss-web.xml new file mode 100644 index 0000000..f26e157 --- /dev/null +++ b/src/main/webapp/WEB-INF/jboss-web.xml @@ -0,0 +1,3 @@ + + / +