push init
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
public abstract class AbstractJpaDao<K, T extends Serializable> implements IGenericDao<K, T> {
|
||||
protected EntityManager em;
|
||||
private Class<T> entityClass;
|
||||
|
||||
public AbstractJpaDao() {
|
||||
this.em = EntityManagerHelper.getEntityManager();
|
||||
}
|
||||
|
||||
//Pour connaitre qu'on travaille avec quelle classe
|
||||
public void setClass(Class<T> class2){
|
||||
this.entityClass= class2;
|
||||
}
|
||||
|
||||
public T findOne(K id) {
|
||||
return em.find(entityClass, id);
|
||||
}
|
||||
|
||||
public List<T> findAll() {
|
||||
return em.createQuery("select e from " + entityClass.getName() + " as e", entityClass).getResultList();
|
||||
}
|
||||
|
||||
//CRUD
|
||||
public void create(T entity){
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
em.persist(entity);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
public T findById(K id) {
|
||||
return em.find(entityClass, id);
|
||||
}
|
||||
|
||||
public T update(T entity){
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
T T_sync = em.merge(entity);
|
||||
t.commit();
|
||||
return T_sync;
|
||||
}
|
||||
|
||||
public void delete(T entity){
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
em.remove(entity);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
public void deleteById(K id) {
|
||||
T T_to_be_deleted = em.find(entityClass, id);
|
||||
em.remove(T_to_be_deleted);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class EntityManagerHelper {
|
||||
|
||||
private static final EntityManagerFactory emf;
|
||||
private static final ThreadLocal<EntityManager> threadLocal;
|
||||
|
||||
static {
|
||||
emf = Persistence.createEntityManagerFactory("dev");
|
||||
threadLocal = new ThreadLocal<EntityManager>();
|
||||
}
|
||||
|
||||
public static EntityManager getEntityManager() {
|
||||
EntityManager em = threadLocal.get();
|
||||
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
threadLocal.set(em);
|
||||
}
|
||||
return em;
|
||||
}
|
||||
|
||||
public static void closeEntityManager() {
|
||||
EntityManager em = threadLocal.get();
|
||||
if (em != null) {
|
||||
em.close();
|
||||
threadLocal.set(null);
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeEntityManagerFactory() {
|
||||
emf.close();
|
||||
}
|
||||
|
||||
public static void beginTransaction() {
|
||||
getEntityManager().getTransaction().begin();
|
||||
}
|
||||
|
||||
public static void rollback() {
|
||||
getEntityManager().getTransaction().rollback();
|
||||
}
|
||||
|
||||
public static void commit() {
|
||||
getEntityManager().getTransaction().commit();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
public interface IGenericDao<K, T extends Serializable> {
|
||||
void create(final T entity);
|
||||
T findById(final K id);
|
||||
T update(final T entity);
|
||||
void delete(final T entity);
|
||||
void deleteById(final K id);
|
||||
List<T> findAll();
|
||||
T findOne(final K id);
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.istic.taa.jaxrs.metier.Choix;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Reponse;
|
||||
import fr.istic.taa.jaxrs.metier.ReponseCourte;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
|
||||
public class QuestionDAO extends AbstractJpaDao<Integer, Question> {
|
||||
public QuestionDAO(){
|
||||
super();
|
||||
this.setClass(Question.class);
|
||||
}
|
||||
|
||||
|
||||
public void addReponse(String reponse, int questionId){
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Question q = em.find(Question.class,questionId);
|
||||
|
||||
q.getReponse().getReponses().add(reponse);
|
||||
em.merge(q);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
|
||||
public void deleteReponse( int questionId) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Question q = em.find(Question.class,questionId);
|
||||
|
||||
q.getReponse().setReponses(new ArrayList<>());
|
||||
em.merge(q);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
|
||||
public void setReponse(Reponse rep, Integer id) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Question q = em.find(Question.class,id);
|
||||
q.setReponse(rep);
|
||||
em.merge(q);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
|
||||
public void setChoix(Integer id) {
|
||||
setReponse(new Choix(),id);
|
||||
}
|
||||
|
||||
public void setReponseCourte(Integer id) {
|
||||
setReponse(new ReponseCourte(),id);
|
||||
}
|
||||
|
||||
|
||||
public void addChoix(Integer id, String choix) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Question q = em.find(Question.class,id);
|
||||
|
||||
((Choix)q.getReponse()).getChoix().add(choix);
|
||||
em.merge(q);
|
||||
t.commit();
|
||||
}
|
||||
|
||||
|
||||
public void changeQuestion(String newQuestion, Integer id) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Question q = em.find(Question.class,id);
|
||||
q.setQuestion(newQuestion);
|
||||
em.merge(q);
|
||||
t.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import fr.istic.taa.jaxrs.metier.Quizz;
|
||||
|
||||
public class QuizzDAO extends AbstractJpaDao<Integer, Quizz> {
|
||||
public QuizzDAO(){
|
||||
super();
|
||||
this.setClass(Quizz.class);
|
||||
}
|
||||
|
||||
public void deleteAllQustion(int quizz_id){
|
||||
EntityTransaction et= em.getTransaction();
|
||||
et.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
||||
quizz.getQuestions().clear();
|
||||
em.merge(quizz);
|
||||
et.commit();
|
||||
}
|
||||
|
||||
public void addQuestion(int quizz_id, int question_id){
|
||||
EntityTransaction et= em.getTransaction();
|
||||
et.begin();
|
||||
Quizz quizz = em.find(Quizz.class, quizz_id);
|
||||
Question question =em.find(Question.class, question_id);
|
||||
|
||||
quizz.getQuestions().add(question);
|
||||
em.merge(quizz);
|
||||
et.commit();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import fr.istic.taa.jaxrs.metier.Reponse;
|
||||
|
||||
public class ReponseDAO extends AbstractJpaDao<Integer, Reponse> {
|
||||
public ReponseDAO(){
|
||||
super();
|
||||
this.setClass(Reponse.class);
|
||||
}
|
||||
|
||||
/*public List<String> getGoodResponses(){
|
||||
EntityTransaction t=em.getTransaction();
|
||||
t.begin();
|
||||
Query query=em.createQuery("select r from Reponse r where r.reponses");
|
||||
List<String> lString=query.getResultList();
|
||||
t.commit();
|
||||
return lString;
|
||||
}*/
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import jakarta.persistence.Query;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SessionDAO extends AbstractJpaDao<Integer, Session> {
|
||||
public SessionDAO() {
|
||||
super();
|
||||
this.setClass(Session.class);
|
||||
}
|
||||
|
||||
public List<Session> findByTheme(String theme){
|
||||
EntityTransaction t=em.getTransaction();
|
||||
Query query=em.createQuery("select s from Session s where s.theme=:theme");
|
||||
query.setParameter("theme",theme);
|
||||
List<Session> sessions=query.getResultList();
|
||||
if(sessions.size()>0){
|
||||
return sessions;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DAO;
|
||||
|
||||
import jakarta.persistence.EntityTransaction;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UtilisateurDAO extends AbstractJpaDao<Integer, Utilisateur> {
|
||||
public UtilisateurDAO() {
|
||||
super();
|
||||
this.setClass(Utilisateur.class);
|
||||
}
|
||||
|
||||
public Utilisateur findByEmail(String email) {
|
||||
List<Utilisateur> results =
|
||||
em.createQuery("SELECT u FROM Utilisateur u WHERE u.email = :email", Utilisateur.class)
|
||||
.setParameter("email", email).getResultList();
|
||||
|
||||
if (results.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return results.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void addToSession(int sessionId, int userId) {
|
||||
EntityTransaction t = em.getTransaction();
|
||||
t.begin();
|
||||
Session s = em.find(Session.class, sessionId);
|
||||
Utilisateur u = em.find(Utilisateur.class, userId);
|
||||
|
||||
//FAUT AJOUTER OWNING SIDE ( Ici u --> s)
|
||||
u.getSession().add(s);
|
||||
//Jsp il faut birectionnelle ou pas?
|
||||
//s.getUtilisateurs().add(u);
|
||||
em.merge(u);
|
||||
t.commit();
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DTO;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@XmlRootElement
|
||||
public class QuestionDTO {
|
||||
private int id;
|
||||
private String question;
|
||||
private List<String> reponses_string;
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DTO;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@XmlRootElement
|
||||
public class QuizzDTO {
|
||||
private int id;
|
||||
private Integer sessionId;
|
||||
private Integer utilisateurId;
|
||||
private List<Integer> questionsId;
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DTO;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@XmlRootElement
|
||||
public class SessionDTO {
|
||||
private String theme;
|
||||
private int codePIN;
|
||||
private int id;
|
||||
|
||||
private List<Integer> quizzsId;
|
||||
private List<Integer> utilisateursId;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.DTO;
|
||||
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@XmlRootElement
|
||||
public class UtilisateurDTO {
|
||||
private int id;
|
||||
private String name;
|
||||
private String email;
|
||||
private String password;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.Mapper;
|
||||
|
||||
import fr.istic.taa.jaxrs.DTO.QuestionDTO;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Reponse;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface QuestionMapper {
|
||||
QuestionMapper INSTANCE = Mappers.getMapper( QuestionMapper.class );
|
||||
|
||||
|
||||
@Mapping(target = "reponses_string", expression = "java(question_ReponseString(question.getReponse()))")
|
||||
QuestionDTO toDTO(Question question);
|
||||
|
||||
Question toEntity(QuestionDTO questionDTO);
|
||||
|
||||
List<QuestionDTO> toDTOs(List<Question> questionList);
|
||||
|
||||
default List<String> question_ReponseString(Reponse reponse){
|
||||
return reponse.getReponses();
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.Mapper;
|
||||
|
||||
import fr.istic.taa.jaxrs.DTO.QuizzDTO;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Quizz;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface QuizzMapper {
|
||||
QuizzMapper INSTANCE = Mappers.getMapper( QuizzMapper.class );
|
||||
|
||||
//https://www.baeldung.com/mapstruct-map-source-object-target-list
|
||||
//https://mapstruct.org/
|
||||
@Mapping(target="sessionId", source="session.id")
|
||||
@Mapping(target="utilisateurId", source = "utilisateur.id")
|
||||
@Mapping(target="questionsId",expression="java(function_mapQ(quizz.getQuestions()))")
|
||||
QuizzDTO toDTO(Quizz quizz);
|
||||
Quizz toEntity(QuizzDTO quizzDTO);
|
||||
|
||||
List<QuizzDTO> toDTOs(List<Quizz> quizzes);
|
||||
|
||||
default List<Integer> function_mapQ(List<Question> questionList){
|
||||
List<Integer> list=new ArrayList<Integer>();
|
||||
for(Question question : questionList){
|
||||
list.add(question.getId());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.Mapper;
|
||||
|
||||
import fr.istic.taa.jaxrs.DTO.SessionDTO;
|
||||
import fr.istic.taa.jaxrs.metier.Quizz;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SessionMapper {
|
||||
SessionMapper INSTANCE = Mappers.getMapper( SessionMapper.class );
|
||||
|
||||
@Mapping(target="utilisateursId",expression="java(function_mapU(session.getUtilisateurs()))")
|
||||
@Mapping(target="quizzsId",expression="java(function_mapQ(session.getQuizzs()))")
|
||||
SessionDTO toDTO(Session session);
|
||||
Session toEntity(SessionDTO sessionDTO);
|
||||
|
||||
List<SessionDTO> toDTOs(List<Session> sessions);
|
||||
|
||||
//https://www.baeldung.com/mapstruct-map-source-object-target-list
|
||||
default List<Integer> function_mapU(List<Utilisateur> utilisateurList) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (Utilisateur utilisateur : utilisateurList) {
|
||||
result.add(utilisateur.getId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
default List<Integer> function_mapQ(List<Quizz> quizzList) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (Quizz quizz : quizzList) {
|
||||
result.add(quizz.getId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.Mapper;
|
||||
|
||||
import fr.istic.taa.jaxrs.DTO.UtilisateurDTO;
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface UtilisateurMapper {
|
||||
UtilisateurMapper INSTANCE = Mappers.getMapper(UtilisateurMapper.class);
|
||||
|
||||
UtilisateurDTO toDTO(Utilisateur utilisateur);
|
||||
Utilisateur toEntity(UtilisateurDTO dto);
|
||||
|
||||
List<UtilisateurDTO> toDTOs(List<Utilisateur> utilisateurList);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package fr.istic.taa.jaxrs;
|
||||
|
||||
import io.undertow.Undertow;
|
||||
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* RESTfull microservice, based on JAX-RS and JBoss Undertow
|
||||
*
|
||||
*/
|
||||
public class RestServer {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(RestServer.class.getName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
UndertowJaxrsServer ut = new UndertowJaxrsServer();
|
||||
TestApplication ta = new TestApplication();
|
||||
ut.deploy(ta);
|
||||
ut.start(
|
||||
Undertow.builder()
|
||||
.addHttpListener(8080, "localhost")
|
||||
|
||||
);
|
||||
|
||||
logger.info("JAX-RS based micro-service running!");
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* JBoss, Home of Professional Open Source
|
||||
* Copyright Red Hat, Inc., and individual contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package fr.istic.taa.jaxrs;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.istic.taa.jaxrs.rest.*;
|
||||
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
|
||||
import jakarta.ws.rs.ApplicationPath;
|
||||
import jakarta.ws.rs.core.Application;
|
||||
|
||||
@ApplicationPath("/")
|
||||
public class TestApplication extends Application {
|
||||
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getClasses() {
|
||||
|
||||
final Set<Class<?>> clazzes = new HashSet<Class<?>>();
|
||||
|
||||
clazzes.add(OpenApiResource.class);
|
||||
clazzes.add(PetResource.class);
|
||||
clazzes.add(QuestionResource.class);
|
||||
clazzes.add(QuizzResource.class);
|
||||
clazzes.add(UtilisateurResource.class);
|
||||
clazzes.add(SessionResource.class);
|
||||
clazzes.add(SwaggerResource.class);
|
||||
return clazzes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.swagger.v3.oas.models.tags.Tag;
|
||||
import jakarta.xml.bind.annotation.XmlElement;
|
||||
import jakarta.xml.bind.annotation.XmlElementWrapper;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "Pet")
|
||||
public class Pet {
|
||||
private long id;
|
||||
private String name;
|
||||
private List<Tag> tags = new ArrayList<Tag>();
|
||||
|
||||
@XmlElement(name = "id")
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement(name = "name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name = "tags")
|
||||
@XmlElement(name = "tag")
|
||||
public List<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(List<Tag> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
import fr.istic.taa.jaxrs.domain.Pet;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
@Path("pet")
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class PetResource {
|
||||
|
||||
@GET
|
||||
@Path("/{petId}")
|
||||
public Pet getPetById(@PathParam("petId") Long petId) {
|
||||
// return pet
|
||||
return new Pet();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/")
|
||||
public Pet getPet(Long petId) {
|
||||
return new Pet();
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@Consumes("application/json")
|
||||
public Response addPet(
|
||||
@Parameter(description = "Pet object that needs to be added to the store", required = true) Pet pet) {
|
||||
// add pet
|
||||
return Response.ok().entity("SUCCESS").build();
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import fr.istic.taa.jaxrs.DAO.QuestionDAO;
|
||||
import fr.istic.taa.jaxrs.DTO.QuestionDTO;
|
||||
import fr.istic.taa.jaxrs.Mapper.QuestionMapper;
|
||||
import fr.istic.taa.jaxrs.metier.Choix;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Reponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.PATCH;
|
||||
import jakarta.ws.rs.PUT;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.DELETE;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
|
||||
@Path("question")
|
||||
@Consumes({"application/json", "application/xml"})
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class QuestionResource {
|
||||
private final QuestionDAO questionDAO = new QuestionDAO();
|
||||
private final QuestionMapper mapper = QuestionMapper.INSTANCE;
|
||||
|
||||
@GET
|
||||
public List<Question> listQuestion(){
|
||||
List<Question> questions = questionDAO.findAll();
|
||||
return questions;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/addQuestion")
|
||||
public Response addQuestion(QuestionDTO dto){
|
||||
Question question = mapper.toEntity(dto);
|
||||
questionDAO.create(question);
|
||||
return Response.status(Response.Status.CREATED).entity("Question ajouté avec Succès : \"" + dto.getQuestion() + "\"").build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{question_id}/changeQuestion/")
|
||||
public Response changeQuestion(@PathParam("question_id") Integer id, String newQuestion){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
questionDAO.changeQuestion(newQuestion,id);
|
||||
return Response.status(Response.Status.CREATED).entity("Question mis à jour : " + newQuestion).build();
|
||||
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{question_id}/getReponse/")
|
||||
public Response getReponse(@PathParam("question_id") Integer id){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
Reponse result = question.getReponse();
|
||||
return Response.ok(result).build();
|
||||
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{question_id}/addReponse/")
|
||||
public Response addReponse(@PathParam("question_id") Integer id, String reponse){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
questionDAO.addReponse(reponse,id);
|
||||
return Response.status(Response.Status.CREATED).entity("Reponse correct \""+reponse+"\" ajouté à la question " + id).build();
|
||||
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{question_id}/deletReponses/")
|
||||
public Response addReponse(@PathParam("question_id") Integer id){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
questionDAO.deleteReponse(id);
|
||||
return Response.status(Response.Status.CREATED).entity("Reponses supprimé de la question " + id).build();
|
||||
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{question_id}/setReponse/choix")
|
||||
public Response setChoix(@PathParam("question_id") Integer id){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
questionDAO.setChoix(id);
|
||||
return Response.status(Response.Status.CREATED).entity("Reponses à choix multiple mise sur la question " + id).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{question_id}/setReponse/reponseCourte")
|
||||
public Response setReponseCourte(@PathParam("question_id") Integer id){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
questionDAO.setReponseCourte(id);
|
||||
return Response.status(Response.Status.CREATED).entity("Reponses courte mise sur la question " + id).build();
|
||||
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{question_id}/AddChoix")
|
||||
public Response addChoix(@PathParam("question_id") Integer id, String choix){
|
||||
Question question = questionDAO.findById(id);
|
||||
if(question == null){
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
if(question.getReponse() instanceof Choix){
|
||||
return Response.status(Response.Status.EXPECTATION_FAILED).build();
|
||||
}
|
||||
questionDAO.addChoix(id, choix);
|
||||
return Response.status(Response.Status.CREATED).entity("Reponses courte mise sur la question " + id).build();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
import fr.istic.taa.jaxrs.DAO.QuestionDAO;
|
||||
import fr.istic.taa.jaxrs.DAO.QuizzDAO;
|
||||
import fr.istic.taa.jaxrs.DTO.QuestionDTO;
|
||||
import fr.istic.taa.jaxrs.DTO.QuizzDTO;
|
||||
import fr.istic.taa.jaxrs.Mapper.QuestionMapper;
|
||||
import fr.istic.taa.jaxrs.Mapper.QuizzMapper;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Quizz;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Path("quizz")
|
||||
@Consumes({"application/json", "application/xml"})
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class QuizzResource {
|
||||
public final QuizzMapper mapper = QuizzMapper.INSTANCE;
|
||||
public final QuizzDAO quizzDAO= new QuizzDAO();
|
||||
public final QuestionDAO questionDAO = new QuestionDAO();
|
||||
|
||||
@GET
|
||||
@Path("/{quizz_id}")
|
||||
public Response getQuizzById(@PathParam("quizz_id") Integer quizzId) {
|
||||
Quizz quizz = quizzDAO.findById(quizzId);
|
||||
if (quizz == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
QuizzDTO dto = mapper.toDTO(quizz);
|
||||
return Response.status(Response.Status.OK).entity(dto).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{quizz_id}/questions")
|
||||
public Response getQuestions(@PathParam("quizz_id") Integer quizzId) {
|
||||
Quizz quizz = quizzDAO.findById(quizzId);
|
||||
if (quizz == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
List<Question> questionList= quizz.getQuestions();
|
||||
List<QuestionDTO> dtos = QuestionMapper.INSTANCE.toDTOs(questionList);
|
||||
|
||||
return Response.status(Response.Status.OK).entity(dtos).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{quizz_id}/add_question/{question_id}")
|
||||
public Response addQuestion(@PathParam("quizz_id") Integer quizzId, @PathParam("question_id") Integer questionId) {
|
||||
Quizz quizz = quizzDAO.findById(quizzId);
|
||||
Question question = questionDAO.findById(questionId);
|
||||
if (quizz == null || question == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
quizzDAO.addQuestion(quizzId,questionId);
|
||||
quizzDAO.update(quizz);
|
||||
QuizzDTO quizzDTO = mapper.toDTO(quizz);
|
||||
return Response.status(Response.Status.OK).entity(quizzDTO).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{quizz_id}/deleteQ")
|
||||
public Response deleteQuestion(@PathParam("quizz_id") Integer quizzId) {
|
||||
Quizz quizz = quizzDAO.findById(quizzId);
|
||||
if (quizz == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
quizzDAO.deleteAllQustion(quizzId);
|
||||
quizzDAO.update(quizz);
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
|
||||
import fr.istic.taa.jaxrs.DAO.SessionDAO;
|
||||
import fr.istic.taa.jaxrs.DTO.QuizzDTO;
|
||||
import fr.istic.taa.jaxrs.DTO.SessionDTO;
|
||||
import fr.istic.taa.jaxrs.DTO.UtilisateurDTO;
|
||||
import fr.istic.taa.jaxrs.Mapper.QuizzMapper;
|
||||
import fr.istic.taa.jaxrs.Mapper.SessionMapper;
|
||||
import fr.istic.taa.jaxrs.Mapper.UtilisateurMapper;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Path("session")
|
||||
@Consumes({"application/json", "application/xml"})
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class SessionResource {
|
||||
private final SessionDAO sessionDAO = new SessionDAO();
|
||||
private final SessionMapper mapper = SessionMapper.INSTANCE;
|
||||
|
||||
@GET
|
||||
public List<Session> listSession() {
|
||||
List<Session> sessions = sessionDAO.findAll();
|
||||
return sessions;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
public Response getSession(@PathParam("id") Integer id) {
|
||||
Session session = sessionDAO.findById(id);
|
||||
if (session == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
SessionDTO result = mapper.toDTO(session);
|
||||
return Response.status(Response.Status.OK).entity(result).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{session_id}/quizzs")
|
||||
public Response getQuizzs(@PathParam("session_id") Integer sessionId) {
|
||||
Session session = sessionDAO.findById(sessionId);
|
||||
if (session == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
List<QuizzDTO> results= QuizzMapper.INSTANCE.toDTOs(session.getQuizzs());
|
||||
return Response.status(Response.Status.OK).entity(results).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{session_id}/utilisateurs")
|
||||
public Response getUtilisateurs(@PathParam("session_id") Integer sessionId) {
|
||||
Session session = sessionDAO.findById(sessionId);
|
||||
if (session == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
List<Utilisateur> utilisateurList= session.getUtilisateurs();
|
||||
List<UtilisateurDTO> dtos= UtilisateurMapper.INSTANCE.toDTOs(utilisateurList);
|
||||
return Response.status(Response.Status.OK).entity(dtos).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{session_id}/delete")
|
||||
public Response deleteSession(@PathParam("session_id") Integer sessionId) {
|
||||
Session session = sessionDAO.findById(sessionId);
|
||||
if (session == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
sessionDAO.delete(session);
|
||||
return Response.status(Response.Status.OK).entity("Réussi de supprimer").build();
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
|
||||
@Path("/api")
|
||||
public class SwaggerResource {
|
||||
private static final Logger logger = Logger.getLogger(SwaggerResource.class.getName());
|
||||
|
||||
@GET
|
||||
public byte[] Get1() {
|
||||
try {
|
||||
return Files.readAllBytes(FileSystems.getDefault().getPath("src/main/webapp/swagger/index.html"));
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{path:.*}")
|
||||
public byte[] Get(@PathParam("path") String path) {
|
||||
try {
|
||||
return Files.readAllBytes(FileSystems.getDefault().getPath("src/main/webapp/swagger/"+path));
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
package fr.istic.taa.jaxrs.rest;
|
||||
|
||||
import fr.istic.taa.jaxrs.DAO.SessionDAO;
|
||||
import fr.istic.taa.jaxrs.DAO.UtilisateurDAO;
|
||||
import fr.istic.taa.jaxrs.DTO.SessionDTO;
|
||||
import fr.istic.taa.jaxrs.DTO.UtilisateurDTO;
|
||||
import fr.istic.taa.jaxrs.Mapper.SessionMapper;
|
||||
import fr.istic.taa.jaxrs.Mapper.UtilisateurMapper;
|
||||
import fr.istic.taa.jaxrs.metier.Question;
|
||||
import fr.istic.taa.jaxrs.metier.Session;
|
||||
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Path("utilisateur")
|
||||
@Consumes({"application/json", "application/xml"})
|
||||
@Produces({"application/json", "application/xml"})
|
||||
public class UtilisateurResource {
|
||||
private final UtilisateurDAO utilisateurDAO = new UtilisateurDAO();
|
||||
private final UtilisateurMapper mapper = UtilisateurMapper.INSTANCE;
|
||||
|
||||
@GET
|
||||
public List<Utilisateur> listUtilisateur() {
|
||||
List<Utilisateur> utilisateurs = utilisateurDAO.findAll();
|
||||
return utilisateurs;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/register")
|
||||
public Response registerUtilisateur(UtilisateurDTO dto) {
|
||||
String email_verification = dto.getEmail();
|
||||
Utilisateur existing = utilisateurDAO.findByEmail(email_verification);
|
||||
|
||||
//VERIFACTION S'IL EXISTE DANS BDD
|
||||
if (existing != null) {
|
||||
return Response.status(Response.Status.CONFLICT).entity("Email est déjà registré").build();
|
||||
}
|
||||
|
||||
Utilisateur utilisateur = mapper.toEntity(dto);
|
||||
utilisateurDAO.create(utilisateur);
|
||||
|
||||
return Response.status(Response.Status.CREATED).entity("Registration succès").build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/login")
|
||||
public Response loginUtilisateur(UtilisateurDTO dto) {
|
||||
Utilisateur utilisateur = utilisateurDAO.findByEmail(dto.getEmail());
|
||||
|
||||
if (utilisateur == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).entity("Email n'existe pas").build();
|
||||
} else if (!utilisateur.getPassword().equals(dto.getPassword())) {
|
||||
return Response.status(Response.Status.UNAUTHORIZED).entity("Mauvais mdp").build();
|
||||
}
|
||||
UtilisateurDTO result = mapper.toDTO(utilisateur);
|
||||
|
||||
return Response.status(Response.Status.OK).entity(result).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
@Operation(summary = "Get user by ID",
|
||||
tags = {"utilisateur"},
|
||||
description = "Get User by ID",
|
||||
responses = {
|
||||
@ApiResponse(description = "Utilisateur", content = @Content(
|
||||
schema = @Schema(implementation = Utilisateur.class)
|
||||
)),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
|
||||
@ApiResponse(responseCode = "404", description = "User not found")
|
||||
})
|
||||
public Response getUtilisateur(@Parameter(
|
||||
description = "ID of user that needs to be fetched",
|
||||
schema = @Schema(
|
||||
type = "integer",
|
||||
format = "int32",
|
||||
description = "param ID of user that needs to be fetched"
|
||||
),
|
||||
required = true)
|
||||
@PathParam("id") Integer id) {
|
||||
Utilisateur utilisateur = utilisateurDAO.findById(id);
|
||||
if (utilisateur == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
UtilisateurDTO result = mapper.toDTO(utilisateur);
|
||||
return Response.ok(result).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{user_id}/add_session/{session_id}")
|
||||
public Response addSession(@PathParam("user_id") Integer user_id, @PathParam("session_id") Integer session_id) {
|
||||
SessionDAO sessionDAO = new SessionDAO();
|
||||
|
||||
Session existingSession = sessionDAO.findById(session_id);
|
||||
Utilisateur utilisateur = utilisateurDAO.findById(user_id);
|
||||
|
||||
if (existingSession == null || utilisateur == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
//If user already joined
|
||||
if (utilisateur.getSession().contains(existingSession)) {
|
||||
return Response.status(Response.Status.CONFLICT).build();
|
||||
}
|
||||
utilisateurDAO.addToSession(user_id, session_id);
|
||||
utilisateurDAO.update(utilisateur);
|
||||
|
||||
// We update it so have to return new DTO
|
||||
UtilisateurDTO dto = mapper.toDTO(utilisateur);
|
||||
|
||||
return Response.status(Response.Status.OK).entity(dto).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{user_id}/session")
|
||||
public Response listSession(@PathParam("user_id") Integer user_id) {
|
||||
Utilisateur utilisateur = utilisateurDAO.findById(user_id);
|
||||
if (utilisateur == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
List<Session> sess = utilisateur.getSession();
|
||||
List<SessionDTO> dtos = SessionMapper.INSTANCE.toDTOs(sess);
|
||||
return Response.status(Response.Status.OK).entity(dtos).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{user_id}/delete")
|
||||
public Response deleteUtilisateur(@PathParam("user_id") Integer user_id) {
|
||||
Utilisateur existing = utilisateurDAO.findById(user_id);
|
||||
if (existing == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
utilisateurDAO.delete(existing);
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
}
|
||||
29
src/main/java/sample/data/jpa/SampleDataJpaApplication.java
Normal file
29
src/main/java/sample/data/jpa/SampleDataJpaApplication.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.data.jpa;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleDataJpaApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleDataJpaApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
66
src/main/java/sample/data/jpa/domain/User.java
Normal file
66
src/main/java/sample/data/jpa/domain/User.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package sample.data.jpa.domain;
|
||||
// Imports ...
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
// An autogenerated id (unique for each user in the db)
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@NotNull
|
||||
private String email;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
// Public methods
|
||||
|
||||
public User() { }
|
||||
|
||||
public User(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public User(String email, String name) {
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// Getter and setter methods
|
||||
// ...
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.PrimaryKeyJoinColumn;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
@@ -1,4 +1,4 @@
|
||||
package fr.istic.taa.jaxrs.metier;
|
||||
package sample.data.jpa.metier;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
4
src/main/java/sample/data/jpa/service/QuestionDao.java
Normal file
4
src/main/java/sample/data/jpa/service/QuestionDao.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
public interface QuestionDao {
|
||||
}
|
||||
4
src/main/java/sample/data/jpa/service/QuizzDao.java
Normal file
4
src/main/java/sample/data/jpa/service/QuizzDao.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
public interface QuizzDao {
|
||||
}
|
||||
4
src/main/java/sample/data/jpa/service/ReponseDao.java
Normal file
4
src/main/java/sample/data/jpa/service/ReponseDao.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
public interface ReponseDao {
|
||||
}
|
||||
4
src/main/java/sample/data/jpa/service/SessionDao.java
Normal file
4
src/main/java/sample/data/jpa/service/SessionDao.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
public interface SessionDao {
|
||||
}
|
||||
20
src/main/java/sample/data/jpa/service/UserDao.java
Normal file
20
src/main/java/sample/data/jpa/service/UserDao.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import sample.data.jpa.domain.User;
|
||||
|
||||
// Imports ...
|
||||
|
||||
@Transactional
|
||||
public interface UserDao extends JpaRepository<User, Long> {
|
||||
|
||||
/**
|
||||
* This method will find an User instance in the database by its email.
|
||||
* Note that this method is not implemented and its working code will be
|
||||
* automagically generated from its signature by Spring Data JPA.
|
||||
*/
|
||||
public User findByEmail(String email);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package sample.data.jpa.service;
|
||||
|
||||
public interface UtilisateurDao {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
@Controller
|
||||
public class QuestionController {
|
||||
|
||||
}
|
||||
6
src/main/java/sample/data/jpa/web/QuizzController.java
Normal file
6
src/main/java/sample/data/jpa/web/QuizzController.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
@Controller
|
||||
public class QuizzController {
|
||||
|
||||
}
|
||||
6
src/main/java/sample/data/jpa/web/ReponseController.java
Normal file
6
src/main/java/sample/data/jpa/web/ReponseController.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
@Controller
|
||||
public class ReponseController {
|
||||
|
||||
}
|
||||
6
src/main/java/sample/data/jpa/web/SessionController.java
Normal file
6
src/main/java/sample/data/jpa/web/SessionController.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
@Controller
|
||||
public class SessionController {
|
||||
|
||||
}
|
||||
91
src/main/java/sample/data/jpa/web/UserController.java
Normal file
91
src/main/java/sample/data/jpa/web/UserController.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import sample.data.jpa.domain.User;
|
||||
import sample.data.jpa.service.UserDao;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
/**
|
||||
* GET /create --> Create a new user and save it in the database.
|
||||
*/
|
||||
@RequestMapping("/create")
|
||||
@ResponseBody
|
||||
public String create(String email, String name) {
|
||||
String userId = "";
|
||||
try {
|
||||
User user = new User(email, name);
|
||||
userDao.save(user);
|
||||
userId = String.valueOf(user.getId());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error creating the user: " + ex.toString();
|
||||
}
|
||||
return "User succesfully created with id = " + userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /delete --> Delete the user having the passed id.
|
||||
*/
|
||||
@RequestMapping("/delete")
|
||||
@ResponseBody
|
||||
public String delete(long id) {
|
||||
try {
|
||||
User user = new User(id);
|
||||
userDao.delete(user);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error deleting the user:" + ex.toString();
|
||||
}
|
||||
return "User succesfully deleted!";
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /get-by-email --> Return the id for the user having the passed
|
||||
* email.
|
||||
*/
|
||||
@RequestMapping("/get-by-email/{email}")
|
||||
@ResponseBody
|
||||
public String getByEmail(@PathVariable("email") String email) {
|
||||
String userId = "";
|
||||
try {
|
||||
User user = userDao.findByEmail(email);
|
||||
userId = String.valueOf(user.getId());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "User not found";
|
||||
}
|
||||
return "The user id is: " + userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /update --> Update the email and the name for the user in the
|
||||
* database having the passed id.
|
||||
*/
|
||||
@RequestMapping("/update")
|
||||
@ResponseBody
|
||||
public String updateUser(long id, String email, String name) {
|
||||
try {
|
||||
User user = userDao.findById(id).get();
|
||||
user.setEmail(email);
|
||||
user.setName(name);
|
||||
userDao.save(user);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return "Error updating the user: " + ex.toString();
|
||||
}
|
||||
return "User succesfully updated!";
|
||||
}
|
||||
|
||||
// Private fields
|
||||
|
||||
@Autowired
|
||||
private UserDao userDao;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package sample.data.jpa.web;
|
||||
|
||||
@Controller
|
||||
public class UtilisateurController {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user