Brand new Web services project

TommyVersion
tomivt 3 weeks ago
parent fa1a716e6a
commit 236378e0cb

@ -1,13 +0,0 @@
# Étape de build
FROM maven:3.8.6-openjdk-21 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
# Étape d'exécution
FROM openjdk:21-jdk
WORKDIR /app
COPY --from=build /app/target/demo-1.0.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

@ -1,14 +0,0 @@
kind: pipeline
type: docker
name: default
steps:
- name: build
image: maven:3.8.6-openjdk-21
commands:
- mvn clean package -DskipTests
- name: test
image: maven:3.8.6-openjdk-21
commands:
- mvn test

@ -1,88 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>WF-WEBAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WF-WEBAPI</name>
<description>WF-WEBAPI</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -1,11 +0,0 @@
package com.example.wfwebapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WfWebapiApplication {
public static void main(String[] args) {
SpringApplication.run(WfWebapiApplication.class, args);
}
}

@ -1,21 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Admin")
public class Admin {
@Id
@Column(name = "users")
private Long userId;
@OneToOne
@JoinColumn(name = "users", insertable = false, updatable = false)
private User user;
// Getters et setters
public Long getUserId() { return userId; }
public void setUserId(Long userId) { this.userId = userId; }
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
}

@ -1,27 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Caracter")
public class Caracter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_caracter")
private Long id;
@Column(name = "caracter", nullable = false, length = 100)
private String caracter;
@ManyToOne
@JoinColumn(name = "id_img", nullable = false)
private Image image;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getCaracter() { return caracter; }
public void setCaracter(String caracter) { this.caracter = caracter; }
public Image getImage() { return image; }
public void setImage(Image image) { this.image = image; }
}

@ -1,39 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
import java.time.LocalDate;
@Entity
@Table(name = "Commentary")
public class Commentary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_comment")
private Long id;
@ManyToOne
@JoinColumn(name = "quote", nullable = false)
private Quote quote;
@ManyToOne
@JoinColumn(name = "users", nullable = false)
private User user;
@Column(name = "dateC", nullable = false)
private LocalDate dateC;
@Column(name = "comment", nullable = false, columnDefinition = "text")
private String comment;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public Quote getQuote() { return quote; }
public void setQuote(Quote quote) { this.quote = quote; }
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public LocalDate getDateC() { return dateC; }
public void setDateC(LocalDate dateC) { this.dateC = dateC; }
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }
}

@ -1,21 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "DailyQuote")
public class DailyQuote {
@Id
@Column(name = "citation_id")
private Long citationId;
@OneToOne
@JoinColumn(name = "citation_id", insertable = false, updatable = false)
private Quote quote;
// Getters et setters
public Long getCitationId() { return citationId; }
public void setCitationId(Long citationId) { this.citationId = citationId; }
public Quote getQuote() { return quote; }
public void setQuote(Quote quote) { this.quote = quote; }
}

@ -1,28 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Favorite")
public class Favorite {
@EmbeddedId
private FavoriteId id;
@ManyToOne
@MapsId("user")
@JoinColumn(name = "users")
private User user;
@ManyToOne
@MapsId("quote")
@JoinColumn(name = "quote")
private Quote quote;
// Getters et setters
public FavoriteId getId() { return id; }
public void setId(FavoriteId id) { this.id = id; }
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public Quote getQuote() { return quote; }
public void setQuote(Quote quote) { this.quote = quote; }
}

@ -1,37 +0,0 @@
package com.example.wfwebapi.model;
import java.io.Serializable;
import java.util.Objects;
import jakarta.persistence.Embeddable;
@Embeddable
public class FavoriteId implements Serializable {
private Long user;
private Long quote;
public FavoriteId() {}
public FavoriteId(Long user, Long quote) {
this.user = user;
this.quote = quote;
}
public Long getUser() { return user; }
public void setUser(Long user) { this.user = user; }
public Long getQuote() { return quote; }
public void setQuote(Long quote) { this.quote = quote; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FavoriteId)) return false;
FavoriteId that = (FavoriteId) o;
return Objects.equals(getUser(), that.getUser()) &&
Objects.equals(getQuote(), that.getQuote());
}
@Override
public int hashCode() {
return Objects.hash(getUser(), getQuote());
}
}

@ -1,21 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Image")
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_img")
private Long id;
@Column(name = "imgPath", nullable = false, unique = true, length = 300)
private String imgPath;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getImgPath() { return imgPath; }
public void setImgPath(String imgPath) { this.imgPath = imgPath; }
}

@ -1,46 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_question")
private Long id;
@Column(name = "texte", nullable = false, columnDefinition = "text", unique = true)
private String texte;
@Column(name = "answerA", nullable = false, length = 30)
private String answerA;
@Column(name = "answerB", nullable = false, length = 30)
private String answerB;
@Column(name = "answerC", nullable = false, length = 30)
private String answerC;
@Column(name = "answerD", nullable = false, length = 30)
private String answerD;
@Column(name = "cAnswer", nullable = false, length = 30)
private String cAnswer;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTexte() { return texte; }
public void setTexte(String texte) { this.texte = texte; }
public String getAnswerA() { return answerA; }
public void setAnswerA(String answerA) { this.answerA = answerA; }
public String getAnswerB() { return answerB; }
public void setAnswerB(String answerB) { this.answerB = answerB; }
public String getAnswerC() { return answerC; }
public void setAnswerC(String answerC) { this.answerC = answerC; }
public String getAnswerD() { return answerD; }
public void setAnswerD(String answerD) { this.answerD = answerD; }
public String getCAnswer() { return cAnswer; }
public void setCAnswer(String cAnswer) { this.cAnswer = cAnswer; }
}

@ -1,32 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Quiz")
public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_quiz")
private Long id;
@Column(name = "title", nullable = false, length = 40)
private String title;
@ManyToOne
@JoinColumn(name = "img", nullable = false)
private Image image;
@Column(name = "nb_quest")
private Integer nbQuest;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public Image getImage() { return image; }
public void setImage(Image image) { this.image = image; }
public Integer getNbQuest() { return nbQuest; }
public void setNbQuest(Integer nbQuest) { this.nbQuest = nbQuest; }
}

@ -1,28 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Quiz_Question")
public class QuizQuestion {
@EmbeddedId
private QuizQuestionId id;
@ManyToOne
@MapsId("quiz")
@JoinColumn(name = "quiz")
private Quiz quiz;
@ManyToOne
@MapsId("question")
@JoinColumn(name = "question")
private Question question;
// Getters et setters
public QuizQuestionId getId() { return id; }
public void setId(QuizQuestionId id) { this.id = id; }
public Quiz getQuiz() { return quiz; }
public void setQuiz(Quiz quiz) { this.quiz = quiz; }
public Question getQuestion() { return question; }
public void setQuestion(Question question) { this.question = question; }
}

@ -1,37 +0,0 @@
package com.example.wfwebapi.model;
import java.io.Serializable;
import java.util.Objects;
import jakarta.persistence.Embeddable;
@Embeddable
public class QuizQuestionId implements Serializable {
private Long quiz;
private Long question;
public QuizQuestionId() {}
public QuizQuestionId(Long quiz, Long question) {
this.quiz = quiz;
this.question = question;
}
public Long getQuiz() { return quiz; }
public void setQuiz(Long quiz) { this.quiz = quiz; }
public Long getQuestion() { return question; }
public void setQuestion(Long question) { this.question = question; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof QuizQuestionId)) return false;
QuizQuestionId that = (QuizQuestionId) o;
return Objects.equals(getQuiz(), that.getQuiz()) &&
Objects.equals(getQuestion(), that.getQuestion());
}
@Override
public int hashCode() {
return Objects.hash(getQuiz(), getQuestion());
}
}

@ -1,59 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Quote")
public class Quote {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_quote")
private Long id;
@Column(name = "content", nullable = false, columnDefinition = "text")
private String content;
@Column(name = "likes")
private Integer likes;
@Column(name = "langue", nullable = false, length = 2)
private String langue;
@Column(name = "isValide", nullable = false)
private Boolean isValide;
@Column(name = "reason", nullable = false, length = 100)
private String reason;
@ManyToOne
@JoinColumn(name = "id_caracter", nullable = false)
private Caracter caracter;
@ManyToOne
@JoinColumn(name = "id_source", nullable = false)
private Source source;
@ManyToOne
@JoinColumn(name = "id_user_verif", nullable = false)
private User userVerif;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public Integer getLikes() { return likes; }
public void setLikes(Integer likes) { this.likes = likes; }
public String getLangue() { return langue; }
public void setLangue(String langue) { this.langue = langue; }
public Boolean getIsValide() { return isValide; }
public void setIsValide(Boolean isValide) { this.isValide = isValide; }
public String getReason() { return reason; }
public void setReason(String reason) { this.reason = reason; }
public Caracter getCaracter() { return caracter; }
public void setCaracter(Caracter caracter) { this.caracter = caracter; }
public Source getSource() { return source; }
public void setSource(Source source) { this.source = source; }
public User getUserVerif() { return userVerif; }
public void setUserVerif(User userVerif) { this.userVerif = userVerif; }
}

@ -1,38 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Record_quiz")
public class RecordQuiz {
@EmbeddedId
private RecordQuizId id;
@ManyToOne
@MapsId("user")
@JoinColumn(name = "users")
private User user;
@ManyToOne
@MapsId("quiz")
@JoinColumn(name = "quiz")
private Quiz quiz;
@Column(name = "nbPoint")
private Integer nbPoint;
@Column(name = "timeQ")
private Integer timeQ;
// Getters et setters
public RecordQuizId getId() { return id; }
public void setId(RecordQuizId id) { this.id = id; }
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
public Quiz getQuiz() { return quiz; }
public void setQuiz(Quiz quiz) { this.quiz = quiz; }
public Integer getNbPoint() { return nbPoint; }
public void setNbPoint(Integer nbPoint) { this.nbPoint = nbPoint; }
public Integer getTimeQ() { return timeQ; }
public void setTimeQ(Integer timeQ) { this.timeQ = timeQ; }
}

@ -1,37 +0,0 @@
package com.example.wfwebapi.model;
import java.io.Serializable;
import java.util.Objects;
import jakarta.persistence.Embeddable;
@Embeddable
public class RecordQuizId implements Serializable {
private Long user;
private Long quiz;
public RecordQuizId() {}
public RecordQuizId(Long user, Long quiz) {
this.user = user;
this.quiz = quiz;
}
public Long getUser() { return user; }
public void setUser(Long user) { this.user = user; }
public Long getQuiz() { return quiz; }
public void setQuiz(Long quiz) { this.quiz = quiz; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RecordQuizId)) return false;
RecordQuizId that = (RecordQuizId) o;
return Objects.equals(getUser(), that.getUser()) &&
Objects.equals(getQuiz(), that.getQuiz());
}
@Override
public int hashCode() {
return Objects.hash(getUser(), getQuiz());
}
}

@ -1,26 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Source")
public class Source {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_source")
private Long id;
@Column(name = "title", nullable = false, length = 100)
private String title;
@Column(name = "dateS", nullable = false)
private Integer dateS;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public Integer getDateS() { return dateS; }
public void setDateS(Integer dateS) { this.dateS = dateS; }
}

@ -1,43 +0,0 @@
package com.example.wfwebapi.model;
import jakarta.persistence.*;
import java.time.LocalDate;
@Entity
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_user")
private Long id;
@Column(name = "username", nullable = false, length = 50)
private String username;
@Column(name = "email", nullable = false, length = 50)
private String email;
@Column(name = "password", nullable = false, length = 100)
private String password;
@ManyToOne
@JoinColumn(name = "img", nullable = false)
private Image image;
@Column(name = "creation", nullable = false)
private LocalDate creation;
// Getters et setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public Image getImage() { return image; }
public void setImage(Image image) { this.image = image; }
public LocalDate getCreation() { return creation; }
public void setCreation(LocalDate creation) { this.creation = creation; }
}

@ -1,12 +0,0 @@
spring.application.name=WF-WEBAPI
# Utilisation de H2 avec le script d'initialisation
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
spring.sql.init.schema-locations=classpath:init.sql
spring.jpa.hibernate.ddl-auto=none
spring.h2.console.enabled=true
server.port=8080

@ -1,338 +0,0 @@
-- Suppression des tables
DROP TABLE IF EXISTS Commentary;
DROP TABLE IF EXISTS Favorite;
DROP TABLE IF EXISTS DailyQuote;
DROP TABLE IF EXISTS Quote;
DROP TABLE IF EXISTS Caracter;
DROP TABLE IF EXISTS Source;
DROP TABLE IF EXISTS Record_quiz;
DROP TABLE IF EXISTS Quiz_Question;
DROP TABLE IF EXISTS Quiz;
DROP TABLE IF EXISTS Question;
DROP TABLE IF EXISTS Admin;
DROP TABLE IF EXISTS Users;
DROP TABLE IF EXISTS Image;
-- Création des tables
-------------------------------------------------------------------------
CREATE TABLE Image(
id_img NUMERIC PRIMARY KEY,
imgPath varchar(300) NOT NULL UNIQUE
);
-------------------------------------------------------------------------
CREATE TABLE Users(
id_user SERIAL PRIMARY KEY,
username varchar(50) NOT NULL,
email varchar(50) NOT NULL,
password varchar(100) NOT NULL,
img NUMERIC NOT NULL,
creation date NOT NULL,
CONSTRAINT unique_col UNIQUE (email),
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
);
Create OR REPLACE Function IfUserIsAdmin() RETURNS trigger AS $$
DECLARE
BEGIN
Delete From Admin
where users = OLD.id_user;
RETURN OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger IfUserIsAdmin BEFORE DELETE on Users
FOR EACH ROW
EXECUTE FUNCTION IfUserIsAdmin();
Create OR REPLACE Function DeleteUserFavorite() RETURNS trigger AS $$
DECLARE
BEGIN
Delete From Favorite
where users = OLD.id_user;
RETURN OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger DeleteUserFavorite BEFORE DELETE on Users
FOR EACH ROW
EXECUTE FUNCTION DeleteUserFavorite();
Create OR REPLACE Function DeleteUserCommentary() RETURNS trigger AS $$
DECLARE
BEGIN
Delete From Commentary
where users = OLD.id_user;
RETURN OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger DeleteUserCommentary BEFORE DELETE on Users
FOR EACH ROW
EXECUTE FUNCTION DeleteUserCommentary();
-------------------------------------------------------------------------
CREATE TABLE Admin(
users SERIAL PRIMARY KEY,
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
);
-------------------------------------------------------------------------
CREATE TABLE Question(
id_question SERIAL PRIMARY KEY,
texte text NOT NULL UNIQUE,
answerA varchar(30) NOT NULL,
answerB varchar(30) NOT NULL,
answerC varchar(30) NOT NULL,
answerD varchar(30) NOT NULL,
cAnswer varchar(30) NOT NULL,
CONSTRAINT check_cAnswer CHECK (cAnswer = answerA OR cAnswer = answerB OR cAnswer = answerC OR cAnswer = answerD)
);
-------------------------------------------------------------------------
CREATE TABLE Quiz(
id_quiz SERIAL PRIMARY KEY,
title varchar(40) NOT NULL,
img NUMERIC NOT NULL,
nb_quest numeric Default 0,
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
);
Create OR REPLACE Function DeleteQuiz() RETURNS trigger AS $$
DECLARE
BEGIN
Delete From Quiz_Question
where quiz=OLD.id_quiz;
Delete From Record_quiz
where quiz=OLD.id_quiz;
END;
$$ LANGUAGE plpgsql ;
Create Trigger DeleteQuiz BEFORE DELETE on Quiz
FOR EACH ROW
EXECUTE FUNCTION DeleteQuiz();
-------------------------------------------------------------------------
CREATE TABLE Quiz_Question(
quiz SERIAL NOT NULL,
question SERIAL NOT NULL,
PRIMARY KEY (quiz, question),
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
CONSTRAINT fk_question FOREIGN KEY(question) REFERENCES Question(id_question)
);
Create OR REPLACE Function NombreQuestionQuiz() RETURNS trigger AS $$
DECLARE
nb numeric;
BEGIN
IF TG_OP='DELETE' Then
SELECT count(quiz) INTO nb
FROM Quiz_Question
WHERE quiz = OLD.quiz;
Else
SELECT count(quiz) INTO nb
FROM Quiz_Question
WHERE quiz = NEW.quiz;
End IF;
Update Quiz
set nb_quest=nb
where id_quiz=NEW.quiz;
Return OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger NombreQuestionQuiz AFTER INSERT or UPDATE or DELETE on Quiz_Question
FOR EACH ROW
EXECUTE FUNCTION NombreQuestionQuiz();
-------------------------------------------------------------------------
CREATE TABLE Record_quiz(
users SERIAL NOT NULL,
quiz SERIAL NOT NULL,
nbPoint numeric DEFAULT 0,
timeQ numeric DEFAULT 0,
PRIMARY KEY (users, quiz),
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user),
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
CONSTRAINT err_nbPoint CHECK(nbPoint >= 0),
CONSTRAINT err_timeQ CHECK(timeQ >= 0)
);
-------------------------------------------------------------------------
CREATE TABLE Source(
id_source SERIAL PRIMARY KEY,
title varchar(100) NOT NULL,
dateS numeric(4) NOT NULL
);
-------------------------------------------------------------------------
CREATE TABLE Caracter(
id_caracter SERIAL PRIMARY KEY,
caracter varchar(100) NOT NULL,
id_img NUMERIC NOT NULL
);
-------------------------------------------------------------------------
CREATE TABLE Quote(
id_quote SERIAL PRIMARY KEY,
content text NOT NULL,
likes numeric DEFAULT '0',
langue char(2) NOT NULL,
isValide boolean NOT NULL DEFAULT 'false',
reason varchar(100) NOT NULL,
id_caracter SERIAL NOT NULL,
id_source SERIAL NOT NULL,
id_user_verif SERIAL NOT NULL,
CONSTRAINT fk_caracter FOREIGN KEY(id_caracter) REFERENCES Caracter(id_caracter),
CONSTRAINT fk_source FOREIGN KEY(id_source) REFERENCES Source(id_source),
CONSTRAINT fk_userverif FOREIGN KEY(id_user_verif) REFERENCES Users(id_user),
CONSTRAINT err_nbLike CHECK (likes >= 0),
CONSTRAINT err_language CHECK (langue = 'fr' OR langue = 'en')
);
Create OR REPLACE Function DeleteQuoteBEFORE() RETURNS trigger AS $$
DECLARE
BEGIN
Delete From Favorite
where quote=OLD.id_quote;
Delete From Commentary
where quote=OLD.id_quote;
If OLD.id_quote in (Select citation_id From DailyQuote) Then
Update DailyQuote
set citation_id = (Select id_quote
From Quote
Where id_quote!=OLD.id_quote
ORDER BY RAND()
LIMIT 1)
Where citation_id=OLD.id_quote;
END IF;
Return OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger DeleteQuoteBEFORE BEFORE DELETE on Quote
FOR EACH ROW
EXECUTE FUNCTION DeleteQuoteBEFORE();
Create OR REPLACE Function DeleteQuoteAFTER() RETURNS trigger AS $$
DECLARE
nb numeric;
BEGIN
Select count(id_caracter) into nb
from Quote
where id_caracter=OLD.id_caracter;
IF nb <= 1 Then
Delete from Caracter
where id_caracter=OLD.id_caracter;
END IF;
Select count(id_source) into nb
from Quote
where id_source=OLD.id_source;
IF nb <= 1 Then
Delete from Source
where id_source=OLD.id_source;
END IF;
Return OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger DeleteQuoteAFTER AFTER DELETE on Quote
FOR EACH ROW
EXECUTE FUNCTION DeleteQuoteAFTER();
-------------------------------------------------------------------------
CREATE TABLE DailyQuote(
citation_id INT PRIMARY KEY,
FOREIGN KEY (citation_id) REFERENCES Quote(id_quote) ON DELETE CASCADE
);
Create OR REPLACE Function UniqueDailyQuote() RETURNS trigger AS $$
DECLARE
nb numeric;
BEGIN
Select count(*) into nb
from DailyQuote;
IF nb = 0 Then
INSERT INTO DailyQuote (citation_id)
VALUES( (Select id_quote
From Quote
Where id_quote!=OLD.id_quote
ORDER BY RAND()
LIMIT 1 ) );
ELSIF nb>1 then
DELETE From DailyQuote
where citation_id!=NEW.citation_id;
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql ;
Create Trigger UniqueDailyQuote AFTER INSERT or DELETE on DailyQuote
FOR EACH ROW
EXECUTE FUNCTION UniqueDailyQuote();
-------------------------------------------------------------------------
CREATE TABLE Favorite(
users SERIAL NOT NULL,
quote SERIAL NOT NULL,
PRIMARY KEY (users, quote),
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
);
-------------------------------------------------------------------------
CREATE TABLE Commentary(
id_comment SERIAL PRIMARY KEY,
quote SERIAL NOT NULL,
users SERIAL NOT NULL,
dateC date NOT NULL,
comment text NOT NULL,
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
);
-------------------------------------------------------------------------

@ -0,0 +1,2 @@
/mvnw text eol=lf
*.cmd text eol=crlf

33
wtf/.gitignore vendored

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>wtf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wtf</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,13 @@
package com.example.wtf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WtfApplication {
public static void main(String[] args) {
SpringApplication.run(WtfApplication.class, args);
}
}

@ -0,0 +1,114 @@
package com.example.wtf.controller;
import com.example.wtf.exception.ResourceNotFound;
import com.example.wtf.model.Question;
import com.example.wtf.repository.questionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@RestController
@RequestMapping("/questions")
public class questionController {
@Autowired
private questionRepository repo;
@GetMapping("/get")
public @ResponseBody CollectionModel<EntityModel<Question>> getQuestions() {
List<EntityModel<Question>> questions = new ArrayList<>();
for (Question question : repo.findAll()) {
EntityModel<Question> questionResource = EntityModel.of(
question,
linkTo(methodOn(questionController.class).getQuestion(question.getId())).withSelfRel(),
linkTo(methodOn(questionController.class).updateQuestion(question.getId(), null)).withRel("Update question"),
linkTo(methodOn(questionController.class).deleteQuestion(question.getId())).withRel("Delete question")
);
questions.add(questionResource);
}
return CollectionModel.of(
questions,
linkTo(methodOn(questionController.class).addQuestion(null)).withRel("Add Question")
);
}
@GetMapping("/get/{id}")
public @ResponseBody EntityModel<Question> getQuestion(@PathVariable Long id) {
Question question = repo.findQuestionById(id);
if (question == null) {
throw new ResourceNotFound(HttpStatus.NOT_FOUND, "Question not found");
}
return EntityModel.of(
question,
linkTo(methodOn(questionController.class).updateQuestion(question.getId(), null)).withRel("Update question"),
linkTo(methodOn(questionController.class).deleteQuestion(question.getId())).withRel("Delete question"),
linkTo(methodOn(questionController.class).getQuestions()).withRel("Questions")
);
}
@PostMapping("/add")
public @ResponseBody EntityModel<Question> addQuestion(@RequestBody Question question) {
repo.save(question);
return EntityModel.of(
question,
linkTo(methodOn(questionController.class).getQuestion(question.getId())).withSelfRel(),
linkTo(methodOn(questionController.class).updateQuestion(question.getId(), null)).withRel("Update question"),
linkTo(methodOn(questionController.class).deleteQuestion(question.getId())).withRel("Delete question"),
linkTo(methodOn(questionController.class).getQuestions()).withRel("Questions")
);
}
@PutMapping("/update/{id}")
public @ResponseBody EntityModel<Question> updateQuestion(@PathVariable Long id,
@RequestBody Question question) {
Question q = repo.findQuestionById(id);
if (q == null) throw new ResourceNotFound(HttpStatus.NOT_FOUND, "Question not found");
if (question.getContent() != null && !question.getContent().isEmpty()) {
q.setContent(question.getContent());
}
if (question.getAnswerA() != null && !question.getAnswerA().isEmpty()) {
q.setAnswerA(question.getAnswerA());
}
if (question.getAnswerB() != null && !question.getAnswerB().isEmpty()) {
q.setAnswerB(question.getAnswerB());
}
if (question.getAnswerC() != null && !question.getAnswerC().isEmpty()) {
q.setAnswerC(question.getAnswerC());
}
if (question.getAnswerD() != null && !question.getAnswerD().isEmpty()) {
q.setAnswerD(question.getAnswerD());
}
if (question.getcAnswer() != null && !question.getcAnswer().isEmpty()) {
q.setcAnswer(question.getcAnswer());
}
repo.save(q);
return EntityModel.of(
q,
linkTo(methodOn(questionController.class).getQuestion(id)).withSelfRel(),
linkTo(methodOn(questionController.class).deleteQuestion(id)).withRel("Delete question"),
linkTo(methodOn(questionController.class).getQuestions()).withRel("Questions")
);
}
@DeleteMapping("delete/{id}")
public @ResponseBody EntityModel<Question> deleteQuestion(@PathVariable Long id) {
Question q = repo.findQuestionById(id);
if (q == null) throw new ResourceNotFound(HttpStatus.NOT_FOUND, "Question not found");
repo.delete(q);
return EntityModel.of(
q,
linkTo(methodOn(questionController.class).getQuestions()).withRel("Questions")
);
}
}

@ -0,0 +1,10 @@
package com.example.wtf.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
public class ResourceNotFound extends ResponseStatusException {
public ResourceNotFound(HttpStatus error, String message) {
super(error, message);
}
}

@ -0,0 +1,87 @@
package com.example.wtf.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Question {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
private String content;
private String answerA;
private String answerB;
private String answerC;
private String answerD;
private String cAnswer;
public Question() {}
public Question(String content, String answerB, String answerA, String answerC, String answerD, String cAnswer) {
this.content = content;
this.answerB = answerB;
this.answerA = answerA;
this.answerC = answerC;
this.answerD = answerD;
this.cAnswer = cAnswer;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAnswerA() {
return answerA;
}
public void setAnswerA(String answerA) {
this.answerA = answerA;
}
public String getAnswerB() {
return answerB;
}
public void setAnswerB(String answerB) {
this.answerB = answerB;
}
public String getAnswerC() {
return answerC;
}
public void setAnswerC(String answerC) {
this.answerC = answerC;
}
public String getAnswerD() {
return answerD;
}
public void setAnswerD(String answerD) {
this.answerD = answerD;
}
public String getcAnswer() {
return cAnswer;
}
public void setcAnswer(String cAnswer) {
this.cAnswer = cAnswer;
}
}

@ -0,0 +1,10 @@
package com.example.wtf.repository;
import com.example.wtf.model.Question;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface questionRepository extends CrudRepository<Question, Long> {
Question findQuestionById(Long id);
}

@ -0,0 +1 @@
spring.application.name=wtf

@ -1,10 +1,10 @@
package com.example.wfwebapi;
package com.example.wtf;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WfWebapiApplicationTests {
class WtfApplicationTests {
@Test
void contextLoads() {
Loading…
Cancel
Save