Added user stats
continuous-integration/drone/push Build is passing Details

Quarkus_Continue
root 2 years ago
parent 968497e1b1
commit 91b2ad7e90

@ -3,7 +3,7 @@ package org.acme.api;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.acme.api.service.*; import org.acme.api.repository.*;
@Singleton @Singleton
public class BowlDbContext { public class BowlDbContext {

@ -16,7 +16,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import org.acme.api.dto.GameDto; import org.acme.api.dto.GameDTO;
import org.acme.api.manager.DbManager; import org.acme.api.manager.DbManager;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -33,9 +33,9 @@ public class GameController {
DbManager dbManager; DbManager dbManager;
@GET @GET
public Uni<List<GameDto>> getUsers() { public Uni<List<GameDTO>> getUsers() {
LOGGER.info("Getting all game"); LOGGER.info("Getting all game");
Uni<List<GameDto>> allGames = dbManager.gameManager.getAllGames(); Uni<List<GameDTO>> allGames = dbManager.gameManager.getAllGames();
return allGames; return allGames;
} }
@ -54,7 +54,7 @@ public class GameController {
@POST @POST
@ReactiveTransactional @ReactiveTransactional
public Uni<Response> createGame(GameDto game) { public Uni<Response> createGame(GameDTO game) {
if (game == null) { if (game == null) {
throw new WebApplicationException("user was invalidly set on request.", 422); throw new WebApplicationException("user was invalidly set on request.", 422);
} }

@ -11,8 +11,8 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import org.acme.api.BowlDbContext; import org.acme.api.BowlDbContext;
import org.acme.api.dto.UserDto; import org.acme.api.dto.UserDTO;
import org.acme.api.dto.UserTinyDto; import org.acme.api.dto.UserTinyDTO;
import org.acme.api.mapper.UserMapper; import org.acme.api.mapper.UserMapper;
import org.eclipse.microprofile.openapi.annotations.Operation; import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
@ -35,7 +35,7 @@ public class UserController {
@GET @GET
@Operation(summary = "Get all users") @Operation(summary = "Get all users")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<List<UserDto>> getUsers() { public Uni<List<UserDTO>> getUsers() {
LOGGER.info("Get all users"); LOGGER.info("Get all users");
return service.userRepository.findAll().list() return service.userRepository.findAll().list()
.map(entities -> entities.stream().map(UserMapper::toDto).collect(Collectors.toList())); .map(entities -> entities.stream().map(UserMapper::toDto).collect(Collectors.toList()));
@ -97,7 +97,7 @@ public class UserController {
@APIResponse(responseCode = "201", description = "User successfully created") @APIResponse(responseCode = "201", description = "User successfully created")
@APIResponse(responseCode = "422", description = "User invalidly set on request") @APIResponse(responseCode = "422", description = "User invalidly set on request")
@ReactiveTransactional @ReactiveTransactional
public Uni<Response> createUser(UserTinyDto user) { public Uni<Response> createUser(UserTinyDTO user) {
if (user == null) { if (user == null) {
throw new WebApplicationException("user was invalidly set on request.", 422); throw new WebApplicationException("user was invalidly set on request.", 422);
} }
@ -129,7 +129,7 @@ public class UserController {
@APIResponse(responseCode = "404", description = "User not found") @APIResponse(responseCode = "404", description = "User not found")
@Path("/{id}") @Path("/{id}")
@ReactiveTransactional @ReactiveTransactional
public Uni<Response> updateUser(@PathParam("id") Long id, UserTinyDto newUser) { public Uni<Response> updateUser(@PathParam("id") Long id, UserTinyDTO newUser) {
LOGGER.info("Update user with id : " + id); LOGGER.info("Update user with id : " + id);
return service.userRepository.findById(id) return service.userRepository.findById(id)
.onItem().ifNull().failWith(() -> new WebApplicationException("User not found", Status.NOT_FOUND)) .onItem().ifNull().failWith(() -> new WebApplicationException("User not found", Status.NOT_FOUND))

@ -41,13 +41,13 @@ import java.time.LocalDate;
@RegisterForReflection // Annotation pour permettre l'utilisation avec Quarkus @RegisterForReflection // Annotation pour permettre l'utilisation avec Quarkus
@Schema(description = "A DTO for transferring game details") @Schema(description = "A DTO for transferring game details")
public class GameDto { public class GameDTO {
@JsonProperty("id") @JsonProperty("id")
public Long id; public Long id;
@JsonProperty("players") @JsonProperty("players")
public Uni<List<ParticipeDto>> players; public Uni<List<ParticipeDTO>> players;
@JsonProperty("date") @JsonProperty("date")
@JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd")
@ -57,23 +57,23 @@ public class GameDto {
public Long hostID; public Long hostID;
@JsonProperty("winner") @JsonProperty("winner")
public UserTinyDto winner; public UserTinyDTO winner;
@JsonIgnore @JsonIgnore
@JsonProperty("rounds") @JsonProperty("rounds")
public Uni<List<RoundDto>> rounds; public Uni<List<RoundDTO>> rounds;
public GameDto() { public GameDTO() {
// Constructeur vide pour la désérialisation // Constructeur vide pour la désérialisation
} }
// Constructeur avec tous les champs sauf l'ID (généré automatiquement) // Constructeur avec tous les champs sauf l'ID (généré automatiquement)
public GameDto(Long id, public GameDTO(Long id,
Uni<List<ParticipeDto>> players, Uni<List<ParticipeDTO>> players,
LocalDate time, LocalDate time,
Long ownerGame, Long ownerGame,
UserTinyDto winner, UserTinyDTO winner,
Uni<List<RoundDto>> rounds) { Uni<List<RoundDTO>> rounds) {
this.players = players; this.players = players;
this.date = time; this.date = time;
this.hostID = ownerGame; this.hostID = ownerGame;

@ -5,7 +5,7 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema;
import io.quarkus.runtime.annotations.RegisterForReflection; import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection @RegisterForReflection
public class ParticipeDto { public class ParticipeDTO {
public Long idUser; public Long idUser;
@Schema(description = "Name of the guest") @Schema(description = "Name of the guest")
@ -15,9 +15,9 @@ public class ParticipeDto {
public int totalPoints; public int totalPoints;
@Schema(description = "The User entity that this Participe belongs to") @Schema(description = "The User entity that this Participe belongs to")
public UserDto user; public UserDTO user;
public ParticipeDto(Long idUser, UserDto user, Integer totalPoints, String guestName) { public ParticipeDTO(Long idUser, UserDTO user, Integer totalPoints, String guestName) {
this.idUser = idUser; this.idUser = idUser;
this.guestName = guestName; this.guestName = guestName;

@ -3,17 +3,17 @@ package org.acme.api.dto;
import io.quarkus.runtime.annotations.RegisterForReflection; import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection @RegisterForReflection
public class RoundDto { public class RoundDTO {
private int playerPosition; private int playerPosition;
private int turn; private int turn;
private int pinsFirstThrow; private int pinsFirstThrow;
private int pinsSecondThrow; private int pinsSecondThrow;
private int points; private int points;
public RoundDto() { public RoundDTO() {
} }
public RoundDto(int playerPosition, int turn, int pinsFirstThrow, int pinsSecondThrow, int points) { public RoundDTO(int playerPosition, int turn, int pinsFirstThrow, int pinsSecondThrow, int points) {
this.playerPosition = playerPosition; this.playerPosition = playerPosition;
this.turn = turn; this.turn = turn;
this.pinsFirstThrow = pinsFirstThrow; this.pinsFirstThrow = pinsFirstThrow;

@ -5,19 +5,20 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema;
@RegisterForReflection @RegisterForReflection
@Schema(description = "A DTO for transferring user details") @Schema(description = "A DTO for transferring user details")
public class UserDto { public class UserDTO {
public Long id; public Long id;
public String name; public String name;
public String image; public String image;
public String mail; public String mail;
//public UserStatsDTO stats; public UserStatsDTO stats;
public UserDto(Long id, String name, String image, String mail) { public UserDTO(Long id, String name, String image, String mail, UserStatsDTO stats) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.image = image; this.image = image;
this.mail = mail; this.mail = mail;
this.stats = stats;
} }
/*public UserDTO(Long id, String name, /*public UserDTO(Long id, String name,

@ -5,7 +5,7 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema;
@RegisterForReflection @RegisterForReflection
@Schema(description = "A DTO describing the stats of a User") @Schema(description = "A DTO describing the stats of a User")
public class UserStatsDto { public class UserStatsDTO {
public Long nbVictories; public Long nbVictories;
public Long nbGames; public Long nbGames;
public Long highscore; public Long highscore;
@ -14,7 +14,7 @@ public class UserStatsDto {
public Double avgScore; public Double avgScore;
public Double avgPinsPerRound; public Double avgPinsPerRound;
public UserStatsDto(Long nbVictories, Long nbGames, Long highscore, Long nbStrikes, Long nbSpares, Double avgScore, public UserStatsDTO(Long nbVictories, Long nbGames, Long highscore, Long nbStrikes, Long nbSpares, Double avgScore,
Double avgPinsPerRound) { Double avgPinsPerRound) {
this.nbVictories = nbVictories; this.nbVictories = nbVictories;
this.nbGames = nbGames; this.nbGames = nbGames;

@ -5,13 +5,13 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema;
@RegisterForReflection @RegisterForReflection
@Schema(description = "A minimal DTO for transferring user information") @Schema(description = "A minimal DTO for transferring user information")
public class UserTinyDto { public class UserTinyDTO {
public String name; public String name;
public String image; public String image;
public String mail; public String mail;
public String password; public String password;
public UserTinyDto(String name, String image, String mail, String password) { public UserTinyDTO(String name, String image, String mail, String password) {
this.name = name; this.name = name;
this.image = image; this.image = image;
this.mail = mail; this.mail = mail;

@ -5,7 +5,7 @@ import java.util.stream.Collectors;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import org.acme.api.dto.GameDto; import org.acme.api.dto.GameDTO;
import org.acme.api.mapper.GameMapper; import org.acme.api.mapper.GameMapper;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
@ -18,18 +18,18 @@ public class GameManager {
this.dbManager = dbManager; this.dbManager = dbManager;
} }
public Uni<GameDto> saveGame(GameDto game) { public Uni<GameDTO> saveGame(GameDTO game) {
return dbManager.dbContext.gameRepository.persist(GameMapper.toEntity(game, dbManager.dbContext)) return dbManager.dbContext.gameRepository.persist(GameMapper.toEntity(game, dbManager.dbContext))
.onItem().transform(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext)); .onItem().transform(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext));
} }
public Uni<GameDto> getDetailsGameById(Long gameId) { public Uni<GameDTO> getDetailsGameById(Long gameId) {
return dbManager.dbContext.gameRepository.findById(gameId) return dbManager.dbContext.gameRepository.findById(gameId)
.onItem().ifNull().failWith(new NotFoundException("Game not found")) .onItem().ifNull().failWith(new NotFoundException("Game not found"))
.onItem().transform(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext)); .onItem().transform(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext));
} }
public Uni<List<GameDto>> getAllGames() { public Uni<List<GameDTO>> getAllGames() {
return dbManager.dbContext.gameRepository.findAll().list() return dbManager.dbContext.gameRepository.findAll().list()
.onItem().transform(games -> games.stream() .onItem().transform(games -> games.stream()
.map(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext)) .map(gameEntity -> GameMapper.toDto(gameEntity, dbManager.dbContext))

@ -7,24 +7,24 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.acme.api.dto.ParticipeDto; import org.acme.api.dto.ParticipeDTO;
import org.acme.api.dto.RoundDto; import org.acme.api.dto.RoundDTO;
import org.acme.hibernates.entities.ParticipeEntity; import org.acme.hibernates.entities.ParticipeEntity;
import org.acme.hibernates.entities.RoundEntity; import org.acme.hibernates.entities.RoundEntity;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
public class Extensions { public class Extensions {
public static Uni<List<RoundDto>> toRoundDtoList(Uni<List<RoundEntity>> uni) { public static Uni<List<RoundDTO>> toRoundDtoList(Uni<List<RoundEntity>> uni) {
return uni.map(roundEntities -> roundEntities.stream() return uni.map(roundEntities -> roundEntities.stream()
.map(RoundMapper::toDto) .map(RoundMapper::toDto)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
public static Uni<List<ParticipeDto>> toParticipeDtoList(Uni<List<ParticipeEntity>> uni) { public static Uni<List<ParticipeDTO>> toParticipeDtoList(Uni<List<ParticipeEntity>> uni) {
return uni return uni
.onItem().transformToUni(participes -> { .onItem().transformToUni(participes -> {
List<ParticipeDto> dtos = new ArrayList<>(); List<ParticipeDTO> dtos = new ArrayList<>();
for (ParticipeEntity entity : participes) { for (ParticipeEntity entity : participes) {
dtos.add(ParticipeMapper.toDto(entity)); dtos.add(ParticipeMapper.toDto(entity));
} }

@ -1,13 +1,13 @@
package org.acme.api.mapper; package org.acme.api.mapper;
import org.acme.api.BowlDbContext; import org.acme.api.BowlDbContext;
import org.acme.api.dto.GameDto; import org.acme.api.dto.GameDTO;
import org.acme.hibernates.entities.GameEntity; import org.acme.hibernates.entities.GameEntity;
public class GameMapper { public class GameMapper {
public static GameDto toDto(GameEntity entity, BowlDbContext dbContext) { public static GameDTO toDto(GameEntity entity, BowlDbContext dbContext) {
GameDto dto = new GameDto(); GameDTO dto = new GameDTO();
dto.id = entity.id; dto.id = entity.id;
dto.players = dbContext.participeRepository.findByGameIdQ(entity.id); dto.players = dbContext.participeRepository.findByGameIdQ(entity.id);
dto.date = Extensions.toLocalDate(entity.time); dto.date = Extensions.toLocalDate(entity.time);
@ -16,7 +16,7 @@ public class GameMapper {
return dto; return dto;
} }
public static GameEntity toEntity(GameDto entity, BowlDbContext dbContext) { public static GameEntity toEntity(GameDTO entity, BowlDbContext dbContext) {
GameEntity game = new GameEntity(); GameEntity game = new GameEntity();
return game; return game;

@ -1,11 +1,11 @@
package org.acme.api.mapper; package org.acme.api.mapper;
import org.acme.api.dto.ParticipeDto; import org.acme.api.dto.ParticipeDTO;
import org.acme.hibernates.entities.ParticipeEntity; import org.acme.hibernates.entities.ParticipeEntity;
public class ParticipeMapper { public class ParticipeMapper {
public static ParticipeDto toDto(ParticipeEntity entity) { public static ParticipeDTO toDto(ParticipeEntity entity) {
return new ParticipeDto(entity.user.id, UserMapper.toDto(entity.user), entity.totalPoints, return new ParticipeDTO(entity.user.id, UserMapper.toDto(entity.user), entity.totalPoints,
entity.guestName); entity.guestName);
} }

@ -2,13 +2,13 @@ package org.acme.api.mapper;
import java.util.List; import java.util.List;
import org.acme.api.dto.RoundDto; import org.acme.api.dto.RoundDTO;
import org.acme.hibernates.entities.RoundEntity; import org.acme.hibernates.entities.RoundEntity;
import org.acme.hibernates.entities.ThrowEntity; import org.acme.hibernates.entities.ThrowEntity;
public class RoundMapper { public class RoundMapper {
public static RoundDto toDto(RoundEntity entity) { public static RoundDTO toDto(RoundEntity entity) {
List<ThrowEntity> throwsGame = entity.throwsGame; List<ThrowEntity> throwsGame = entity.throwsGame;
int val1 = 0; int val1 = 0;
int val2 = 0; int val2 = 0;
@ -20,6 +20,6 @@ public class RoundMapper {
val1 = throwsGame.get(1).pins; val1 = throwsGame.get(1).pins;
} }
return new RoundDto(entity.participe.id.position, 0, val1, val2, entity.points); return new RoundDTO(entity.participe.id.position, 0, val1, val2, entity.points);
} }
} }

@ -1,7 +1,7 @@
package org.acme.api.mapper; package org.acme.api.mapper;
import org.acme.api.dto.UserDto; import org.acme.api.dto.UserDTO;
import org.acme.api.dto.UserTinyDto; import org.acme.api.dto.UserTinyDTO;
import org.acme.hibernates.entities.UserEntity; import org.acme.hibernates.entities.UserEntity;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -9,11 +9,11 @@ public class UserMapper {
private static final String HASH_ALGORITHM = "SHA-256"; private static final String HASH_ALGORITHM = "SHA-256";
public static UserDto toDto(UserEntity user) { public static UserDTO toDto(UserEntity user) {
return new UserDto(user.id, user.getName(), user.getImage(), user.getMail()); return new UserDTO(user.id, user.getName(), user.getImage(), user.getMail(), UserStatsMapper.toDto(user.stats));
} }
public static UserEntity toEntity(UserTinyDto user) { public static UserEntity toEntity(UserTinyDTO user) {
try { try {
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM); MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
byte[] hashedBytes = digest.digest(user.password.getBytes("UTF-8")); byte[] hashedBytes = digest.digest(user.password.getBytes("UTF-8"));

@ -0,0 +1,12 @@
package org.acme.api.mapper;
import org.acme.api.dto.UserStatsDTO;
import org.acme.hibernates.entities.UserStatsEntity;
public class UserStatsMapper {
public static UserStatsDTO toDto(UserStatsEntity stats) {
return new UserStatsDTO(stats.getNbVictories(),stats.getNbGames(), stats.getHighscore(), stats.getNbStrikes(), stats.getNbSpares(), stats.getAvgScore(), stats.getAvgPinsPerRound());
}
}

@ -1,10 +1,10 @@
package org.acme.api.service; package org.acme.api.repository;
import java.util.List; import java.util.List;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import org.acme.api.dto.GameDto; import org.acme.api.dto.GameDTO;
import org.acme.hibernates.entities.GameEntity; import org.acme.hibernates.entities.GameEntity;
import io.quarkus.hibernate.reactive.panache.PanacheRepository; import io.quarkus.hibernate.reactive.panache.PanacheRepository;
@ -16,8 +16,8 @@ public class GameRepository implements PanacheRepository<GameEntity> {
return list("id", id); return list("id", id);
} }
public Uni<List<GameDto>> findByIdGame(Long id) { public Uni<List<GameDTO>> findByIdGame(Long id) {
return find("id", id).project(GameDto.class).list(); return find("id", id).project(GameDTO.class).list();
} }
} }
// public Uni<GameDto> getDetailsGameById(Long gameId) { // public Uni<GameDto> getDetailsGameById(Long gameId) {

@ -1,10 +1,10 @@
package org.acme.api.service; package org.acme.api.repository;
import java.util.List; import java.util.List;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import org.acme.api.dto.ParticipeDto; import org.acme.api.dto.ParticipeDTO;
import org.acme.hibernates.entities.ParticipeEntity; import org.acme.hibernates.entities.ParticipeEntity;
import io.quarkus.hibernate.reactive.panache.PanacheQuery; import io.quarkus.hibernate.reactive.panache.PanacheQuery;
@ -17,8 +17,8 @@ public class ParticipeRepository implements PanacheRepository<ParticipeEntity> {
return list("game.id", gameId); return list("game.id", gameId);
} }
public Uni<List<ParticipeDto>> findByGameIdQ(Long gameId) { public Uni<List<ParticipeDTO>> findByGameIdQ(Long gameId) {
PanacheQuery<ParticipeDto> query = find("game.id", gameId).project(ParticipeDto.class); PanacheQuery<ParticipeDTO> query = find("game.id", gameId).project(ParticipeDTO.class);
return query.list(); return query.list();
} }
} }

@ -1,4 +1,4 @@
package org.acme.api.service; package org.acme.api.repository;
import java.util.List; import java.util.List;

@ -1,4 +1,4 @@
package org.acme.api.service; package org.acme.api.repository;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;

@ -1,9 +1,9 @@
package org.acme.api.service; package org.acme.api.repository;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import org.acme.api.dto.UserDto; import org.acme.api.dto.UserDTO;
import org.acme.api.dto.UserTinyDto; import org.acme.api.dto.UserTinyDTO;
import org.acme.api.mapper.UserMapper; import org.acme.api.mapper.UserMapper;
import org.acme.hibernates.entities.UserEntity; import org.acme.hibernates.entities.UserEntity;
@ -15,13 +15,13 @@ import java.util.Objects;
@ApplicationScoped @ApplicationScoped
public class UserRepository implements PanacheRepository<UserEntity> { public class UserRepository implements PanacheRepository<UserEntity> {
public Uni<UserDto> addUser(UserTinyDto user) { public Uni<UserDTO> addUser(UserTinyDTO user) {
UserEntity userEntity = UserMapper.toEntity(user); UserEntity userEntity = UserMapper.toEntity(user);
return persistAndFlush(userEntity) return persistAndFlush(userEntity)
.map(ignore -> UserMapper.toDto(userEntity)); .map(ignore -> UserMapper.toDto(userEntity));
} }
public Uni<UserDto> findWithName(String name) { public Uni<UserDTO> findWithName(String name) {
return find("name", name.toLowerCase()) return find("name", name.toLowerCase())
.firstResult() .firstResult()
.map(UserMapper::toDto); .map(UserMapper::toDto);
@ -39,13 +39,13 @@ public class UserRepository implements PanacheRepository<UserEntity> {
.map(Objects::nonNull); .map(Objects::nonNull);
} }
public Uni<UserDto> findWithMail(String mail) { public Uni<UserDTO> findWithMail(String mail) {
return find("mail", mail.toLowerCase()) return find("mail", mail.toLowerCase())
.firstResult() .firstResult()
.map(UserMapper::toDto); .map(UserMapper::toDto);
} }
public Uni<UserDto> getUserById(Long id) { public Uni<UserDTO> getUserById(Long id) {
return find("id", id) return find("id", id)
.firstResult() .firstResult()
.map(UserMapper::toDto); .map(UserMapper::toDto);

@ -9,27 +9,21 @@ import javax.persistence.*;
import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.ColumnDefault;
@Entity @Entity
@Table(name = "Game") @Table(name = "games")
public class GameEntity { public class GameEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id; public Long id;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false) @Column(nullable = false)
@ColumnDefault("CURRENT_TIMESTAMP") @ColumnDefault("CURRENT_TIMESTAMP")
public Date time = new Date(); public Date time = new Date();
@ColumnDefault("0") @ColumnDefault("0")
public Long winner = 0L; public Long winner = 0L;
@ColumnDefault("0") @ColumnDefault("0")
public int nbPoints = 0; public int nbPoints = 0;
@ColumnDefault("false") @ColumnDefault("false")
public Boolean isFinished = false; public Boolean isFinished = false;
@ManyToOne @ManyToOne
@JoinColumn(name = "host_id", referencedColumnName = "id") @JoinColumn(name = "host_id", referencedColumnName = "id")
public UserEntity ownerGame; public UserEntity ownerGame;

@ -8,23 +8,18 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.ColumnDefault;
@Entity @Entity
@Table(name = "participe") @Table(name = "participes")
public class ParticipeEntity { public class ParticipeEntity {
@EmbeddedId @EmbeddedId
public ParticipeId id; public ParticipeId id;
@ManyToOne @ManyToOne
@JoinColumn(name = "idUser", referencedColumnName = "id") @JoinColumn(name = "idUser", referencedColumnName = "id")
public UserEntity user; public UserEntity user;
@ManyToOne @ManyToOne
@JoinColumn(name = "idGame", referencedColumnName = "id", insertable = false, updatable = false) @JoinColumn(name = "idGame", referencedColumnName = "id", insertable = false, updatable = false)
public GameEntity game; public GameEntity game;
@Column(name = "guestName") @Column(name = "guestName")
public String guestName; public String guestName;
@ColumnDefault("0") @ColumnDefault("0")
@Column(name = "totalPoints") @Column(name = "totalPoints")
public Integer totalPoints = 0; public Integer totalPoints = 0;

@ -138,26 +138,21 @@ import javax.persistence.*;
// // constructors, getters and setters // // constructors, getters and setters
// } // }
@Entity @Entity
@Table(name = "Round") @Table(name = "rounds")
public class RoundEntity { public class RoundEntity {
@EmbeddedId @EmbeddedId
public RoundId id; public RoundId id;
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "game_id", referencedColumnName = "id", insertable = false, updatable = false) @JoinColumn(name = "game_id", referencedColumnName = "id", insertable = false, updatable = false)
public GameEntity game; public GameEntity game;
@ManyToOne @ManyToOne
@JoinColumns({ @JoinColumns({
@JoinColumn(name = "idGame", referencedColumnName = "idGame", insertable = false, updatable = false), @JoinColumn(name = "idGame", referencedColumnName = "idGame", insertable = false, updatable = false),
@JoinColumn(name = "position", referencedColumnName = "position", insertable = false, updatable = false) @JoinColumn(name = "position", referencedColumnName = "position", insertable = false, updatable = false)
}) })
public ParticipeEntity participe; public ParticipeEntity participe;
@Column(name = "points") @Column(name = "points")
public Integer points; public Integer points;
@OneToMany(mappedBy = "round", cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(mappedBy = "round", cascade = CascadeType.ALL, orphanRemoval = true)
public List<ThrowEntity> throwsGame = new ArrayList<>(); public List<ThrowEntity> throwsGame = new ArrayList<>();

@ -5,12 +5,10 @@ import javax.persistence.*;
import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.ColumnDefault;
@Entity @Entity
@Table(name = "Throw") @Table(name = "throws")
public class ThrowEntity { public class ThrowEntity {
@EmbeddedId @EmbeddedId
public ThrowId id; public ThrowId id;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumns({
@JoinColumn(name = "game_id", referencedColumnName = "game_id", insertable = false, updatable = false), @JoinColumn(name = "game_id", referencedColumnName = "game_id", insertable = false, updatable = false),
@ -19,7 +17,6 @@ public class ThrowEntity {
@JoinColumn(name = "turnNumber", referencedColumnName = "turnNumber", insertable = false, updatable = false) @JoinColumn(name = "turnNumber", referencedColumnName = "turnNumber", insertable = false, updatable = false)
}) })
public RoundEntity round; public RoundEntity round;
@ColumnDefault("0") @ColumnDefault("0")
@Column(name = "pins") @Column(name = "pins")
public int pins = 0; public int pins = 0;

@ -3,30 +3,25 @@ package org.acme.hibernates.entities;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(name = "Users", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"}), @UniqueConstraint(columnNames = {"mail"})}) @Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"}), @UniqueConstraint(columnNames = {"mail"})})
public class UserEntity { public class UserEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id; public Long id;
@Column(length = 64) @Column(length = 64)
private String name; private String name;
@Column(length = 255) @Column(length = 255)
private String image; private String image;
@Column(length = 255) @Column(length = 255)
private String mail; private String mail;
@Column(length = 144) @Column(length = 144)
private String password; private String password;
//@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true) @OneToOne(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
//public UserStatsEntity stats; public UserStatsEntity stats;
public UserEntity() { public UserEntity() {
//this.stats = new UserStatsEntity(this); this.stats = new UserStatsEntity(this);
} }
public UserEntity(String name, String image, String mail, String password) { public UserEntity(String name, String image, String mail, String password) {
@ -34,15 +29,13 @@ public class UserEntity {
this.image = image; this.image = image;
this.mail = mail; this.mail = mail;
this.password = password; this.password = password;
//this.stats = new UserStatsEntity(this); this.stats = new UserStatsEntity(this);
} }
// return name as uppercase in the model
public String getName() { public String getName() {
return name.substring(0, 1).toUpperCase() + name.substring(1); return name.substring(0, 1).toUpperCase() + name.substring(1);
} }
// store all names in lowercase in the DB
public void setName(String name) { public void setName(String name) {
this.name = name.toLowerCase(); this.name = name.toLowerCase();
} }

@ -7,15 +7,14 @@ import java.io.Serializable;
import java.util.UUID; import java.util.UUID;
@Entity @Entity
@Table(name = "UserStats") @Table(name = "userStats")
public class UserStatsEntity { public class UserStatsEntity {
@Id @Id
private Long id; private Long id;
@MapsId @MapsId
@OneToOne(fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", referencedColumnName = "id", insertable = false, updatable = false) @JoinColumn(name = "userId", referencedColumnName = "id")
private UserEntity user; private UserEntity user;
@ColumnDefault("0") @ColumnDefault("0")
private Long nbVictories = 0L; private Long nbVictories = 0L;
@ColumnDefault("0") @ColumnDefault("0")
@ -33,11 +32,9 @@ public class UserStatsEntity {
public UserStatsEntity() { public UserStatsEntity() {
} }
public UserStatsEntity(UserEntity user) { public UserStatsEntity(UserEntity user) {
this.user = user; this.user = user;
} }
public Long getId() { public Long getId() {
return id; return id;
} }

@ -5,6 +5,10 @@ VALUES
('emre', 'af7c70f8789d9e0fde5b5f18b61c5e5dc5d5c5f5e7e5c4f4be7b2cb5d57ef52c','','emre.kartal@etu.uca.fr'), ('emre', 'af7c70f8789d9e0fde5b5f18b61c5e5dc5d5c5f5e7e5c4f4be7b2cb5d57ef52c','','emre.kartal@etu.uca.fr'),
('arthur', '0cc175b9c0f1b6a831c399e269772661','','arthur.valin@etu.uca.fr'); ('arthur', '0cc175b9c0f1b6a831c399e269772661','','arthur.valin@etu.uca.fr');
INSERT INTO userStats (userId, avgPinsPerRound, avgScore, highscore, nbGames, nbSpares, nbStrikes, nbVictories) VALUES (1, -1, -1, 0, 0, 0, 0, 0);
INSERT INTO userStats (userId, avgPinsPerRound, avgScore, highscore, nbGames, nbSpares, nbStrikes, nbVictories) VALUES (2, -1, -1, 0, 0, 0, 0, 0);
INSERT INTO userStats (userId, avgPinsPerRound, avgScore, highscore, nbGames, nbSpares, nbStrikes, nbVictories) VALUES (3, -1, -1, 0, 0, 0, 0, 0);
/*INSERT INTO games (isFinished, nbPoints, time, winner, host_id) VALUES (false, 0, CURRENT_TIMESTAMP, 0, 1); /*INSERT INTO games (isFinished, nbPoints, time, winner, host_id) VALUES (false, 0, CURRENT_TIMESTAMP, 0, 1);
INSERT INTO participe (idGame, position, guestname, totalpoints, iduser) VALUES (1, 1, 'Alice', 0, 1); INSERT INTO participe (idGame, position, guestname, totalpoints, iduser) VALUES (1, 1, 'Alice', 0, 1);
INSERT INTO round (game_id, idGame, position, turnNumber, points) VALUES (1, 1, 1, 1, 0); INSERT INTO round (game_id, idGame, position, turnNumber, points) VALUES (1, 1, 1, 1, 0);

Loading…
Cancel
Save