Merge branch 'master' of git.inpt.fr:tocard-inc/enseeiht/projet-web

This commit is contained in:
gdamms 2022-05-20 10:59:16 +02:00
commit a1deb49b99
12 changed files with 222 additions and 154 deletions

View file

@ -10,3 +10,6 @@ end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true
[*.yml]
indent_size = 2

View file

@ -34,6 +34,7 @@ services:
- MYSQL_PASSWORD=${MYSQL_PASSWORD} - MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
volumes: volumes:
- ./mariadb-init:/docker-entrypoint-initdb.d/ # seed volume
- ./mariadb-volume/:/var/lib/mysql/ # data storage - ./mariadb-volume/:/var/lib/mysql/ # data storage
ports: ports:
- "3306:3306" - "3306:3306"

View file

@ -0,0 +1,107 @@
CREATE TABLE Pixel (
id bigint(20) NOT NULL AUTO_INCREMENT,
price int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO Pixel(id, price) VALUES
(1,1),
(2,1),
(3,1),
(4,1),
(5,1),
(6,1),
(7,1),
(8,1),
(9,1),
(10,1),
(11,1),
(12,1),
(13,1),
(14,1),
(15,1),
(16,1),
(17,1),
(18,1),
(19,1),
(20,1),
(21,1),
(22,1),
(23,1),
(24,1),
(25,1),
(26,1),
(27,1),
(28,1),
(29,1),
(30,1),
(31,1),
(32,1),
(33,1),
(34,1),
(35,1),
(36,1),
(37,1),
(38,1),
(39,1),
(40,1),
(41,1),
(42,1),
(43,1),
(44,1),
(45,1),
(46,1),
(47,1),
(48,1),
(49,1),
(50,1),
(51,1),
(52,1),
(53,1),
(54,1),
(55,1),
(56,1),
(57,1),
(58,1),
(59,1),
(60,1),
(61,1),
(62,1),
(63,1),
(64,1),
(65,1),
(66,1),
(67,1),
(68,1),
(69,1),
(70,1),
(71,1),
(72,1),
(73,1),
(74,1),
(75,1),
(76,1),
(77,1),
(78,1),
(79,1),
(80,1),
(81,1),
(82,1),
(83,1),
(84,1),
(85,1),
(86,1),
(87,1),
(88,1),
(89,1),
(90,1),
(91,1),
(92,1),
(93,1),
(94,1),
(95,1),
(96,1),
(97,1),
(98,1),
(99,1),
(100,1);

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