push init

This commit is contained in:
tuanvu
2025-10-06 10:52:07 +02:00
parent b446b16027
commit e3743e6d16
52 changed files with 720 additions and 1282 deletions

View 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);
}
}

View 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
// ...
}

View File

@@ -0,0 +1,28 @@
package sample.data.jpa.metier;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.PrimaryKeyJoinColumn;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
@PrimaryKeyJoinColumn(name = "Choix_Id")
public class Choix extends Reponse{
List<String> choix;
@Override
public String valHTML(){
String res = "";
for (String val : this.choix) {
res+=val+"<br/>";
}
return res;
}
}

View File

@@ -0,0 +1,27 @@
package sample.data.jpa.metier;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Question implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String question;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="bonne_reponse", referencedColumnName = "id")
private Reponse reponse;
@ManyToOne
@JoinColumn(name="id_quizz")
private Quizz quizz;
}

View File

@@ -0,0 +1,30 @@
package sample.data.jpa.metier;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Quizz implements Serializable {
@Id
@GeneratedValue
private int id;
@ManyToOne
private Session session;
@ManyToOne
@JoinColumn(name="id_utilisateur")
private Utilisateur utilisateur;
@OneToMany(mappedBy = "quizz")
private List<Question> questions=new ArrayList<Question>();
}

View File

@@ -0,0 +1,30 @@
package sample.data.jpa.metier;
import java.io.Serializable;
import java.util.List;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="Type_reponse")
@DiscriminatorValue("Reponse")
public abstract class Reponse implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@OneToOne
private Question question;
public List<String> reponses;
public String valHTML(){
return "";
}
}

View File

@@ -0,0 +1,21 @@
package sample.data.jpa.metier;
import jakarta.persistence.Entity;
import jakarta.persistence.PrimaryKeyJoinColumn;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
@PrimaryKeyJoinColumn(name = "RC_Id")
public class ReponseCourte extends Reponse{
String value;
@Override
public String valHTML(){
return "INPUT";
}
}

View File

@@ -0,0 +1,30 @@
package sample.data.jpa.metier;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Session implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(unique=true)
private int codePIN;
@OneToMany(mappedBy="session")
private List<Quizz> quizzs = new ArrayList<>();
@ManyToMany(mappedBy = "session")
private List<Utilisateur> utilisateurs = new ArrayList<>();
private String theme;
}

View File

@@ -0,0 +1,33 @@
package sample.data.jpa.metier;
import jakarta.persistence.*;
import lombok.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Utilisateur implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@Column(unique=true)
private String email;
private String password;
@ManyToMany
@JoinTable(
name="utilisateur_session",
joinColumns = @JoinColumn(name="utilisateur_id"),
inverseJoinColumns = @JoinColumn(name = "session_id")
)
private List<Session> session= new ArrayList<>();;
@OneToMany(mappedBy = "utilisateur")
private List<Quizz> quizzs;
}

View File

@@ -0,0 +1,4 @@
package sample.data.jpa.service;
public interface QuestionDao {
}

View File

@@ -0,0 +1,4 @@
package sample.data.jpa.service;
public interface QuizzDao {
}

View File

@@ -0,0 +1,4 @@
package sample.data.jpa.service;
public interface ReponseDao {
}

View File

@@ -0,0 +1,4 @@
package sample.data.jpa.service;
public interface SessionDao {
}

View 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);
}

View File

@@ -0,0 +1,4 @@
package sample.data.jpa.service;
public interface UtilisateurDao {
}

View File

@@ -0,0 +1,6 @@
package sample.data.jpa.web;
@Controller
public class QuestionController {
}

View File

@@ -0,0 +1,6 @@
package sample.data.jpa.web;
@Controller
public class QuizzController {
}

View File

@@ -0,0 +1,6 @@
package sample.data.jpa.web;
@Controller
public class ReponseController {
}

View File

@@ -0,0 +1,6 @@
package sample.data.jpa.web;
@Controller
public class SessionController {
}

View 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;
}

View File

@@ -0,0 +1,6 @@
package sample.data.jpa.web;
@Controller
public class UtilisateurController {
}