From 52bba8c256ecf96b10b589acdd081ff9c6754afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Fri, 13 May 2022 14:27:19 +0200 Subject: [PATCH] refactor!: modification des beans + rest --- src/main/java/com/pixels/beans/Groupe.java | 31 ------- .../java/com/pixels/beans/Permission.java | 28 ------- src/main/java/com/pixels/beans/Pixel.java | 4 +- src/main/java/com/pixels/beans/Role.java | 28 ------- .../java/com/pixels/beans/Transaction.java | 24 ++++-- src/main/java/com/pixels/beans/User.java | 11 ++- src/main/java/com/pixels/beans/Wallet.java | 32 -------- .../com/pixels/services/PixelService.java | 27 ++----- .../java/com/pixels/services/UserService.java | 80 +++++++++++++++++++ 9 files changed, 111 insertions(+), 154 deletions(-) delete mode 100644 src/main/java/com/pixels/beans/Groupe.java delete mode 100644 src/main/java/com/pixels/beans/Permission.java delete mode 100644 src/main/java/com/pixels/beans/Role.java delete mode 100644 src/main/java/com/pixels/beans/Wallet.java create mode 100644 src/main/java/com/pixels/services/UserService.java diff --git a/src/main/java/com/pixels/beans/Groupe.java b/src/main/java/com/pixels/beans/Groupe.java deleted file mode 100644 index a95addd..0000000 --- a/src/main/java/com/pixels/beans/Groupe.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.pixels.beans; - -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 { - - @Id // clé primaire - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - // nom du groupe - @NonNull - @NotBlank - private String name; - - @OneToOne // le wallet commun à tous les membres du groupe - private Wallet wallet; -} diff --git a/src/main/java/com/pixels/beans/Permission.java b/src/main/java/com/pixels/beans/Permission.java deleted file mode 100644 index b5cddc3..0000000 --- a/src/main/java/com/pixels/beans/Permission.java +++ /dev/null @@ -1,28 +0,0 @@ -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 { - - @Id // clé primaire - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - // 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 0d0a541..7232484 100644 --- a/src/main/java/com/pixels/beans/Pixel.java +++ b/src/main/java/com/pixels/beans/Pixel.java @@ -48,11 +48,11 @@ public class Pixel implements Serializable { @OneToMany(mappedBy = "pixel") private List transactions; - Color getColor() { + public Color getColor() { return new Color(color); } - void setColor(Color color) { + public void setColor(Color color) { this.color = color.getRGB(); } diff --git a/src/main/java/com/pixels/beans/Role.java b/src/main/java/com/pixels/beans/Role.java deleted file mode 100644 index 7756303..0000000 --- a/src/main/java/com/pixels/beans/Role.java +++ /dev/null @@ -1,28 +0,0 @@ -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 { - - @Id // clé primaire du code - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - // 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 c0cab07..2938a34 100644 --- a/src/main/java/com/pixels/beans/Transaction.java +++ b/src/main/java/com/pixels/beans/Transaction.java @@ -8,7 +8,6 @@ import java.awt.Color; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; -import lombok.RequiredArgsConstructor; import java.io.Serializable; import java.util.Date; @@ -16,7 +15,6 @@ 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 { @@ -24,7 +22,7 @@ public class Transaction implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - // prix de la transaction* + // prix de la transaction @NonNull @NotNull private Integer price; @@ -39,17 +37,27 @@ public class Transaction implements Serializable { @NotNull private Date time; - @ManyToOne // wallet acheteur - private Wallet wallet; + // wallet acheteur + @ManyToOne + private User buyer; - @ManyToOne // pixel acheté + // pixel acheté + @ManyToOne private Pixel pixel; - Color getColor() { + public Color getColor() { return new Color(color); } - void setColor(Color color) { + public void setColor(Color color) { this.color = color.getRGB(); } + + public Transaction(Date time, Pixel pixel) { + this.time = time; + this.pixel = pixel; + this.price = pixel.getPrice(); + this.color = pixel.getColor().getRGB(); + this.buyer = pixel.getOwner(); + } } diff --git a/src/main/java/com/pixels/beans/User.java b/src/main/java/com/pixels/beans/User.java index 4913517..373a1c9 100644 --- a/src/main/java/com/pixels/beans/User.java +++ b/src/main/java/com/pixels/beans/User.java @@ -2,6 +2,7 @@ package com.pixels.beans; import javax.persistence.*; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import com.pixels.utils.Hash; @@ -39,11 +40,13 @@ public class User implements Serializable { @NotBlank private String hashPassword; - @OneToOne // portefeuille - private Wallet wallet; + // portefeuille + @OneToMany + private List pixels; - @ManyToOne // permission - private Permission permission; + // le solde de l'utilisateur + @NotNull + private Integer balance = 0; // pixels possédés @OneToMany(mappedBy = "owner") diff --git a/src/main/java/com/pixels/beans/Wallet.java b/src/main/java/com/pixels/beans/Wallet.java deleted file mode 100644 index 950af16..0000000 --- a/src/main/java/com/pixels/beans/Wallet.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.pixels.beans; - -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 { - - @Id // clé primaire du code - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - // solde du wallet - @NonNull - @NotBlank - private Integer balance; - - @OneToMany(mappedBy = "wallet") // transactions associées au wallet - private List transactions; -} diff --git a/src/main/java/com/pixels/services/PixelService.java b/src/main/java/com/pixels/services/PixelService.java index 64f5d79..0ad0309 100644 --- a/src/main/java/com/pixels/services/PixelService.java +++ b/src/main/java/com/pixels/services/PixelService.java @@ -5,9 +5,8 @@ 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.NotFoundException; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -38,16 +37,11 @@ public class PixelService { @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 String add(String json) { - Pixel pixel = gson.fromJson(json, Pixel.class); - em.persist(pixel); - return gson.toJson(pixel); + if (pixel == null) { + throw new NotFoundException(); + } else { + return gson.toJson(pixel); + } } @PUT @@ -60,13 +54,4 @@ public class PixelService { 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/java/com/pixels/services/UserService.java b/src/main/java/com/pixels/services/UserService.java new file mode 100644 index 0000000..0e2b3a6 --- /dev/null +++ b/src/main/java/com/pixels/services/UserService.java @@ -0,0 +1,80 @@ +package com.pixels.services; + +import java.util.Date; + +import javax.ejb.Singleton; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import com.google.gson.Gson; +import com.pixels.beans.Pixel; +import com.pixels.beans.Transaction; +import com.pixels.beans.User; + +@Singleton +@Path("/user") +public class UserService { + + @PersistenceContext + private EntityManager em; + + Gson gson = new Gson(); + + @GET + @Produces(MediaType.APPLICATION_JSON) + public String list() { + TypedQuery query = em.createNamedQuery("User.list", User.class); + return gson.toJson(query.getResultList()); + } + + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public String single(@PathParam("id") Long id) { + User user = em.find(User.class, id); + if (user == null) { + throw new NotFoundException(); + } else { + return gson.toJson(user); + } + } + + @GET + @Path("{id_user}/buy/{id_pixel}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public String buy_pixel(@PathParam("id") Long id_pixel, @PathParam("id") Long id_user, String json) { + // on récupère les informations du changement + Pixel pixel_new_infos = gson.fromJson(json, Pixel.class); + + // on récupère le pixel de la db via son id + Pixel pixel = em.find(Pixel.class, id_pixel); + + // on récupère le nouveau proprio + User user = em.find(User.class, id_user); + + // on update le pixel + pixel.setOwner(user); + pixel.setColor(pixel_new_infos.getColor()); + pixel.setDescription(pixel_new_infos.getDescription()); + pixel.setPrice(pixel.getPrice() + 1); + + // on ajoute la transaction + Date now = new Date(System.currentTimeMillis()); + Transaction new_transaction = new Transaction(now, pixel); + em.persist(new_transaction); + + // on update le pixel dans la db + em.merge(pixel); + + return gson.toJson(pixel); + } +}