feat: update backend
This commit is contained in:
parent
21568c4d0f
commit
9f58270be8
|
@ -1,6 +1,7 @@
|
||||||
package com.pixels.beans;
|
package com.pixels.beans;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
@ -27,6 +28,7 @@ public class Pixel implements Serializable {
|
||||||
// prix du pixel à l'achat
|
// prix du pixel à l'achat
|
||||||
@NonNull
|
@NonNull
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Min(value = 0)
|
||||||
private Integer price;
|
private Integer price;
|
||||||
|
|
||||||
// couleur du pixel sur la peinture
|
// couleur du pixel sur la peinture
|
||||||
|
@ -38,7 +40,6 @@ public class Pixel implements Serializable {
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.pixels.beans;
|
package com.pixels.beans;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
@ -36,6 +37,7 @@ public class User implements Serializable {
|
||||||
|
|
||||||
// le solde de l'utilisateur
|
// le solde de l'utilisateur
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@Min(value = 0)
|
||||||
private Integer balance = 0;
|
private Integer balance = 0;
|
||||||
|
|
||||||
@Column(unique = true)
|
@Column(unique = true)
|
||||||
|
|
|
@ -8,6 +8,7 @@ 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.CookieParam;
|
import javax.ws.rs.CookieParam;
|
||||||
|
import javax.ws.rs.FormParam;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.NotFoundException;
|
import javax.ws.rs.NotFoundException;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
|
@ -70,36 +71,17 @@ public class PixelService {
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("{id}/buy/")
|
@Path("{id}/buy/")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
public void buy_pixel(@PathParam("id") Long id, @CookieParam("JSESSIONID") Cookie cookie) {
|
||||||
public String buy_pixel(@PathParam("id") Long id, @CookieParam("JSESSIONID") Cookie cookie, String json) {
|
|
||||||
// on récupère les informations du changement
|
|
||||||
Pixel pixel_json = gson.fromJson(json, Pixel.class);
|
|
||||||
|
|
||||||
// on récupère le pixel de la db via son id
|
// on récupère le pixel de la db via son id
|
||||||
Pixel pixel = em.find(Pixel.class, id);
|
Pixel pixel = em.find(Pixel.class, id);
|
||||||
|
|
||||||
// on récupère le nouveau proprio
|
// on récupère le nouveau proprio
|
||||||
User user = User.fromSessionID(cookie.getValue(), em);
|
User user = User.fromSessionID(cookie.getValue(), em);
|
||||||
|
|
||||||
if (user.getBalance() >= pixel.getPrice() && pixel_json.getPrice() > pixel.getPrice()) {
|
if (user.getBalance() >= pixel.getPrice()) {
|
||||||
// on récupère l'ancien owner
|
// on update l'owner
|
||||||
User old_owner = pixel.getOwner();
|
|
||||||
|
|
||||||
// on update le pixel
|
|
||||||
pixel.setOwner(user);
|
pixel.setOwner(user);
|
||||||
pixel.setColor(pixel_json.getColor());
|
|
||||||
pixel.setDescription(pixel_json.getDescription());
|
|
||||||
pixel.setPrice(pixel_json.getPrice());
|
|
||||||
|
|
||||||
//Si le user possède le pixel on le débite pas
|
|
||||||
if (old_owner.getUsername().equals(user.getUsername())){
|
|
||||||
// on débite le user
|
|
||||||
user.setBalance(user.getBalance() - pixel_json.getPrice());
|
|
||||||
|
|
||||||
// on crédite l'ancien owner
|
|
||||||
old_owner.setBalance(old_owner.getBalance() + pixel_json.getPrice());
|
|
||||||
}
|
|
||||||
|
|
||||||
// on ajoute la transaction
|
// on ajoute la transaction
|
||||||
Date now = new Date(System.currentTimeMillis());
|
Date now = new Date(System.currentTimeMillis());
|
||||||
|
@ -110,8 +92,33 @@ public class PixelService {
|
||||||
em.merge(pixel);
|
em.merge(pixel);
|
||||||
em.merge(user);
|
em.merge(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
return gson.toJson(pixel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("{id}/modify/")
|
||||||
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
|
||||||
|
public void modify(@PathParam("id") Long id, @CookieParam("JSESSIONID") Cookie cookie,
|
||||||
|
@FormParam("red") int red, @FormParam("green") int green, @FormParam("blue") int blue,
|
||||||
|
@FormParam("price") int price, @FormParam("description") String description) {
|
||||||
|
// on récupère le pixel de la db via son id
|
||||||
|
Pixel pixel = em.find(Pixel.class, id);
|
||||||
|
|
||||||
|
// on récupère le nouveau proprio
|
||||||
|
User user = User.fromSessionID(cookie.getValue(), em);
|
||||||
|
|
||||||
|
if (user.equals(pixel.getOwner())) {
|
||||||
|
pixel.setPrice(price);
|
||||||
|
pixel.setDescription(description);
|
||||||
|
pixel.setColor(String.format("%02X%02X%02X", red, green, blue));
|
||||||
|
|
||||||
|
// on ajoute la transaction
|
||||||
|
Date now = new Date(System.currentTimeMillis());
|
||||||
|
Transaction new_transaction = new Transaction(now, pixel);
|
||||||
|
em.persist(new_transaction);
|
||||||
|
|
||||||
|
// on commit les modifs dans la db
|
||||||
|
em.merge(pixel);
|
||||||
|
em.merge(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,4 +140,24 @@ public class UserService {
|
||||||
LOGGER.info("user not logged in, cannot logout");
|
LOGGER.info("user not logged in, cannot logout");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("pay")
|
||||||
|
public void pay(@CookieParam("JSESSIONID") Cookie cookie, @FormParam("amount") int amount) {
|
||||||
|
if (amount > 0) {
|
||||||
|
try {
|
||||||
|
// on trouve l'utilisateur connecté via son sessionID
|
||||||
|
User user = User.fromSessionID(cookie.getValue(), em);
|
||||||
|
|
||||||
|
// on rajoute l'argent
|
||||||
|
user.setBalance(user.getBalance() + amount);
|
||||||
|
|
||||||
|
// on commit les modifs dans la db
|
||||||
|
em.merge(user);
|
||||||
|
} catch (NoResultException e) {
|
||||||
|
LOGGER.info("user not logged in, cannot logout");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue