From 733d77d7bda8010f0c36c4ea735758da00ede034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 14 Apr 2022 09:53:29 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20fin=20de=20petite=20session=20au=20calm?= =?UTF-8?q?e=20tkt,=20kestufou=20=C3=A0=20lire=20mes=20messages=20batard?= =?UTF-8?q?=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Damien Co-authored-by: pejour Co-authored-by: Tom Heurtebise --- build.gradle | 6 +- src/main/java/com/pixels/HelloServlet.java | 1 + src/main/java/com/pixels/beans/Group.java | 93 +++++++++++ .../java/com/pixels/beans/Permission.java | 67 ++++++++ src/main/java/com/pixels/beans/Pixel.java | 130 ++++++++++++++++ src/main/java/com/pixels/beans/Role.java | 67 ++++++++ .../java/com/pixels/beans/Transaction.java | 146 ++++++++++++++++++ src/main/java/com/pixels/beans/User.java | 130 ++++++++++++++++ src/main/java/com/pixels/beans/Wallet.java | 96 ++++++++++++ src/main/java/com/pixels/utils/Hash.java | 25 +++ 10 files changed, 758 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/pixels/beans/Group.java create mode 100644 src/main/java/com/pixels/beans/Permission.java create mode 100644 src/main/java/com/pixels/beans/Pixel.java create mode 100644 src/main/java/com/pixels/beans/Role.java create mode 100644 src/main/java/com/pixels/beans/Transaction.java create mode 100644 src/main/java/com/pixels/beans/User.java create mode 100644 src/main/java/com/pixels/beans/Wallet.java create mode 100644 src/main/java/com/pixels/utils/Hash.java diff --git a/build.gradle b/build.gradle index 5a154bb..6158cdf 100644 --- a/build.gradle +++ b/build.gradle @@ -21,10 +21,10 @@ dependencies { compileOnly 'javax:javaee-api:8.0.1' // Pour pouvoir parser le JSON - // implementation 'com.google.code.gson:gson:2.8.6' + implementation 'com.google.code.gson:gson:2.8.6' - // implementation 'org.hibernate:hibernate-core:5.4.30.Final' - // implementation 'mysql:mysql-connector-java:8.0.24' + implementation 'org.hibernate:hibernate-core:5.4.30.Final' + implementation 'mysql:mysql-connector-java:8.0.24' // Pour les tests testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' diff --git a/src/main/java/com/pixels/HelloServlet.java b/src/main/java/com/pixels/HelloServlet.java index 7b3d6af..42af07d 100644 --- a/src/main/java/com/pixels/HelloServlet.java +++ b/src/main/java/com/pixels/HelloServlet.java @@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletResponse; @WebServlet(name = "HelloServlet", urlPatterns = { "hello" }, loadOnStartup = 1) public class HelloServlet extends HttpServlet { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().print("Hello, World!"); diff --git a/src/main/java/com/pixels/beans/Group.java b/src/main/java/com/pixels/beans/Group.java new file mode 100644 index 0000000..0f909a0 --- /dev/null +++ b/src/main/java/com/pixels/beans/Group.java @@ -0,0 +1,93 @@ +package com.pixels.beans; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Group.list", query = "SELECT g FROM Group") +public class Group implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire + + private String name; // le nom du groupe + + @OneToOne + private Wallet wallet; // le wallet commun à tous les membres du groupe + + public Group() { + } + + public Group(Long id, String name, Wallet wallet) { + this.id = id; + this.name = name; + this.wallet = wallet; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public Wallet getWallet() { + return this.wallet; + } + + public void setWallet(Wallet wallet) { + this.wallet = wallet; + } + + public Group id(Long id) { + setId(id); + return this; + } + + public Group name(String name) { + setName(name); + return this; + } + + public Group wallet(Wallet wallet) { + setWallet(wallet); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Group)) { + return false; + } + Group group = (Group) o; + return Objects.equals(id, group.id) && Objects.equals(name, group.name) && Objects.equals(wallet, group.wallet); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, wallet); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", name='" + getName() + "'" + + ", wallet='" + getWallet() + "'" + + "}"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pixels/beans/Permission.java b/src/main/java/com/pixels/beans/Permission.java new file mode 100644 index 0000000..946e114 --- /dev/null +++ b/src/main/java/com/pixels/beans/Permission.java @@ -0,0 +1,67 @@ +package com.pixels.beans; + +import javax.persistence.*; + +import java.io.Serializable; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Permission.list", query = "SELECT pe FROM Permission pe") +public class Permission implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire du code + + private int type; // type de la permission + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getType() { + return this.type; + } + + public void setType(int type) { + this.type = type; + } + + public Permission id(Long id) { + setId(id); + return this; + } + + public Permission type(int type) { + setType(type); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Permission)) { + return false; + } + Permission permission = (Permission) o; + return Objects.equals(id, permission.id) && type == permission.type; + } + + @Override + public int hashCode() { + return Objects.hash(id, type); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", balance='" + getType() + "'" + + "}"; + } +} \ No newline at end of file diff --git a/src/main/java/com/pixels/beans/Pixel.java b/src/main/java/com/pixels/beans/Pixel.java new file mode 100644 index 0000000..5499b7b --- /dev/null +++ b/src/main/java/com/pixels/beans/Pixel.java @@ -0,0 +1,130 @@ +package com.pixels.beans; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Pixel.list", query = "SELECT p FROM Pixel p order by p.dateTime desc") +public class Pixel implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire du code + + private int price; // prix du pixel à l'achat + private int color; // couleur du pixel sur la peinture + private String description; // petit mot écrit par le user + + @ManyToOne(fetch = FetchType.EAGER) + private User owner; + + @OneToMany(mappedBy = "pixel") + private List transactions; + + public Pixel() { + } + + public Pixel(Long id, int price, int color, String description, User owner) { + this.id = id; + this.price = price; + this.color = color; + this.description = description; + this.owner = owner; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getPrice() { + return this.price; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getColor() { + return this.color; + } + + public void setColor(int color) { + this.color = color; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public User getOwner() { + return this.owner; + } + + public void setOwner(User owner) { + this.owner = owner; + } + + public Pixel id(Long id) { + setId(id); + return this; + } + + public Pixel price(int price) { + setPrice(price); + return this; + } + + public Pixel color(int color) { + setColor(color); + return this; + } + + public Pixel description(String description) { + setDescription(description); + return this; + } + + public Pixel owner(User owner) { + setOwner(owner); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Pixel)) { + return false; + } + Pixel pixel = (Pixel) o; + return id.equals(pixel.id) && price == pixel.price && color == pixel.color + && description.equals(pixel.description) && owner.equals(pixel.owner); + } + + @Override + public int hashCode() { + return Objects.hash(id, price, color, description, owner); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", price='" + getPrice() + "'" + + ", color='" + getColor() + "'" + + ", description='" + getDescription() + "'" + + ", owner='" + getOwner() + "'" + + "}"; + } + +} diff --git a/src/main/java/com/pixels/beans/Role.java b/src/main/java/com/pixels/beans/Role.java new file mode 100644 index 0000000..c4663f0 --- /dev/null +++ b/src/main/java/com/pixels/beans/Role.java @@ -0,0 +1,67 @@ +package com.pixels.beans; + +import javax.persistence.*; + +import java.io.Serializable; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Role.list", query = "SELECT r FROM Role r") +public class Role implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire du code + + private int type; // type du role + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getType() { + return this.type; + } + + public void setType(int type) { + this.type = type; + } + + public Role id(Long id) { + setId(id); + return this; + } + + public Role type(int type) { + setType(type); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Role)) { + return false; + } + Role role = (Role) o; + return Objects.equals(id, role.id) && type == role.type; + } + + @Override + public int hashCode() { + return Objects.hash(id, type); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", balance='" + getType() + "'" + + "}"; + } +} \ No newline at end of file diff --git a/src/main/java/com/pixels/beans/Transaction.java b/src/main/java/com/pixels/beans/Transaction.java new file mode 100644 index 0000000..6c140fe --- /dev/null +++ b/src/main/java/com/pixels/beans/Transaction.java @@ -0,0 +1,146 @@ +package com.pixels.beans; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.Date; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Transaction.list", query = "SELECT t FROM Transaction t order by t.dateTime desc") // TO DO : + // Vérifier +public class Transaction implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire + + private int price; + private int color; // couleur du pixel sur la peinture + private Date time; + + @ManyToOne + private Wallet wallet; + @ManyToOne + private Pixel pixel; + + public Transaction() { + } + + public Transaction(Long id, int price, int color, Date time, Wallet wallet, Pixel pixel) { + this.id = id; + this.price = price; + this.color = color; + this.time = time; + this.wallet = wallet; + this.pixel = pixel; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getPrice() { + return this.price; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getColor() { + return this.color; + } + + public void setColor(int color) { + this.color = color; + } + + public Date getTime() { + return this.time; + } + + public void setTime(Date time) { + this.time = time; + } + + public Wallet getWallet() { + return this.wallet; + } + + public void setWallet(Wallet wallet) { + this.wallet = wallet; + } + + public Pixel getPixel() { + return this.pixel; + } + + public void setPixel(Pixel pixel) { + this.pixel = pixel; + } + + public Transaction id(Long id) { + setId(id); + return this; + } + + public Transaction price(int price) { + setPrice(price); + return this; + } + + public Transaction color(int color) { + setColor(color); + return this; + } + + public Transaction time(Date time) { + setTime(time); + return this; + } + + public Transaction wallet(Wallet wallet) { + setWallet(wallet); + return this; + } + + public Transaction pixel(Pixel pixel) { + setPixel(pixel); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Transaction)) { + return false; + } + Transaction transaction = (Transaction) o; + return Objects.equals(id, transaction.id) && price == transaction.price && color == transaction.color + && Objects.equals(time, transaction.time) && Objects.equals(wallet, transaction.wallet) + && Objects.equals(pixel, transaction.pixel); + } + + @Override + public int hashCode() { + return Objects.hash(id, price, color, time, wallet, pixel); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", price='" + getPrice() + "'" + + ", color='" + getColor() + "'" + + ", time='" + getTime() + "'" + + ", wallet='" + getWallet() + "'" + + ", pixel='" + getPixel() + "'" + + "}"; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pixels/beans/User.java b/src/main/java/com/pixels/beans/User.java new file mode 100644 index 0000000..8fbfd0f --- /dev/null +++ b/src/main/java/com/pixels/beans/User.java @@ -0,0 +1,130 @@ +package com.pixels.beans; + +import javax.persistence.*; + +import com.pixels.utils.Hash; + +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +@Entity +@NamedQuery(name = "User.list", query = "SELECT u FROM User u") +public class User implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire du code + + private String userName; + private String email; + private String hashPassword; // sha256 + + @OneToOne + private Wallet wallet; + + @ManyToOne + private Permission permission; + + @OneToMany(mappedBy = "owner") + private List owned; + + public User() { + } + + public User(Long id, String userName, String email, String password) { + this.id = id; + this.userName = userName; + this.email = email; + this.hashPassword = Hash.sha256(password); + ; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return this.userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getEmail() { + return this.email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Wallet getWallet() { + return this.wallet; + } + + public void setWallet(Wallet wallet) { + this.wallet = wallet; + } + + public String getHashPassword() { + return this.hashPassword; + } + + public void setHashPassword(String password) { + this.hashPassword = Hash.sha256(password); + } + + public User id(Long id) { + setId(id); + return this; + } + + public User userName(String userName) { + setUserName(userName); + return this; + } + + public User email(String email) { + setEmail(email); + return this; + } + + public User hashPassword(String hashPassword) { + setHashPassword(hashPassword); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof User)) { + return false; + } + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(userName, user.userName) + && Objects.equals(email, user.email) && Objects.equals(hashPassword, user.hashPassword); + } + + @Override + public int hashCode() { + return Objects.hash(id, userName, email, hashPassword); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", userName='" + getUserName() + "'" + + ", email='" + getEmail() + "'" + + ", hashPassword='" + getHashPassword() + "'" + + "}"; + } + +} diff --git a/src/main/java/com/pixels/beans/Wallet.java b/src/main/java/com/pixels/beans/Wallet.java new file mode 100644 index 0000000..5d0b996 --- /dev/null +++ b/src/main/java/com/pixels/beans/Wallet.java @@ -0,0 +1,96 @@ +package com.pixels.beans; + +import javax.persistence.*; + +import java.io.Serializable; +import java.util.List; +import java.util.Objects; + +@Entity +@NamedQuery(name = "Wallet.list", query = "SELECT w FROM Wallet w") +public class Wallet implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; // clé primaire du code + + private int balance; + + @OneToMany(mappedBy = "wallet") + private List transactions; + + public Wallet() { + } + + public Wallet(Long id, int balance, List transactions) { + this.id = id; + this.balance = balance; + this.transactions = transactions; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getBalance() { + return this.balance; + } + + public void setBalance(int balance) { + this.balance = balance; + } + + public List getTransactions() { + return this.transactions; + } + + public void setTransactions(List transactions) { + this.transactions = transactions; + } + + public Wallet id(Long id) { + setId(id); + return this; + } + + public Wallet balance(int balance) { + setBalance(balance); + return this; + } + + public Wallet transactions(List transactions) { + setTransactions(transactions); + return this; + } + + @Override + public boolean equals(Object o) { + if (o == this) + return true; + if (!(o instanceof Wallet)) { + return false; + } + Wallet wallet = (Wallet) o; + return Objects.equals(id, wallet.id) && balance == wallet.balance + && Objects.equals(transactions, wallet.transactions); + } + + @Override + public int hashCode() { + return Objects.hash(id, balance, transactions); + } + + @Override + public String toString() { + return "{" + + " id='" + getId() + "'" + + ", balance='" + getBalance() + "'" + + ", transactions='" + getTransactions() + "'" + + "}"; + } + +} diff --git a/src/main/java/com/pixels/utils/Hash.java b/src/main/java/com/pixels/utils/Hash.java new file mode 100644 index 0000000..ffcd6a1 --- /dev/null +++ b/src/main/java/com/pixels/utils/Hash.java @@ -0,0 +1,25 @@ +package com.pixels.utils; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Hash { + public static String sha256(String password) { + String hash = ""; + byte[] hashBytes; + try { + hashBytes = MessageDigest.getInstance("SHA-256") + .digest(password.getBytes(StandardCharsets.UTF_8)); + StringBuilder sb = new StringBuilder(); + for (byte b : hashBytes) { + sb.append(String.format("%02X", b)); + } + hash = sb.toString(); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return hash; + } +}