refactor!: modification des beans + rest

This commit is contained in:
Laureηt 2022-05-13 14:27:19 +02:00
parent d91b272bc1
commit 52bba8c256
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
9 changed files with 111 additions and 154 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -48,11 +48,11 @@ public class Pixel implements Serializable {
@OneToMany(mappedBy = "pixel") @OneToMany(mappedBy = "pixel")
private List<Transaction> transactions; private List<Transaction> transactions;
Color getColor() { public Color getColor() {
return new Color(color); return new Color(color);
} }
void setColor(Color color) { public void setColor(Color color) {
this.color = color.getRGB(); this.color = color.getRGB();
} }

View file

@ -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;
}

View file

@ -8,7 +8,6 @@ import java.awt.Color;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -16,7 +15,6 @@ import java.util.Date;
@Data @Data
@Entity @Entity
@NoArgsConstructor @NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Transaction.list", query = "SELECT t FROM Transaction t order by t.time desc") @NamedQuery(name = "Transaction.list", query = "SELECT t FROM Transaction t order by t.time desc")
public class Transaction implements Serializable { public class Transaction implements Serializable {
@ -24,7 +22,7 @@ public class Transaction implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
// prix de la transaction* // prix de la transaction
@NonNull @NonNull
@NotNull @NotNull
private Integer price; private Integer price;
@ -39,17 +37,27 @@ public class Transaction implements Serializable {
@NotNull @NotNull
private Date time; private Date time;
@ManyToOne // wallet acheteur // wallet acheteur
private Wallet wallet; @ManyToOne
private User buyer;
@ManyToOne // pixel acheté // pixel acheté
@ManyToOne
private Pixel pixel; private Pixel pixel;
Color getColor() { public Color getColor() {
return new Color(color); return new Color(color);
} }
void setColor(Color color) { public void setColor(Color color) {
this.color = color.getRGB(); 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();
}
} }

View file

@ -2,6 +2,7 @@ package com.pixels.beans;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import com.pixels.utils.Hash; import com.pixels.utils.Hash;
@ -39,11 +40,13 @@ public class User implements Serializable {
@NotBlank @NotBlank
private String hashPassword; private String hashPassword;
@OneToOne // portefeuille // portefeuille
private Wallet wallet; @OneToMany
private List<Pixel> pixels;
@ManyToOne // permission // le solde de l'utilisateur
private Permission permission; @NotNull
private Integer balance = 0;
// pixels possédés // pixels possédés
@OneToMany(mappedBy = "owner") @OneToMany(mappedBy = "owner")

View file

@ -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<Transaction> transactions;
}

View file

@ -5,9 +5,8 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
@ -38,16 +37,11 @@ public class PixelService {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String single(@PathParam("id") Long id) { public String single(@PathParam("id") Long id) {
Pixel pixel = em.find(Pixel.class, id); Pixel pixel = em.find(Pixel.class, id);
if (pixel == null) {
throw new NotFoundException();
} else {
return gson.toJson(pixel); 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);
} }
@PUT @PUT
@ -60,13 +54,4 @@ public class PixelService {
em.merge(pixel); em.merge(pixel);
return gson.toJson(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);
}
} }

View file

@ -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<User> 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);
}
}