feat: début d'un backend

This commit is contained in:
Laureηt 2022-05-12 19:03:51 +02:00
parent b4abd530e6
commit 033fc5851c
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
9 changed files with 104 additions and 14 deletions

View file

@ -4,12 +4,16 @@ import javax.persistence.*;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Groupe.list", query = "SELECT g FROM Groupe g") @NamedQuery(name = "Groupe.list", query = "SELECT g FROM Groupe g")
public class Groupe implements Serializable { public class Groupe implements Serializable {

View file

@ -1,14 +1,19 @@
package com.pixels.beans; package com.pixels.beans;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Permission.list", query = "SELECT pe FROM Permission pe") @NamedQuery(name = "Permission.list", query = "SELECT pe FROM Permission pe")
public class Permission implements Serializable { public class Permission implements Serializable {
@ -18,5 +23,6 @@ public class Permission implements Serializable {
// type de la permission // type de la permission
@NonNull @NonNull
@NotNull
private Integer type; private Integer type;
} }

View file

@ -1,9 +1,13 @@
package com.pixels.beans; package com.pixels.beans;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.awt.Color; import java.awt.Color;
@ -12,6 +16,8 @@ import java.util.List;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Pixel.list", query = "SELECT p FROM Pixel p") @NamedQuery(name = "Pixel.list", query = "SELECT p FROM Pixel p")
public class Pixel implements Serializable { public class Pixel implements Serializable {
@ -21,14 +27,17 @@ public class Pixel implements Serializable {
// prix du pixel à l'achat // prix du pixel à l'achat
@NonNull @NonNull
@NotNull
private Integer price; private Integer price;
// couleur du pixel sur la peinture // couleur du pixel sur la peinture
@NonNull @NonNull
private Color color; @NotNull
private Integer color;
// petit mot écrit par le user // petit mot écrit par le user
@NonNull @NonNull
@NotBlank
private String description; private String description;
// propriétaire actuel du pixel // propriétaire actuel du pixel
@ -38,4 +47,18 @@ public class Pixel implements Serializable {
// liste des transactions associées au pixel // liste des transactions associées au pixel
@OneToMany(mappedBy = "pixel") @OneToMany(mappedBy = "pixel")
private List<Transaction> transactions; private List<Transaction> 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;
}
} }

View file

@ -1,14 +1,19 @@
package com.pixels.beans; package com.pixels.beans;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Role.list", query = "SELECT r FROM Role r") @NamedQuery(name = "Role.list", query = "SELECT r FROM Role r")
public class Role implements Serializable { public class Role implements Serializable {
@ -18,5 +23,6 @@ public class Role implements Serializable {
// type du role // type du role
@NonNull @NonNull
@NotNull
private Integer type; private Integer type;
} }

View file

@ -1,17 +1,22 @@
package com.pixels.beans; package com.pixels.beans;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.awt.Color; import java.awt.Color;
import lombok.Data; import lombok.Data;
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;
@Data @Data
@Entity @Entity
@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 {
@ -21,14 +26,17 @@ public class Transaction implements Serializable {
// prix de la transaction* // prix de la transaction*
@NonNull @NonNull
@NotNull
private Integer price; private Integer price;
// couleur du pixel de la transaction // couleur du pixel de la transaction
@NonNull @NonNull
private Color color; @NotNull
private Integer color;
// date de la transaction // date de la transaction
@NonNull @NonNull
@NotNull
private Date time; private Date time;
@ManyToOne // wallet acheteur @ManyToOne // wallet acheteur
@ -36,4 +44,12 @@ public class Transaction implements Serializable {
@ManyToOne // pixel acheté @ManyToOne // pixel acheté
private Pixel pixel; private Pixel pixel;
Color getColor() {
return new Color(color);
}
void setColor(Color color) {
this.color = color.getRGB();
}
} }

View file

@ -6,13 +6,17 @@ import javax.validation.constraints.NotBlank;
import com.pixels.utils.Hash; import com.pixels.utils.Hash;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "User.list", query = "SELECT u FROM User u") @NamedQuery(name = "User.list", query = "SELECT u FROM User u")
public class User implements Serializable { public class User implements Serializable {

View file

@ -4,13 +4,17 @@ import javax.persistence.*;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
@Data @Data
@Entity @Entity
@NoArgsConstructor
@RequiredArgsConstructor
@NamedQuery(name = "Wallet.list", query = "SELECT w FROM Wallet w") @NamedQuery(name = "Wallet.list", query = "SELECT w FROM Wallet w")
public class Wallet implements Serializable { public class Wallet implements Serializable {

View file

@ -1,7 +1,5 @@
package com.pixels.services; package com.pixels.services;
import java.util.List;
import java.awt.Color; import java.awt.Color;
import javax.ejb.Singleton; import javax.ejb.Singleton;
@ -10,10 +8,14 @@ import javax.json.JsonObject;
import javax.json.JsonObjectBuilder; import javax.json.JsonObjectBuilder;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
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.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@ -32,22 +34,44 @@ public class PixelService {
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public String list() { public String list() {
List<Pixel> query = em.createNamedQuery("Pixel.list", Pixel.class).getResultList(); TypedQuery<Pixel> query = em.createNamedQuery("Pixel.list", Pixel.class);
return gson.toJson(query); 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 @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public JsonObject add() { public String add(String json) {
Pixel pixel = gson.fromJson(json, Pixel.class);
JsonObjectBuilder response = Json.createObjectBuilder();
Pixel pixel = new Pixel(10, new Color(255, 0, 0), "un pixel Poog");
em.persist(pixel); em.persist(pixel);
return gson.toJson(pixel);
response.add("status", "pixel created !");
return response.build();
} }
@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);
}
} }

View file

@ -0,0 +1,3 @@
<jboss-web>
<context-root>/</context-root>
</jboss-web>