Compare commits
10 Commits
master
...
Quarkus_Co
Author | SHA1 | Date |
---|---|---|
![]() |
91b2ad7e90 | 2 years ago |
|
968497e1b1 | 2 years ago |
|
46ae14a55e | 2 years ago |
|
468a877ece | 2 years ago |
|
4011ff18c6 | 2 years ago |
![]() |
2d23793eb0 | 2 years ago |
|
b61e33dde7 | 2 years ago |
|
c1b8eca526 | 2 years ago |
![]() |
a336610308 | 2 years ago |
|
a921345f49 | 2 years ago |
@ -1,13 +0,0 @@
|
||||
package org.acme.Api.Mappeur;
|
||||
|
||||
import org.acme.Api.DTO.ParticipeDto;
|
||||
import org.acme.Api.DTO.RoundDto;
|
||||
import org.acme.Hibernates.entities.ParticipeEntity;
|
||||
|
||||
public class ParticipeMappeur {
|
||||
public static ParticipeDto toDto(ParticipeEntity entity) {
|
||||
return new ParticipeDto(entity.user.id, UserMappeur.toUserDto(entity.user), entity.totalPoints,
|
||||
entity.guestName);
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package org.acme.Api.Mappeur;
|
||||
|
||||
public class TrhowMappeur {
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.acme.Api.Mappeur;
|
||||
|
||||
import org.acme.Api.DTO.UserDTO;
|
||||
import org.acme.Api.DTO.UserTinyDTO;
|
||||
import org.acme.Hibernates.entities.UserEntity;
|
||||
|
||||
public class UserMappeur {
|
||||
public static UserDTO toUserDto(UserEntity entity) {
|
||||
return new UserDTO(entity.id, entity.name, entity.stats.getNbVictories(), entity.stats.getNbGames(),
|
||||
entity.stats.getHighscore(), entity.stats.getNbStrikes(), entity.stats.getNbSpares(),
|
||||
entity.stats.getAvgScore(), entity.stats.getAvgPinsPerRound());
|
||||
}
|
||||
|
||||
public static UserTinyDTO toUserTinyDTO(UserEntity entity) {
|
||||
return new UserTinyDTO(entity.id, entity.name);
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.acme.Api.controllers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@ApplicationScoped
|
||||
@Path("/test")
|
||||
public class TestController {
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
@Path("/po")
|
||||
public String hello() {
|
||||
return "Hello from RESTEasy Reactive";
|
||||
}
|
||||
|
||||
}
|
@ -1,125 +0,0 @@
|
||||
package org.acme.Api.controllers;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.transaction.Transactional;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.NotFoundException;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.PanacheQuery;
|
||||
|
||||
import org.acme.Api.BowlDbContext;
|
||||
import org.acme.Api.DTO.UserDTO;
|
||||
import org.acme.Api.DTO.UserTinyDTO;
|
||||
import org.acme.Api.service.UserRepository;
|
||||
import org.acme.Hibernates.entities.UserEntity;
|
||||
import org.eclipse.microprofile.openapi.annotations.Operation;
|
||||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.common.runtime.ReactiveTransactional;
|
||||
import io.quarkus.panache.common.Sort;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@ApplicationScoped
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Path("/users")
|
||||
public class UserController {
|
||||
private static final Logger LOGGER = Logger.getLogger(UserController.class.getName());
|
||||
|
||||
@Inject
|
||||
BowlDbContext service;
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get all users")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Uni<List<UserDTO>> getUsers() {
|
||||
LOGGER.info("Get all users");
|
||||
return service.userRepository.findAll().project(UserDTO.class).list();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get a user by ID")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
public Uni<Response> getUserById(@PathParam("id") Long id) {
|
||||
LOGGER.info("Get user with id : " + id);
|
||||
return service.userRepository.findByBowlinId(id)
|
||||
.onItem()
|
||||
.transform(
|
||||
entity -> entity == null ? Response.status(Status.NOT_FOUND) : Response.ok(entity).status(200))
|
||||
.onItem().transform(Response.ResponseBuilder::build);
|
||||
|
||||
}
|
||||
|
||||
@POST
|
||||
@Operation(summary = "Create a new User")
|
||||
@APIResponse(responseCode = "201", description = "User successfully created")
|
||||
@APIResponse(responseCode = "422", description = "User invalidly set on request")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> createUser(UserEntity user) {
|
||||
if (user == null) {
|
||||
throw new WebApplicationException("user was invalidly set on request.", 422);
|
||||
}
|
||||
LOGGER.info("creating user: " + user.getName());
|
||||
return service.userRepository.persist(user)
|
||||
.map(persistedUser -> Response
|
||||
.created(URI.create("/users/" + user.id))
|
||||
.entity(persistedUser)
|
||||
.build())
|
||||
.onFailure().recoverWithItem(Response.status(Status.BAD_REQUEST).build());
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Operation(summary = "Update a User")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> updateUser(@PathParam("id") Long id, UserEntity newUser) {
|
||||
LOGGER.info("Update user with id : " + id);
|
||||
return service.userRepository.findById(id)
|
||||
.onItem().ifNull().failWith(() -> new WebApplicationException("User not found", Status.NOT_FOUND))
|
||||
.onItem().ifNotNull().invoke(oldUser -> {
|
||||
oldUser.setName(newUser.getName());
|
||||
})
|
||||
.onItem().ifNotNull().transform(entity -> Response.ok(entity).build());
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Operation(summary = "Delete a User")
|
||||
@APIResponse(responseCode = "200", description = "User successfully deleted")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> delete(@PathParam("id") Long id) {
|
||||
LOGGER.info("Delete user with id : " + id);
|
||||
return service.userRepository.deleteById(id)
|
||||
.onItem().transform(entity -> !entity ? Response.status(Status.NOT_FOUND).build()
|
||||
: Response.ok().status(200).build());
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get the number of users")
|
||||
@Path("/count")
|
||||
public Uni<Long> count() {
|
||||
LOGGER.info("Get user count");
|
||||
return service.userRepository.count();
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package org.acme.Api.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
import org.acme.Api.DTO.UserDTO;
|
||||
import org.acme.Api.DTO.UserTinyDTO;
|
||||
import org.acme.Hibernates.entities.UserEntity;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@ApplicationScoped
|
||||
public class UserRepository implements PanacheRepository<UserEntity> {
|
||||
// public Uni<UserEntity> getUserByName(String name) {
|
||||
// return UserEntity.find("name", name).firstResult();
|
||||
// }
|
||||
|
||||
public Uni<List<UserEntity>> findwithName(String name) {
|
||||
return list("name", name);
|
||||
}
|
||||
|
||||
public Uni<List<UserDTO>> findByBowlinId(Long id) {
|
||||
return find("id", id).project(UserDTO.class).list();
|
||||
}
|
||||
|
||||
// public Uni<Long> deleteUser() {
|
||||
// return delete("name", "Stef");
|
||||
// }
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package org.acme.Api.service;
|
||||
|
||||
public class UserService {
|
||||
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package org.acme.Hibernates.entities;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import org.eclipse.microprofile.openapi.annotations.media.Schema;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
// @Entity
|
||||
// @Table(name = "participe")
|
||||
// public class ParticipeEntity {
|
||||
// // @EmbeddedId
|
||||
// // @Schema(description = "Composite primary key of the entity")
|
||||
// // private ParticipeId id;
|
||||
|
||||
// // @Id
|
||||
// // @ManyToOne
|
||||
// // @JoinColumn(name = "game_id", referencedColumnName = "id")
|
||||
// // @Schema(description = "The Game entity that this Participe belongs to")
|
||||
// // private GameEntity game;
|
||||
// @Id
|
||||
// @Schema(description = "The position of the player in the game")
|
||||
// private int position2;
|
||||
// @Id
|
||||
// @Schema(description = "The position of the player in the game")
|
||||
// private int position;
|
||||
|
||||
// @ManyToOne
|
||||
// @JoinColumn(name = "id_user", referencedColumnName = "id")
|
||||
// @Schema(description = "The User entity that this Participe belongs to")
|
||||
// private UserEntity user;
|
||||
|
||||
// @Column(name = "guest_name")
|
||||
// @Schema(description = "Name of the guest")
|
||||
// private String guestName;
|
||||
|
||||
// @Column(name = "total_points")
|
||||
// @Schema(description = "Total points of the player in the game")
|
||||
// private int totalPoints;
|
||||
|
||||
// // @MapsId
|
||||
// // @OneToOne
|
||||
// // @JoinColumn(name = "user_id", referencedColumnName = "id", insertable =
|
||||
// // false, updatable = false)
|
||||
// // private UserEntity user;
|
||||
|
||||
// // public ParticipeId getId() {
|
||||
// // return id;
|
||||
// // }
|
||||
|
||||
// // public void setId(ParticipeId id) {
|
||||
// // this.id = id;
|
||||
// // }
|
||||
|
||||
// public String getGuestName() {
|
||||
// return guestName;
|
||||
// }
|
||||
|
||||
// public void setGuestName(String guestName) {
|
||||
// this.guestName = guestName;
|
||||
// }
|
||||
|
||||
// public int getTotalPoints() {
|
||||
// return totalPoints;
|
||||
// }
|
||||
|
||||
// public void setTotalPoints(int totalPoints) {
|
||||
// this.totalPoints = totalPoints;
|
||||
// }
|
||||
|
||||
// public UserEntity getUser() {
|
||||
// return user;
|
||||
// }
|
||||
|
||||
// public void setUser(UserEntity user) {
|
||||
// this.user = user;
|
||||
// }
|
||||
|
||||
// }
|
||||
@Entity
|
||||
@Table(name = "participe")
|
||||
public class ParticipeEntity {
|
||||
|
||||
@EmbeddedId
|
||||
public ParticipeId id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "iduser", referencedColumnName = "id")
|
||||
public UserEntity user;
|
||||
/// luii
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "idGame", referencedColumnName = "id", insertable = false, updatable = false)
|
||||
public GameEntity game;
|
||||
|
||||
@Column(name = "guestname")
|
||||
public String guestName;
|
||||
|
||||
@ColumnDefault("0")
|
||||
@Column(name = "totalpoints")
|
||||
public Integer totalPoints = 0;
|
||||
|
||||
public ParticipeEntity() {
|
||||
}
|
||||
|
||||
public ParticipeEntity(ParticipeId id, UserEntity user, GameEntity game, String guestName, Integer totalPoints) {
|
||||
this.id = id;
|
||||
this.user = user;
|
||||
this.game = game;
|
||||
this.guestName = guestName;
|
||||
this.totalPoints = totalPoints;
|
||||
}
|
||||
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package org.acme.Hibernates.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@Column(length = 100)
|
||||
public String name;
|
||||
|
||||
@Column(length = 100)
|
||||
private String password;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
|
||||
public UserStatsEntity stats;
|
||||
|
||||
public UserEntity() {
|
||||
this.stats = new UserStatsEntity(this);
|
||||
}
|
||||
|
||||
// return name as uppercase in the model
|
||||
public String getName() {
|
||||
return name.toUpperCase();
|
||||
}
|
||||
|
||||
// store all names in lowercase in the DB
|
||||
public void setName(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,181 @@
|
||||
package org.acme.api.controllers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.acme.api.BowlDbContext;
|
||||
import org.acme.api.dto.UserDTO;
|
||||
import org.acme.api.dto.UserTinyDTO;
|
||||
import org.acme.api.mapper.UserMapper;
|
||||
import org.eclipse.microprofile.openapi.annotations.Operation;
|
||||
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.common.runtime.ReactiveTransactional;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@ApplicationScoped
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Path("/users")
|
||||
public class UserController {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(UserController.class.getName());
|
||||
|
||||
@Inject
|
||||
BowlDbContext service;
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get all users")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Uni<List<UserDTO>> getUsers() {
|
||||
LOGGER.info("Get all users");
|
||||
return service.userRepository.findAll().list()
|
||||
.map(entities -> entities.stream().map(UserMapper::toDto).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get a user by ID")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
public Uni<Response> getUserById(@PathParam("id") Long id) {
|
||||
LOGGER.info("Get user with id : " + id);
|
||||
return service.userRepository.getUserById(id)
|
||||
.map(user -> user == null ? Response.status(Status.NOT_FOUND) : Response.ok(user).status(200))
|
||||
.map(Response.ResponseBuilder::build);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get a user by name")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/name/{name}")
|
||||
public Uni<Response> getUserByName(@PathParam("name") String name) {
|
||||
LOGGER.info("Get user with name : " + name);
|
||||
|
||||
return service.userRepository.verifiedName(name)
|
||||
.flatMap(exist -> {
|
||||
if (exist) {
|
||||
return service.userRepository.findWithName(name)
|
||||
.map(user -> Response.ok(user).build());
|
||||
} else {
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get a user by mail")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/mail/{mail}")
|
||||
public Uni<Response> getUserByMail(@PathParam("mail") String mail) {
|
||||
LOGGER.info("Get user with mail : " + mail);
|
||||
|
||||
return service.userRepository.verifiedMail(mail)
|
||||
.flatMap(exist -> {
|
||||
if (exist) {
|
||||
return service.userRepository.findWithMail(mail)
|
||||
.map(user -> Response.ok(user).build());
|
||||
} else {
|
||||
throw new NotFoundException("User not found");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@POST
|
||||
@Operation(summary = "Create a new user")
|
||||
@APIResponse(responseCode = "201", description = "User successfully created")
|
||||
@APIResponse(responseCode = "422", description = "User invalidly set on request")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> createUser(UserTinyDTO user) {
|
||||
if (user == null) {
|
||||
throw new WebApplicationException("user was invalidly set on request.", 422);
|
||||
}
|
||||
LOGGER.info("creating user: " + user);
|
||||
return Uni.combine().all().unis(
|
||||
service.userRepository.verifiedName(user.name),
|
||||
service.userRepository.verifiedMail(user.mail))
|
||||
.asTuple()
|
||||
.flatMap(tuple -> {
|
||||
boolean nameExists = tuple.getItem1();
|
||||
boolean mailExists = tuple.getItem2();
|
||||
if (nameExists) {
|
||||
throw new WebApplicationException("Name already exists", Status.BAD_REQUEST);
|
||||
}
|
||||
if (mailExists) {
|
||||
throw new WebApplicationException("Mail already exists", Status.BAD_REQUEST);
|
||||
}
|
||||
return service.userRepository.addUser(user)
|
||||
.map(createdUser -> Response.status(Response.Status.CREATED).entity(createdUser).build());
|
||||
})
|
||||
.onFailure().recoverWithItem(Response.status(Status.BAD_REQUEST).build());
|
||||
}
|
||||
|
||||
|
||||
@PUT
|
||||
@Operation(summary = "Update a user")
|
||||
@APIResponse(responseCode = "200", description = "OK")
|
||||
@APIResponse(responseCode = "400", description = "Bad Request")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> updateUser(@PathParam("id") Long id, UserTinyDTO newUser) {
|
||||
LOGGER.info("Update user with id : " + id);
|
||||
return service.userRepository.findById(id)
|
||||
.onItem().ifNull().failWith(() -> new WebApplicationException("User not found", Status.NOT_FOUND))
|
||||
.onItem().ifNotNull().transformToUni(oldUser ->
|
||||
Uni.combine().all().unis(
|
||||
service.userRepository.verifiedName(newUser.name),
|
||||
service.userRepository.verifiedMail(newUser.mail))
|
||||
.asTuple()
|
||||
.flatMap(tuple -> {
|
||||
boolean nameExists = tuple.getItem1();
|
||||
boolean mailExists = tuple.getItem2();
|
||||
if (nameExists && !oldUser.getName().equalsIgnoreCase(newUser.name)) {
|
||||
throw new WebApplicationException("Name already exists", Status.BAD_REQUEST);
|
||||
}
|
||||
if (mailExists && !oldUser.getMail().equalsIgnoreCase(newUser.mail)) {
|
||||
throw new WebApplicationException("Mail already exists", Status.BAD_REQUEST);
|
||||
}
|
||||
oldUser.setName(newUser.name);
|
||||
oldUser.setMail(newUser.mail);
|
||||
oldUser.setImage(newUser.image);
|
||||
return service.userRepository.persistAndFlush(oldUser)
|
||||
.map(entity -> Response.ok(UserMapper.toDto(entity)).build());
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@DELETE
|
||||
@Operation(summary = "Delete a user")
|
||||
@APIResponse(responseCode = "204")
|
||||
@APIResponse(responseCode = "404", description = "User not found")
|
||||
@Path("/{id}")
|
||||
@ReactiveTransactional
|
||||
public Uni<Response> delete(@PathParam("id") Long id) {
|
||||
LOGGER.info("Delete user with id : " + id);
|
||||
return service.userRepository.deleteById(id)
|
||||
.onItem().transform(entity -> !entity ? Response.status(Status.NOT_FOUND).build()
|
||||
: Response.noContent().build());
|
||||
}
|
||||
|
||||
@GET
|
||||
@Operation(summary = "Get the number of users")
|
||||
@Path("/count")
|
||||
public Uni<Long> count() {
|
||||
LOGGER.info("Get user count");
|
||||
return service.userRepository.count();
|
||||
}
|
||||
}
|
@ -1,19 +1,19 @@
|
||||
package org.acme.Api.DTO;
|
||||
package org.acme.api.dto;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class RoundDto {
|
||||
public class RoundDTO {
|
||||
private int playerPosition;
|
||||
private int turn;
|
||||
private int pinsFirstThrow;
|
||||
private int pinsSecondThrow;
|
||||
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.turn = turn;
|
||||
this.pinsFirstThrow = pinsFirstThrow;
|
@ -1,16 +1,20 @@
|
||||
package org.acme.Api.DTO;
|
||||
package org.acme.api.dto;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.common.ProjectedFieldName;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import org.eclipse.microprofile.openapi.annotations.media.Schema;
|
||||
|
||||
@RegisterForReflection
|
||||
@Schema(description = "A minimal DTO for transferring user information")
|
||||
public class UserTinyDTO {
|
||||
public Long id;
|
||||
|
||||
public String name;
|
||||
public UserTinyDTO(Long id, String name) {
|
||||
this.id = id;
|
||||
public String image;
|
||||
public String mail;
|
||||
public String password;
|
||||
public UserTinyDTO(String name, String image, String mail, String password) {
|
||||
this.name = name;
|
||||
this.image = image;
|
||||
this.mail = mail;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
package org.acme.Api.Manager;
|
||||
package org.acme.api.manager;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.acme.Api.BowlDbContext;
|
||||
import org.acme.api.BowlDbContext;
|
||||
|
||||
@Singleton
|
||||
public class DbManager {
|
@ -0,0 +1,12 @@
|
||||
package org.acme.api.mapper;
|
||||
|
||||
import org.acme.api.dto.ParticipeDTO;
|
||||
import org.acme.hibernates.entities.ParticipeEntity;
|
||||
|
||||
public class ParticipeMapper {
|
||||
public static ParticipeDTO toDto(ParticipeEntity entity) {
|
||||
return new ParticipeDTO(entity.user.id, UserMapper.toDto(entity.user), entity.totalPoints,
|
||||
entity.guestName);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package org.acme.api.mapper;
|
||||
|
||||
public class TrhowMapper {
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package org.acme.api.mapper;
|
||||
|
||||
import org.acme.api.dto.UserDTO;
|
||||
import org.acme.api.dto.UserTinyDTO;
|
||||
import org.acme.hibernates.entities.UserEntity;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class UserMapper {
|
||||
|
||||
private static final String HASH_ALGORITHM = "SHA-256";
|
||||
|
||||
public static UserDTO toDto(UserEntity user) {
|
||||
return new UserDTO(user.id, user.getName(), user.getImage(), user.getMail(), UserStatsMapper.toDto(user.stats));
|
||||
}
|
||||
|
||||
public static UserEntity toEntity(UserTinyDTO user) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
|
||||
byte[] hashedBytes = digest.digest(user.password.getBytes("UTF-8"));
|
||||
String hashedPassword = bytesToHex(hashedBytes);
|
||||
return new UserEntity(user.name.toLowerCase(), user.image, user.mail.toLowerCase(), hashedPassword);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException("Error hashing password", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static String bytesToHex(byte[] bytes) {
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
String hex = Integer.toHexString(0xFF & b);
|
||||
if (hex.length() == 1) {
|
||||
hexString.append('0');
|
||||
}
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
}
|
||||
}
|
@ -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,11 +1,10 @@
|
||||
package org.acme.Api.service;
|
||||
package org.acme.api.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
import org.acme.Hibernates.entities.GameEntity;
|
||||
import org.acme.Hibernates.entities.RoundEntity;
|
||||
import org.acme.hibernates.entities.RoundEntity;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
|
||||
import io.smallrye.mutiny.Uni;
|
@ -1,8 +1,8 @@
|
||||
package org.acme.Api.service;
|
||||
package org.acme.api.repository;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
import org.acme.Hibernates.entities.ThrowEntity;
|
||||
import org.acme.hibernates.entities.ThrowEntity;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
|
||||
|
@ -0,0 +1,54 @@
|
||||
package org.acme.api.repository;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
import org.acme.api.dto.UserDTO;
|
||||
import org.acme.api.dto.UserTinyDTO;
|
||||
import org.acme.api.mapper.UserMapper;
|
||||
import org.acme.hibernates.entities.UserEntity;
|
||||
|
||||
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@ApplicationScoped
|
||||
public class UserRepository implements PanacheRepository<UserEntity> {
|
||||
|
||||
public Uni<UserDTO> addUser(UserTinyDTO user) {
|
||||
UserEntity userEntity = UserMapper.toEntity(user);
|
||||
return persistAndFlush(userEntity)
|
||||
.map(ignore -> UserMapper.toDto(userEntity));
|
||||
}
|
||||
|
||||
public Uni<UserDTO> findWithName(String name) {
|
||||
return find("name", name.toLowerCase())
|
||||
.firstResult()
|
||||
.map(UserMapper::toDto);
|
||||
}
|
||||
|
||||
public Uni<Boolean> verifiedName(String name) {
|
||||
return find("name", name.toLowerCase())
|
||||
.firstResult()
|
||||
.map(Objects::nonNull);
|
||||
}
|
||||
|
||||
public Uni<Boolean> verifiedMail(String mail) {
|
||||
return find("mail", mail.toLowerCase())
|
||||
.firstResult()
|
||||
.map(Objects::nonNull);
|
||||
}
|
||||
|
||||
public Uni<UserDTO> findWithMail(String mail) {
|
||||
return find("mail", mail.toLowerCase())
|
||||
.firstResult()
|
||||
.map(UserMapper::toDto);
|
||||
}
|
||||
|
||||
public Uni<UserDTO> getUserById(Long id) {
|
||||
return find("id", id)
|
||||
.firstResult()
|
||||
.map(UserMapper::toDto);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package org.acme.hibernates.entities;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
import org.eclipse.microprofile.openapi.annotations.media.Schema;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
|
||||
@Entity
|
||||
@Table(name = "participes")
|
||||
public class ParticipeEntity {
|
||||
@EmbeddedId
|
||||
public ParticipeId id;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "idUser", referencedColumnName = "id")
|
||||
public UserEntity user;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "idGame", referencedColumnName = "id", insertable = false, updatable = false)
|
||||
public GameEntity game;
|
||||
@Column(name = "guestName")
|
||||
public String guestName;
|
||||
@ColumnDefault("0")
|
||||
@Column(name = "totalPoints")
|
||||
public Integer totalPoints = 0;
|
||||
|
||||
public ParticipeEntity() {
|
||||
}
|
||||
|
||||
public ParticipeEntity(ParticipeId id, UserEntity user, GameEntity game, String guestName, Integer totalPoints) {
|
||||
this.id = id;
|
||||
this.user = user;
|
||||
this.game = game;
|
||||
this.guestName = guestName;
|
||||
this.totalPoints = totalPoints;
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.acme.Hibernates.entities;
|
||||
package org.acme.hibernates.entities;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.acme.Hibernates.entities;
|
||||
package org.acme.hibernates.entities;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
@ -1,4 +1,4 @@
|
||||
package org.acme.Hibernates.entities;
|
||||
package org.acme.hibernates.entities;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
@ -0,0 +1,67 @@
|
||||
package org.acme.hibernates.entities;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"}), @UniqueConstraint(columnNames = {"mail"})})
|
||||
public class UserEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
@Column(length = 64)
|
||||
private String name;
|
||||
@Column(length = 255)
|
||||
private String image;
|
||||
@Column(length = 255)
|
||||
private String mail;
|
||||
@Column(length = 144)
|
||||
private String password;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
|
||||
public UserStatsEntity stats;
|
||||
|
||||
public UserEntity() {
|
||||
this.stats = new UserStatsEntity(this);
|
||||
}
|
||||
|
||||
public UserEntity(String name, String image, String mail, String password) {
|
||||
this.name = name;
|
||||
this.image = image;
|
||||
this.mail = mail;
|
||||
this.password = password;
|
||||
this.stats = new UserStatsEntity(this);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name.substring(0, 1).toUpperCase() + name.substring(1);
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name.toLowerCase();
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getMail() {
|
||||
return mail;
|
||||
}
|
||||
|
||||
public void setMail(String mail) {
|
||||
this.mail = mail.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
@ -1,61 +1,16 @@
|
||||
-- INSERT INTO users(id, name,password) VALUES (nextval('hibernate_sequence'), 'Emre','Emre');
|
||||
-- INSERT INTO users(id, name,password) VALUES (nextval('hibernate_sequence'), 'Arthur','Arthur');
|
||||
-- INSERT INTO users(id, name,password) VALUES (nextval('hibernate_sequence'), 'Lucas','Lucas');
|
||||
-- INSERT INTO users(id, name,password) VALUES (nextval('hibernate_sequence'), 'Louison','Louison');
|
||||
-- INSERT INTO users (Id, Name, Password) VALUES (1, 'Alice', 'password1'),
|
||||
-- (2, 'Bob', 'password2'),
|
||||
-- (3, 'Charlie', 'password3');
|
||||
-- INSERT INTO user_stats (user_id, nbVictories, nbGames) VALUES
|
||||
-- (1, 2, 5),
|
||||
-- (2, 3, 7),
|
||||
-- (3, 1, 3);
|
||||
-- INSERT INTO games (Id, host_id) VALUES
|
||||
-- (1, 1),
|
||||
-- (2, 2);
|
||||
-- INSERT INTO participe (idGame, position, iduser, guestname, totalPoints) VALUES
|
||||
-- (1, 1, 1, NULL, 0),
|
||||
-- (1, 2, 2, NULL, 0),
|
||||
-- (2, 1, 2, NULL, 0),
|
||||
-- (2, 2, NULL, 'guest1', 0),
|
||||
-- (2, 3, NULL, 'guest2', 0);
|
||||
-- INSERT INTO round (game_id, PlayerPosition, TurnNumber, points) VALUES
|
||||
-- (1, 1, 1, 10),
|
||||
-- (1, 2, 1, 7),
|
||||
-- (1, 1, 2, 9),
|
||||
-- (1, 2, 2, 1),
|
||||
-- (1, 1, 3, 3),
|
||||
-- (1, 2, 3, 10),
|
||||
-- (2, 1, 1, 9),
|
||||
-- (2, 2, 1, 1),
|
||||
-- (2, 1, 2, 8),
|
||||
-- (2, 2, 2, 2),
|
||||
-- (2, 3, 2, 0),
|
||||
-- (2, 1, 3, 10),
|
||||
-- (2, 2, 3, 10),
|
||||
-- (2, 3, 3, 7);
|
||||
INSERT INTO
|
||||
Users (name, password, image, mail)
|
||||
VALUES
|
||||
('david', '38762cf7f55934b34d179ae6a4c80cadccbb7f0a2c2a5b4c4a1cb595d8dadb31','','david.d_almeida@etu.uca.fr'),
|
||||
('emre', 'af7c70f8789d9e0fde5b5f18b61c5e5dc5d5c5f5e7e5c4f4be7b2cb5d57ef52c','','emre.kartal@etu.uca.fr'),
|
||||
('arthur', '0cc175b9c0f1b6a831c399e269772661','','arthur.valin@etu.uca.fr');
|
||||
|
||||
-- INSERT INTO ThrowTable (Order, idGame, position, , pins) VALUES
|
||||
-- (1, 1, 1, 10),
|
||||
-- (1, 1, 2, 7),
|
||||
-- (2, 1, 1, 9),
|
||||
-- (2, 1, 2, 1),
|
||||
-- (3, 1, 1, 3),
|
||||
-- (3, 1, 2, 10),
|
||||
-- (1, 2, 1, 9),
|
||||
-- (1, 2, 2, 1),
|
||||
-- (2, 2, 1, 8),
|
||||
-- (2, 2, 2, 2),
|
||||
-- (3, 2, 3, 0),
|
||||
-- (3, 2, 1, 10),
|
||||
-- (4, 2, 2, 10),
|
||||
-- (4, 2, 3, 7);
|
||||
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 users (name, password) VALUES ('Alice', 'password123');
|
||||
INSERT INTO users (name, Password) VALUES
|
||||
('Bob', 'password2'),
|
||||
('Charlie', 'password3');
|
||||
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 round (game_id, idGame, position, turnNumber, points) VALUES (1, 1, 1, 1, 0);
|
||||
INSERT INTO throwtable (OrderTrhow, game_id, idGame, position, turnNumber, pins) VALUES (1, 1, 1, 1, 1, 0);
|
||||
INSERT INTO user_stats (user_id, avgPinsPerRound, avgScore, highscore, nbGames, nbSpares, nbStrikes, nbVictories) VALUES (1, 0, 0, 0, 0, 0, 0, 0);
|
||||
INSERT INTO user_stats (user_id, avgPinsPerRound, avgScore, highscore, nbGames, nbSpares, nbStrikes, nbVictories) VALUES (1, 0, 0, 0, 0, 0, 0, 0);*/
|
||||
|
@ -1,56 +0,0 @@
|
||||
// package org.acme;
|
||||
|
||||
// import java.util.List;
|
||||
|
||||
// import org.acme.Api.controllers.UserController;
|
||||
// import org.acme.Api.service.UserService;
|
||||
// import org.acme.Hibernates.entities.UserEntity;
|
||||
|
||||
// @ExtendWith(MockitoExtension.class)
|
||||
// public class UserCtrlTest {
|
||||
// @Mock
|
||||
// private UserService userService;
|
||||
|
||||
// @InjectMocks
|
||||
// private UserController userController;
|
||||
|
||||
// @TestFactory
|
||||
// Stream<DynamicTest> shouldReturnUserById() {
|
||||
// List<UserEntity> users = Arrays.asList(
|
||||
// new UserEntity(1L, "John", "password"),
|
||||
// new UserEntity(2L, "Jane", "password"));
|
||||
|
||||
// return DynamicTest.stream(
|
||||
// users.iterator(),
|
||||
// user -> "should return user with id " + user.getId(),
|
||||
// user -> {
|
||||
// // Given
|
||||
// Mockito.when(userService.findByBowlinId(user.getId()))
|
||||
// .thenReturn(CompletableFuture.completedFuture(user));
|
||||
|
||||
// // When
|
||||
// Uni<Response> response = userController.getUserById(user.getId());
|
||||
|
||||
// // Then
|
||||
// response.subscribe().with(result -> {
|
||||
// assertEquals(Response.Status.OK.getStatusCode(), result.getStatus());
|
||||
// assertEquals(user, result.getEntity());
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// @Test
|
||||
// void shouldReturn404IfUserNotFound() {
|
||||
// // Given
|
||||
// Long id = 1L;
|
||||
// Mockito.when(userService.findByBowlinId(id)).thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
// // When
|
||||
// Uni<Response> response = userController.getUserById(id);
|
||||
|
||||
// // Then
|
||||
// response.subscribe().with(result -> {
|
||||
// assertEquals(Response.Status.NOT_FOUND.getStatusCode(), result.getStatus());
|
||||
// });
|
||||
// }
|
||||
// }
|
Loading…
Reference in new issue