Compare commits
6 Commits
TommyVersi
...
master
Author | SHA1 | Date |
---|---|---|
|
8c79d986d5 | 2 weeks ago |
|
cc543d1af3 | 2 weeks ago |
![]() |
7f96ab14a4 | 3 weeks ago |
|
ac30d228ca | 3 weeks ago |
|
2fb0914cca | 3 weeks ago |
|
24e80f96f9 | 3 weeks ago |
@ -0,0 +1,14 @@
|
||||
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,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/WF-WEBAPI/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/WF-WEB-API.iml" filepath="$PROJECT_DIR$/.idea/WF-WEB-API.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
# É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"]
|
@ -0,0 +1,128 @@
|
||||
<?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>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<server.port>6969</server.port>
|
||||
<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>
|
||||
<!-- JUnit 5 (Jupiter) -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.0</version> <!-- Tu peux adapter la version -->
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Mockito pour les mocks -->
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Mockito + JUnit 5 integration -->
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Test (utile pour les tests d'intégration ou avec contexte Spring) -->
|
||||
<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-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.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
<delimiters>
|
||||
<delimiter>@</delimiter>
|
||||
</delimiters>
|
||||
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,11 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.example.wfwebapi.assembler;
|
||||
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
import com.example.wfwebapi.controller.QuoteController;
|
||||
import com.example.wfwebapi.model.Quote;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.server.RepresentationModelAssembler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class QuoteModelAssembler implements RepresentationModelAssembler<Quote, EntityModel<Quote>> {
|
||||
@Override
|
||||
public EntityModel<Quote> toModel(Quote quote) {
|
||||
return EntityModel.of(quote,
|
||||
linkTo(methodOn(QuoteController.class).getQuoteById(quote.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuoteController.class).getAllQuotes(0, 10)).withRel("quotes"));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.example.wfwebapi.assembler;
|
||||
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
import com.example.wfwebapi.controller.UserController;
|
||||
import com.example.wfwebapi.model.User;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.hateoas.server.RepresentationModelAssembler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserModelAssembler implements RepresentationModelAssembler<User, EntityModel<User>> {
|
||||
@Override
|
||||
public EntityModel<User> toModel(User user) {
|
||||
return EntityModel.of(user,
|
||||
linkTo(methodOn(UserController.class).getUserById(user.getId())).withSelfRel(),
|
||||
linkTo(methodOn(UserController.class).getAllUsers(0, 5)).withRel("users"));
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.example.wfwebapi.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
// Désactive CSRF
|
||||
.csrf(csrf -> csrf.disable())
|
||||
// Désactive la protection des frames (utile pour la console H2)
|
||||
.headers(headers -> headers.frameOptions().disable())
|
||||
// Autorise toutes les requêtes sans authentification
|
||||
.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll());
|
||||
return http.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Admin;
|
||||
import com.example.wfwebapi.repository.AdminRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/admin")
|
||||
public class AdminController {
|
||||
|
||||
@Autowired
|
||||
private AdminRepository adminRepository;
|
||||
|
||||
@GetMapping("/{userId}")
|
||||
public Admin getAdmin(@PathVariable Long userId) {
|
||||
return adminRepository.findById(userId)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Admin non trouvé pour l'utilisateur : " + userId));
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.User;
|
||||
import com.example.wfwebapi.repository.UserRepository;
|
||||
import com.example.wfwebapi.security.JwtTokenProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private JwtTokenProvider tokenProvider;
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<?> login(@RequestBody AuthRequest authRequest) {
|
||||
User user = userRepository.findByUsername(authRequest.getUsername())
|
||||
.orElseThrow(() -> new RuntimeException("Utilisateur non trouvé"));
|
||||
|
||||
if (!user.getPassword().equals(authRequest.getPassword())) {
|
||||
return ResponseEntity.status(401).body("Mot de passe invalide");
|
||||
}
|
||||
|
||||
String token = tokenProvider.createToken(user.getUsername());
|
||||
return ResponseEntity.ok(new AuthResponse(token));
|
||||
}
|
||||
|
||||
public static class AuthRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
public String getUsername() { return username; }
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
public String getPassword() { return password; }
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
}
|
||||
|
||||
public static class AuthResponse {
|
||||
private String token;
|
||||
public AuthResponse(String token) { this.token = token; }
|
||||
public String getToken() { return token; }
|
||||
public void setToken(String token) { this.token = token; }
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Caracter;
|
||||
import com.example.wfwebapi.repository.CaracterRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/caracter")
|
||||
public class CaracterController {
|
||||
|
||||
@Autowired
|
||||
private CaracterRepository caracterRepository;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Caracter getCaracterById(@PathVariable Long id) {
|
||||
return caracterRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Caracter non trouvé : " + id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Caracter> getCaracters() {
|
||||
return caracterRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Caracter createCaracter(@RequestBody Caracter caracter) {
|
||||
return caracterRepository.save(caracter);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Caracter updateCaracter(@RequestBody Caracter updatedCaracter) {
|
||||
return caracterRepository.findById(updatedCaracter.getId())
|
||||
.map(c -> {
|
||||
c.setCaracter(updatedCaracter.getCaracter());
|
||||
c.setImage(updatedCaracter.getImage());
|
||||
return caracterRepository.save(c);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Caracter non trouvé : " + updatedCaracter.getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteCaracter(@RequestParam Long id) {
|
||||
if (!caracterRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Caracter non trouvé : " + id);
|
||||
caracterRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import java.util.List;
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Commentary;
|
||||
import com.example.wfwebapi.repository.CommentaryRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/commentary")
|
||||
public class CommentaryController {
|
||||
|
||||
@Autowired
|
||||
private CommentaryRepository commentaryRepository;
|
||||
|
||||
@GetMapping("/{quoteId}")
|
||||
public List<Commentary> getCommentariesByQuote(@PathVariable Long quoteId) {
|
||||
return commentaryRepository.findByQuote_Id(quoteId);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Commentary addCommentary(@RequestParam Long quote, @RequestBody Commentary commentary) {
|
||||
commentary.setQuote(new com.example.wfwebapi.model.Quote() {{ setId(quote); }});
|
||||
return commentaryRepository.save(commentary);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteCommentariesByQuote(@RequestParam Long id) {
|
||||
List<Commentary> comments = commentaryRepository.findByQuote_Id(id);
|
||||
if(comments.isEmpty())
|
||||
throw new ResourceNotFoundException("Aucun commentaire trouvé pour la citation " + id);
|
||||
commentaryRepository.deleteAll(comments);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.DailyQuote;
|
||||
import com.example.wfwebapi.repository.DailyQuoteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/dailyquote")
|
||||
public class DailyQuoteController {
|
||||
|
||||
@Autowired
|
||||
private DailyQuoteRepository dailyQuoteRepository;
|
||||
|
||||
@GetMapping
|
||||
public DailyQuote getDailyQuote() {
|
||||
return dailyQuoteRepository.findAll().stream().findFirst()
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Aucune DailyQuote définie"));
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import java.util.List;
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Favorite;
|
||||
import com.example.wfwebapi.model.FavoriteId;
|
||||
import com.example.wfwebapi.model.User;
|
||||
import com.example.wfwebapi.model.Quote;
|
||||
import com.example.wfwebapi.repository.FavoriteRepository;
|
||||
import com.example.wfwebapi.repository.UserRepository;
|
||||
import com.example.wfwebapi.repository.QuoteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/favorite")
|
||||
public class FavoriteController {
|
||||
|
||||
@Autowired
|
||||
private FavoriteRepository favoriteRepository;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private QuoteRepository quoteRepository;
|
||||
|
||||
@GetMapping("/{userId}")
|
||||
public List<Favorite> getFavoritesByUser(@PathVariable Long userId) {
|
||||
return favoriteRepository.findByUser_Id(userId);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Favorite addFavorite(@RequestParam Long user, @RequestParam Long quote) {
|
||||
User u = userRepository.findById(user)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Utilisateur non trouvé : " + user));
|
||||
Quote q = quoteRepository.findById(quote)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Citation non trouvée : " + quote));
|
||||
|
||||
Favorite favorite = new Favorite();
|
||||
favorite.setId(new FavoriteId(user, quote));
|
||||
favorite.setUser(u);
|
||||
favorite.setQuote(q);
|
||||
return favoriteRepository.save(favorite);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteFavorite(@RequestParam Long user, @RequestParam Long quote) {
|
||||
FavoriteId id = new FavoriteId(user, quote);
|
||||
if (!favoriteRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Favorite non trouvée");
|
||||
favoriteRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Image;
|
||||
import com.example.wfwebapi.repository.ImageRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/image")
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ImageRepository imageRepository;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Image getImageById(@PathVariable Long id) {
|
||||
return imageRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Image non trouvée : " + id));
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<Image> getAllImages() {
|
||||
return imageRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Image createImage(@RequestBody Image image) {
|
||||
return imageRepository.save(image);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Image updateImage(@RequestBody Image updatedImage) {
|
||||
return imageRepository.findById(updatedImage.getId())
|
||||
.map(img -> {
|
||||
img.setImgPath(updatedImage.getImgPath());
|
||||
return imageRepository.save(img);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Image non trouvée : " + updatedImage.getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteImage(@RequestParam Long id) {
|
||||
if (!imageRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Image non trouvée : " + id);
|
||||
imageRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Question;
|
||||
import com.example.wfwebapi.repository.QuestionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/question")
|
||||
public class QuestionController {
|
||||
|
||||
@Autowired
|
||||
private QuestionRepository questionRepository;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Question getQuestionById(@PathVariable Long id) {
|
||||
return questionRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Question non trouvée : " + id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Question createQuestion(@RequestBody Question question) {
|
||||
return questionRepository.save(question);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Question updateQuestion(@RequestBody Question updatedQuestion) {
|
||||
return questionRepository.findById(updatedQuestion.getId())
|
||||
.map(q -> {
|
||||
q.setTexte(updatedQuestion.getTexte());
|
||||
q.setAnswerA(updatedQuestion.getAnswerA());
|
||||
q.setAnswerB(updatedQuestion.getAnswerB());
|
||||
q.setAnswerC(updatedQuestion.getAnswerC());
|
||||
q.setAnswerD(updatedQuestion.getAnswerD());
|
||||
q.setCAnswer(updatedQuestion.getCAnswer());
|
||||
return questionRepository.save(q);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Question non trouvée : " + updatedQuestion.getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteQuestion(@RequestParam Long id) {
|
||||
if (!questionRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Question non trouvée : " + id);
|
||||
questionRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Quiz;
|
||||
import com.example.wfwebapi.repository.QuizRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/quiz")
|
||||
public class QuizController {
|
||||
|
||||
@Autowired
|
||||
private QuizRepository quizRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<Quiz> getAllQuiz() {
|
||||
return quizRepository.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Quiz getQuizById(@PathVariable Long id) {
|
||||
return quizRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Quiz non trouvé : " + id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Quiz createQuiz(@RequestBody Quiz quiz) {
|
||||
return quizRepository.save(quiz);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Quiz updateQuiz(@RequestBody Quiz updatedQuiz) {
|
||||
return quizRepository.findById(updatedQuiz.getId())
|
||||
.map(q -> {
|
||||
q.setTitle(updatedQuiz.getTitle());
|
||||
q.setImage(updatedQuiz.getImage());
|
||||
q.setNbQuest(updatedQuiz.getNbQuest()); // ✅ champ maintenant mis à jour
|
||||
return quizRepository.save(q);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Quiz non trouvé : " + updatedQuiz.getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteQuiz(@RequestParam Long id) {
|
||||
if (!quizRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Quiz non trouvé : " + id);
|
||||
quizRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.example.wfwebapi.assembler.QuoteModelAssembler;
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Quote;
|
||||
import com.example.wfwebapi.repository.QuoteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/quote")
|
||||
public class QuoteController {
|
||||
|
||||
@Autowired
|
||||
private QuoteRepository quoteRepository;
|
||||
|
||||
@Autowired
|
||||
private QuoteModelAssembler assembler;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public EntityModel<Quote> getQuoteById(@PathVariable Long id) {
|
||||
Quote quote = quoteRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Citation non trouvée : " + id));
|
||||
return assembler.toModel(quote);
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public CollectionModel<EntityModel<Quote>> getAllQuotes(@RequestParam int index, @RequestParam int count) {
|
||||
List<EntityModel<Quote>> quotes = quoteRepository.findAll(PageRequest.of(index, count))
|
||||
.stream()
|
||||
.map(assembler::toModel)
|
||||
.collect(Collectors.toList());
|
||||
return CollectionModel.of(quotes);
|
||||
}
|
||||
|
||||
@GetMapping("/dailyquote")
|
||||
public EntityModel<Quote> getDailyQuote(@RequestParam int year, @RequestParam int month,
|
||||
@RequestParam int day, @RequestParam String lang) {
|
||||
Quote quote = quoteRepository.findAll().stream()
|
||||
.filter(q -> q.getLangue().equalsIgnoreCase(lang))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Aucune citation trouvée pour la langue " + lang));
|
||||
return assembler.toModel(quote);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public EntityModel<Quote> createQuote(@RequestBody Quote newQuote) {
|
||||
Quote saved = quoteRepository.save(newQuote);
|
||||
return assembler.toModel(saved);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public EntityModel<Quote> updateQuote(@RequestBody Quote updatedQuote) {
|
||||
Quote quote = quoteRepository.findById(updatedQuote.getId())
|
||||
.map(q -> {
|
||||
q.setContent(updatedQuote.getContent());
|
||||
q.setLangue(updatedQuote.getLangue());
|
||||
q.setLikes(updatedQuote.getLikes());
|
||||
q.setIsValide(updatedQuote.getIsValide());
|
||||
q.setReason(updatedQuote.getReason());
|
||||
q.setCaracter(updatedQuote.getCaracter());
|
||||
q.setSource(updatedQuote.getSource());
|
||||
q.setUserVerif(updatedQuote.getUserVerif());
|
||||
return quoteRepository.save(q);
|
||||
})
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Citation non trouvée : " + updatedQuote.getId()));
|
||||
return assembler.toModel(quote);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
public void deleteQuote(@RequestParam Long idQuote) {
|
||||
if (!quoteRepository.existsById(idQuote)) {
|
||||
throw new ResourceNotFoundException("Citation non trouvée : " + idQuote);
|
||||
}
|
||||
quoteRepository.deleteById(idQuote);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.RecordQuiz;
|
||||
import com.example.wfwebapi.model.RecordQuizId;
|
||||
import com.example.wfwebapi.repository.RecordQuizRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/recordquiz")
|
||||
public class RecordQuizController {
|
||||
|
||||
@Autowired
|
||||
private RecordQuizRepository recordQuizRepository;
|
||||
|
||||
@GetMapping
|
||||
public RecordQuiz getRecord(@RequestParam Long user, @RequestParam Long quiz) {
|
||||
RecordQuizId id = new RecordQuizId(user, quiz);
|
||||
return recordQuizRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("RecordQuiz non trouvé"));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public RecordQuiz createRecord(@RequestBody RecordQuiz recordQuiz) {
|
||||
return recordQuizRepository.save(recordQuiz);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public RecordQuiz updateRecord(@RequestBody RecordQuiz recordQuiz) {
|
||||
RecordQuizId id = recordQuiz.getId();
|
||||
if (!recordQuizRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("RecordQuiz non trouvé");
|
||||
return recordQuizRepository.save(recordQuiz);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteRecord(@RequestParam Long user, @RequestParam Long quiz) {
|
||||
RecordQuizId id = new RecordQuizId(user, quiz);
|
||||
if (!recordQuizRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("RecordQuiz non trouvé");
|
||||
recordQuizRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Source;
|
||||
import com.example.wfwebapi.repository.SourceRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/source")
|
||||
public class SourceController {
|
||||
|
||||
@Autowired
|
||||
private SourceRepository sourceRepository;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Source getSourceById(@PathVariable Long id) {
|
||||
return sourceRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Source non trouvée : " + id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Source createSource(@RequestBody Source source) {
|
||||
return sourceRepository.save(source);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Source updateSource(@RequestBody Source updatedSource) {
|
||||
return sourceRepository.findById(updatedSource.getId())
|
||||
.map(s -> {
|
||||
s.setTitle(updatedSource.getTitle());
|
||||
s.setDateS(updatedSource.getDateS());
|
||||
return sourceRepository.save(s);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Source non trouvée : " + updatedSource.getId()));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public void deleteSource(@RequestParam Long id) {
|
||||
if (!sourceRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Source non trouvée : " + id);
|
||||
sourceRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.example.wfwebapi.assembler.UserModelAssembler;
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.User;
|
||||
import com.example.wfwebapi.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private UserModelAssembler assembler;
|
||||
|
||||
// GET /api/v1/users/{id}
|
||||
@GetMapping("/{id}")
|
||||
public EntityModel<User> getUserById(@PathVariable Long id) {
|
||||
User user = userRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Utilisateur non trouvé : " + id));
|
||||
return assembler.toModel(user);
|
||||
}
|
||||
|
||||
// GET /api/v1/users/all?index=0&count=5
|
||||
@GetMapping("/all")
|
||||
public CollectionModel<EntityModel<User>> getAllUsers(@RequestParam int index, @RequestParam int count) {
|
||||
List<EntityModel<User>> users = userRepository.findAll(PageRequest.of(index, count))
|
||||
.stream().map(assembler::toModel)
|
||||
.collect(Collectors.toList());
|
||||
return CollectionModel.of(users);
|
||||
}
|
||||
|
||||
// POST /api/v1/users
|
||||
@PostMapping
|
||||
public EntityModel<User> createUser(@RequestBody User newUser) {
|
||||
User user = userRepository.save(newUser);
|
||||
return assembler.toModel(user);
|
||||
}
|
||||
|
||||
// PUT /api/v1/users?id=1
|
||||
@PutMapping
|
||||
public EntityModel<User> updateUser(@RequestParam Long id, @RequestBody User updatedUser) {
|
||||
User user = userRepository.findById(id)
|
||||
.map(u -> {
|
||||
u.setUsername(updatedUser.getUsername());
|
||||
u.setEmail(updatedUser.getEmail());
|
||||
u.setPassword(updatedUser.getPassword());
|
||||
u.setImage(updatedUser.getImage());
|
||||
u.setCreation(updatedUser.getCreation());
|
||||
return userRepository.save(u);
|
||||
}).orElseThrow(() -> new ResourceNotFoundException("Utilisateur non trouvé : " + id));
|
||||
return assembler.toModel(user);
|
||||
}
|
||||
|
||||
// DELETE /api/v1/users?id=1
|
||||
@DeleteMapping
|
||||
public void deleteUser(@RequestParam Long id) {
|
||||
if (!userRepository.existsById(id))
|
||||
throw new ResourceNotFoundException("Utilisateur non trouvé : " + id);
|
||||
userRepository.deleteById(id);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.example.wtf.exception;
|
||||
package com.example.wfwebapi.exception;
|
||||
|
||||
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.exception;
|
||||
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
public ResourceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.exception;
|
||||
|
||||
public class UnauthorizedException extends RuntimeException {
|
||||
public UnauthorizedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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());
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
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; }
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
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());
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
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,49 +1,43 @@
|
||||
package com.example.wtf.model;
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "app_user")
|
||||
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
@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(cascade = CascadeType.ALL)
|
||||
@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; }
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Admin;
|
||||
|
||||
public interface AdminRepository extends JpaRepository<Admin, Long> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Caracter;
|
||||
|
||||
public interface CaracterRepository extends JpaRepository<Caracter, Long> {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Commentary;
|
||||
|
||||
public interface CommentaryRepository extends JpaRepository<Commentary, Long> {
|
||||
List<Commentary> findByQuote_Id(Long quoteId);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.DailyQuote;
|
||||
|
||||
public interface DailyQuoteRepository extends JpaRepository<DailyQuote, Long> {
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Favorite;
|
||||
import com.example.wfwebapi.model.FavoriteId;
|
||||
|
||||
public interface FavoriteRepository extends JpaRepository<Favorite, FavoriteId> {
|
||||
List<Favorite> findByUser_Id(Long userId);
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Image;
|
||||
|
||||
public interface ImageRepository extends JpaRepository<Image, Long> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Question;
|
||||
|
||||
public interface QuestionRepository extends JpaRepository<Question, Long> {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.QuizQuestion;
|
||||
import com.example.wfwebapi.model.QuizQuestionId;
|
||||
|
||||
public interface QuizQuestionRepository extends JpaRepository<QuizQuestion, QuizQuestionId> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Quiz;
|
||||
|
||||
public interface QuizRepository extends JpaRepository<Quiz, Long> {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Quote;
|
||||
|
||||
public interface QuoteRepository extends JpaRepository<Quote, Long> {
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.RecordQuiz;
|
||||
import com.example.wfwebapi.model.RecordQuizId;
|
||||
|
||||
public interface RecordQuizRepository extends JpaRepository<RecordQuiz, RecordQuizId> {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.Source;
|
||||
|
||||
public interface SourceRepository extends JpaRepository<Source, Long> {
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.wfwebapi.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import com.example.wfwebapi.model.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
Optional<User> findByUsername(String username);
|
||||
Optional<User> findByEmail(String email);
|
||||
boolean existsByUsername(String username);
|
||||
boolean existsByEmail(String email);
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.example.wfwebapi.security;
|
||||
|
||||
import com.example.wfwebapi.exception.UnauthorizedException;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
|
||||
private final JwtTokenProvider tokenProvider;
|
||||
|
||||
public JwtAuthenticationFilter(JwtTokenProvider tokenProvider) {
|
||||
this.tokenProvider = tokenProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.startsWith("Bearer ")) {
|
||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token manquant ou invalide");
|
||||
return;
|
||||
}
|
||||
|
||||
String token = header.substring(7);
|
||||
if (!tokenProvider.validateToken(token)) {
|
||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token invalide ou expiré");
|
||||
return;
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.wfwebapi.security;
|
||||
|
||||
import io.jsonwebtoken.*;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import java.security.Key;
|
||||
import java.util.Date;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class JwtTokenProvider {
|
||||
|
||||
// Clé secrète (à stocker en sécurité dans une variable d'environnement en production)
|
||||
private final Key secretKey = Keys.hmacShaKeyFor("MaCléSuperSecrètePourJWTQueJeDoisChanger".getBytes());
|
||||
// Durée de validité du token (par exemple 1h)
|
||||
private final long validityInMilliseconds = 3600000;
|
||||
|
||||
// Création du token
|
||||
public String createToken(String username) {
|
||||
Claims claims = Jwts.claims().setSubject(username);
|
||||
Date now = new Date();
|
||||
Date validity = new Date(now.getTime() + validityInMilliseconds);
|
||||
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
.setIssuedAt(now)
|
||||
.setExpiration(validity)
|
||||
.signWith(secretKey, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
|
||||
// Validation du token
|
||||
public boolean validateToken(String token) {
|
||||
try {
|
||||
Jws<Claims> claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token);
|
||||
return !claims.getBody().getExpiration().before(new Date());
|
||||
} catch (JwtException | IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Récupération du nom d'utilisateur depuis le token
|
||||
public String getUsername(String token) {
|
||||
return Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody().getSubject();
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
spring.application.name=WF-WEB-API
|
@ -0,0 +1,144 @@
|
||||
-- Suppression des tables (si elles existent)
|
||||
DROP TABLE IF EXISTS commentary;
|
||||
DROP TABLE IF EXISTS favorite;
|
||||
DROP TABLE IF EXISTS daily_quote;
|
||||
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
|
||||
|
||||
-- Table image (l'id est auto-incrémenté)
|
||||
CREATE TABLE image (
|
||||
id_img INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
img_path VARCHAR(300) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
-- Table users (l'id est auto-incrémenté)
|
||||
CREATE TABLE users (
|
||||
id_user INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL,
|
||||
email VARCHAR(50) NOT NULL,
|
||||
password VARCHAR(100) NOT NULL,
|
||||
img INTEGER NOT NULL,
|
||||
creation DATE NOT NULL,
|
||||
CONSTRAINT unique_col UNIQUE (email),
|
||||
CONSTRAINT fk_users_img FOREIGN KEY (img) REFERENCES image(id_img)
|
||||
);
|
||||
|
||||
-- Table admin
|
||||
-- La colonne 'users' doit contenir l'id d'un utilisateur existant (pas auto-incrémenté ici)
|
||||
CREATE TABLE admin (
|
||||
users INTEGER PRIMARY KEY,
|
||||
CONSTRAINT fk_admin_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||
);
|
||||
|
||||
-- Table question (l'id est auto-incrémenté)
|
||||
CREATE TABLE question (
|
||||
id_question INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
texte TEXT NOT NULL UNIQUE,
|
||||
answer_a VARCHAR(30) NOT NULL,
|
||||
answer_b VARCHAR(30) NOT NULL,
|
||||
answer_c VARCHAR(30) NOT NULL,
|
||||
answer_d VARCHAR(30) NOT NULL,
|
||||
c_answer VARCHAR(30) NOT NULL,
|
||||
CONSTRAINT check_c_answer CHECK (c_answer = answer_a OR c_answer = answer_b OR c_answer = answer_c OR c_answer = answer_d)
|
||||
);
|
||||
|
||||
-- Table quiz (l'id est auto-incrémenté)
|
||||
CREATE TABLE quiz (
|
||||
id_quiz INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
title VARCHAR(40) NOT NULL,
|
||||
img INTEGER NOT NULL,
|
||||
nb_quest NUMERIC DEFAULT 0,
|
||||
CONSTRAINT fk_quiz_img FOREIGN KEY (img) REFERENCES image(id_img)
|
||||
);
|
||||
|
||||
-- Table quiz_question
|
||||
-- Les colonnes ici sont des clés étrangères et doivent être de type INTEGER (les valeurs proviennent des tables quiz et question)
|
||||
CREATE TABLE quiz_question (
|
||||
quiz INTEGER NOT NULL,
|
||||
question INTEGER NOT NULL,
|
||||
PRIMARY KEY (quiz, question),
|
||||
CONSTRAINT fk_quiz_question_quiz FOREIGN KEY (quiz) REFERENCES quiz(id_quiz),
|
||||
CONSTRAINT fk_quiz_question_question FOREIGN KEY (question) REFERENCES question(id_question)
|
||||
);
|
||||
|
||||
-- Table record_quiz
|
||||
CREATE TABLE record_quiz (
|
||||
users INTEGER NOT NULL,
|
||||
quiz INTEGER NOT NULL,
|
||||
nb_point NUMERIC DEFAULT 0,
|
||||
time_q NUMERIC DEFAULT 0,
|
||||
PRIMARY KEY (users, quiz),
|
||||
CONSTRAINT fk_recordquiz_user FOREIGN KEY (users) REFERENCES users(id_user),
|
||||
CONSTRAINT fk_recordquiz_quiz FOREIGN KEY (quiz) REFERENCES quiz(id_quiz),
|
||||
CONSTRAINT err_nb_point CHECK (nb_point >= 0),
|
||||
CONSTRAINT err_time_q CHECK (time_q >= 0)
|
||||
);
|
||||
|
||||
-- Table source (l'id est auto-incrémenté)
|
||||
CREATE TABLE source (
|
||||
id_source INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
title VARCHAR(100) NOT NULL,
|
||||
date_s NUMERIC(4) NOT NULL
|
||||
);
|
||||
|
||||
-- Table caracter (l'id est auto-incrémenté)
|
||||
CREATE TABLE caracter (
|
||||
id_caracter INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
caracter VARCHAR(100) NOT NULL,
|
||||
id_img INTEGER NOT NULL,
|
||||
CONSTRAINT fk_caracter_image FOREIGN KEY (id_img) REFERENCES image(id_img)
|
||||
);
|
||||
|
||||
-- Table quote (l'id est auto-incrémenté)
|
||||
CREATE TABLE quote (
|
||||
id_quote INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
content TEXT NOT NULL,
|
||||
likes NUMERIC DEFAULT 0,
|
||||
langue CHAR(2) NOT NULL,
|
||||
is_valide BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
reason VARCHAR(100) NOT NULL,
|
||||
id_caracter INTEGER NOT NULL,
|
||||
id_source INTEGER NOT NULL,
|
||||
id_user_verif INTEGER NOT NULL,
|
||||
CONSTRAINT fk_quote_caracter FOREIGN KEY (id_caracter) REFERENCES caracter(id_caracter),
|
||||
CONSTRAINT fk_quote_source FOREIGN KEY (id_source) REFERENCES source(id_source),
|
||||
CONSTRAINT fk_quote_userverif FOREIGN KEY (id_user_verif) REFERENCES users(id_user),
|
||||
CONSTRAINT err_nb_like CHECK (likes >= 0),
|
||||
CONSTRAINT err_language CHECK (langue = 'fr' OR langue = 'en')
|
||||
);
|
||||
|
||||
-- Table daily_quote
|
||||
CREATE TABLE daily_quote (
|
||||
citation_id INTEGER PRIMARY KEY,
|
||||
FOREIGN KEY (citation_id) REFERENCES quote(id_quote) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Table favorite
|
||||
CREATE TABLE favorite (
|
||||
users INTEGER NOT NULL,
|
||||
quote INTEGER NOT NULL,
|
||||
PRIMARY KEY (users, quote),
|
||||
CONSTRAINT fk_favorite_quote FOREIGN KEY (quote) REFERENCES quote(id_quote),
|
||||
CONSTRAINT fk_favorite_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||
);
|
||||
|
||||
-- Table commentary (l'id est auto-incrémenté)
|
||||
CREATE TABLE commentary (
|
||||
id_comment INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
quote INTEGER NOT NULL,
|
||||
users INTEGER NOT NULL,
|
||||
date_c DATE NOT NULL,
|
||||
comment TEXT NOT NULL,
|
||||
CONSTRAINT fk_commentary_quote FOREIGN KEY (quote) REFERENCES quote(id_quote),
|
||||
CONSTRAINT fk_commentary_user FOREIGN KEY (users) REFERENCES users(id_user)
|
||||
);
|
@ -1,10 +1,10 @@
|
||||
package com.example.wtf;
|
||||
package com.example.wfwebapi;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class WtfApplicationTests {
|
||||
class WfWebapiApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
@ -0,0 +1,54 @@
|
||||
package com.example.wfwebapi.assembler;
|
||||
|
||||
import com.example.wfwebapi.controller.QuoteController;
|
||||
import com.example.wfwebapi.model.Quote;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
class QuoteModelAssemblerTest {
|
||||
|
||||
private QuoteModelAssembler assembler;
|
||||
private Quote quote;
|
||||
private EntityModel<Quote> model;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
assembler = new QuoteModelAssembler();
|
||||
quote = new Quote();
|
||||
quote.setId(1L);
|
||||
model = assembler.toModel(quote);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testModelContent() {
|
||||
assertEquals(quote, model.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasSelfLink() {
|
||||
assertTrue(model.getLinks().hasLink("self"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHasQuotesLink() {
|
||||
assertTrue(model.getLinks().hasLink("quotes"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSelfLinkHref() {
|
||||
String expectedHref = linkTo(methodOn(QuoteController.class).getQuoteById(1L)).withSelfRel().getHref();
|
||||
String actualHref = model.getRequiredLink("self").getHref();
|
||||
assertEquals(expectedHref, actualHref);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testQuotesLinkHref() {
|
||||
String expectedHref = linkTo(methodOn(QuoteController.class).getAllQuotes(0, 10)).withRel("quotes").getHref();
|
||||
String actualHref = model.getRequiredLink("quotes").getHref();
|
||||
assertEquals(expectedHref, actualHref);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.Admin;
|
||||
import com.example.wfwebapi.repository.AdminRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class AdminControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private AdminRepository adminRepository;
|
||||
|
||||
private String baseUrl() {
|
||||
return "http://localhost:" + port + "/api/v1/admin/";
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAdminFound() {
|
||||
Admin admin = new Admin();
|
||||
admin.setUserId(999L);
|
||||
adminRepository.save(admin);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<Admin> response = restTemplate.getForEntity(baseUrl() + "999", Admin.class);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
assertEquals(999L, response.getBody().getUserId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAdminNotFound() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
Exception exception = assertThrows(Exception.class, () ->
|
||||
restTemplate.getForEntity(baseUrl() + "123456", Admin.class)
|
||||
);
|
||||
|
||||
assertTrue(exception.getMessage().contains("404"));
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.Image;
|
||||
import com.example.wfwebapi.model.User;
|
||||
import com.example.wfwebapi.repository.ImageRepository;
|
||||
import com.example.wfwebapi.repository.UserRepository;
|
||||
import com.example.wfwebapi.security.JwtTokenProvider;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class AuthControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private AuthController authController;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private ImageRepository imageRepository;
|
||||
|
||||
@Autowired
|
||||
private JwtTokenProvider tokenProvider;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/auth" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
userRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
Image image = new Image();
|
||||
image.setImgPath("fake/path.png");
|
||||
image = imageRepository.save(image);
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("john");
|
||||
user.setPassword("password123");
|
||||
user.setEmail("john@example.com");
|
||||
user.setCreation(java.time.LocalDate.now());
|
||||
user.setImage(image);
|
||||
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
assertNotNull(authController);
|
||||
assertNotNull(userRepository);
|
||||
assertNotNull(tokenProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginSuccess() {
|
||||
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||
request.setUsername("john");
|
||||
request.setPassword("password123");
|
||||
|
||||
ResponseEntity<AuthController.AuthResponse> response = restTemplate.postForEntity(
|
||||
getUrl("/login"), request, AuthController.AuthResponse.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertNotNull(response.getBody().getToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginInvalidPassword() {
|
||||
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||
request.setUsername("john");
|
||||
request.setPassword("wrongpass");
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||
getUrl("/login"), request, String.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
|
||||
assertEquals("Mot de passe invalide", response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLoginUserNotFound() {
|
||||
AuthController.AuthRequest request = new AuthController.AuthRequest();
|
||||
request.setUsername("unknown");
|
||||
request.setPassword("any");
|
||||
|
||||
ResponseEntity<String> response = restTemplate.postForEntity(
|
||||
getUrl("/login"), request, String.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Utilisateur non trouvé"));
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.Caracter;
|
||||
import com.example.wfwebapi.model.Image;
|
||||
import com.example.wfwebapi.repository.CaracterRepository;
|
||||
import com.example.wfwebapi.repository.ImageRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class CaracterControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private CaracterRepository caracterRepository;
|
||||
|
||||
@Autowired
|
||||
private ImageRepository imageRepository;
|
||||
|
||||
private Image image;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/caracter" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
caracterRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
image = new Image();
|
||||
image.setImgPath("path/to/image.png");
|
||||
imageRepository.save(image);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAndGetCaracterById() {
|
||||
Caracter caracter = new Caracter();
|
||||
caracter.setCaracter("Déterminé");
|
||||
caracter.setImage(image);
|
||||
|
||||
ResponseEntity<Caracter> postResponse = restTemplate.postForEntity(getUrl(""), caracter, Caracter.class);
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
Long id = Objects.requireNonNull(postResponse.getBody()).getId();
|
||||
|
||||
ResponseEntity<Caracter> getResponse = restTemplate.getForEntity(getUrl("/" + id), Caracter.class);
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals("Déterminé", getResponse.getBody().getCaracter());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllCaracters() {
|
||||
Caracter c = new Caracter();
|
||||
c.setCaracter("Créatif");
|
||||
c.setImage(image);
|
||||
caracterRepository.save(c);
|
||||
|
||||
ResponseEntity<Caracter[]> response = restTemplate.getForEntity(getUrl("/"), Caracter[].class);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertTrue(response.getBody().length >= 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateCaracter() {
|
||||
Caracter c = new Caracter();
|
||||
c.setCaracter("Patient");
|
||||
c.setImage(image);
|
||||
c = caracterRepository.save(c);
|
||||
|
||||
c.setCaracter("Impulsif");
|
||||
|
||||
HttpEntity<Caracter> request = new HttpEntity<>(c);
|
||||
ResponseEntity<Caracter> response = restTemplate.exchange(getUrl(""), HttpMethod.PUT, request, Caracter.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals("Impulsif", response.getBody().getCaracter());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteCaracter() {
|
||||
Caracter c = new Caracter();
|
||||
c.setCaracter("Sérieux");
|
||||
c.setImage(image);
|
||||
c = caracterRepository.save(c);
|
||||
|
||||
ResponseEntity<Void> response = restTemplate.exchange(getUrl("?id=" + c.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertFalse(caracterRepository.existsById(c.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetCaracterNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/999999"), String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Caracter non trouvé"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteCaracterNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(getUrl("?id=999999"), HttpMethod.DELETE, null, String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Caracter non trouvé"));
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.*;
|
||||
import com.example.wfwebapi.repository.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class CommentaryControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired private CommentaryRepository commentaryRepository;
|
||||
@Autowired private QuoteRepository quoteRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
@Autowired private CaracterRepository caracterRepository;
|
||||
@Autowired private SourceRepository sourceRepository;
|
||||
|
||||
private Quote quote;
|
||||
private User user;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/commentary" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
clearDatabase();
|
||||
|
||||
Image image = new Image();
|
||||
image.setImgPath("test.png");
|
||||
imageRepository.save(image);
|
||||
|
||||
user = new User();
|
||||
user.setUsername("user");
|
||||
user.setEmail("user@mail.com");
|
||||
user.setPassword("pass");
|
||||
user.setCreation(LocalDate.now());
|
||||
user.setImage(image);
|
||||
userRepository.save(user);
|
||||
|
||||
Caracter caracter = new Caracter();
|
||||
caracter.setCaracter("Fort");
|
||||
caracter.setImage(image);
|
||||
caracterRepository.save(caracter);
|
||||
|
||||
Source source = new Source();
|
||||
source.setTitle("source");
|
||||
source.setDateS(2000);
|
||||
sourceRepository.save(source);
|
||||
|
||||
quote = new Quote();
|
||||
quote.setContent("Ceci est une citation.");
|
||||
quote.setLangue("fr");
|
||||
quote.setIsValide(true);
|
||||
quote.setLikes(0);
|
||||
quote.setReason("valid");
|
||||
quote.setCaracter(caracter);
|
||||
quote.setSource(source);
|
||||
quote.setUserVerif(user);
|
||||
quoteRepository.save(quote);
|
||||
}
|
||||
|
||||
void clearDatabase() {
|
||||
commentaryRepository.deleteAll();
|
||||
quoteRepository.deleteAll();
|
||||
caracterRepository.deleteAll();
|
||||
sourceRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddAndGetCommentary() {
|
||||
Commentary commentary = new Commentary();
|
||||
commentary.setUser(user);
|
||||
commentary.setComment("Super citation !");
|
||||
commentary.setDateC(LocalDate.now());
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
HttpEntity<Commentary> request = new HttpEntity<>(commentary, headers);
|
||||
|
||||
ResponseEntity<Commentary> postResponse = restTemplate.postForEntity(
|
||||
getUrl("?quote=" + quote.getId()), request, Commentary.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
assertNotNull(postResponse.getBody().getId());
|
||||
|
||||
ResponseEntity<Commentary[]> getResponse = restTemplate.getForEntity(
|
||||
getUrl("/" + quote.getId()), Commentary[].class);
|
||||
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals(1, getResponse.getBody().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteCommentary() {
|
||||
Commentary commentary = new Commentary();
|
||||
commentary.setUser(user);
|
||||
commentary.setQuote(quote);
|
||||
commentary.setComment("À supprimer");
|
||||
commentary.setDateC(LocalDate.now());
|
||||
commentaryRepository.save(commentary);
|
||||
|
||||
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||
getUrl("?id=" + quote.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||
|
||||
List<Commentary> remaining = commentaryRepository.findByQuote_Id(quote.getId());
|
||||
assertTrue(remaining.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteCommentaryNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
getUrl("?id=99999"), HttpMethod.DELETE, null, String.class);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Aucun commentaire trouvé"));
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.*;
|
||||
import com.example.wfwebapi.repository.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class DailyQuoteControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired private DailyQuoteRepository dailyQuoteRepository;
|
||||
@Autowired private QuoteRepository quoteRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
@Autowired private CaracterRepository caracterRepository;
|
||||
@Autowired private SourceRepository sourceRepository;
|
||||
|
||||
private Quote quote;
|
||||
|
||||
private String getUrl() {
|
||||
return "http://localhost:" + port + "/api/v1/dailyquote";
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
dailyQuoteRepository.deleteAll();
|
||||
quoteRepository.deleteAll();
|
||||
caracterRepository.deleteAll();
|
||||
sourceRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
Image image = new Image();
|
||||
image.setImgPath("daily.png");
|
||||
imageRepository.save(image);
|
||||
|
||||
User user = new User();
|
||||
user.setUsername("dailyUser");
|
||||
user.setPassword("pwd");
|
||||
user.setEmail("daily@mail.com");
|
||||
user.setCreation(LocalDate.now());
|
||||
user.setImage(image);
|
||||
userRepository.save(user);
|
||||
|
||||
Caracter caracter = new Caracter();
|
||||
caracter.setCaracter("Calme");
|
||||
caracter.setImage(image);
|
||||
caracterRepository.save(caracter);
|
||||
|
||||
Source source = new Source();
|
||||
source.setTitle("Livre A");
|
||||
source.setDateS(2001);
|
||||
sourceRepository.save(source);
|
||||
|
||||
quote = new Quote();
|
||||
quote.setContent("Chaque jour est un cadeau.");
|
||||
quote.setLangue("fr");
|
||||
quote.setIsValide(true);
|
||||
quote.setLikes(10);
|
||||
quote.setReason("approuvée");
|
||||
quote.setUserVerif(user);
|
||||
quote.setCaracter(caracter);
|
||||
quote.setSource(source);
|
||||
quoteRepository.save(quote);
|
||||
|
||||
DailyQuote dailyQuote = new DailyQuote();
|
||||
dailyQuote.setCitationId(quote.getId()); // CORRECT : ne pas faire setQuote()
|
||||
dailyQuoteRepository.save(dailyQuote);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDailyQuote() {
|
||||
ResponseEntity<DailyQuote> response = restTemplate.getForEntity(getUrl(), DailyQuote.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertEquals(quote.getId(), response.getBody().getCitationId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDailyQuoteNotFound() {
|
||||
dailyQuoteRepository.deleteAll();
|
||||
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(getUrl(), String.class);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Aucune DailyQuote définie"));
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.*;
|
||||
import com.example.wfwebapi.repository.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class FavoriteControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired private FavoriteRepository favoriteRepository;
|
||||
@Autowired private QuoteRepository quoteRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
@Autowired private CaracterRepository caracterRepository;
|
||||
@Autowired private SourceRepository sourceRepository;
|
||||
|
||||
private User user;
|
||||
private Quote quote;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/favorite" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
favoriteRepository.deleteAll();
|
||||
quoteRepository.deleteAll();
|
||||
caracterRepository.deleteAll();
|
||||
sourceRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
Image image = new Image();
|
||||
image.setImgPath("fav.png");
|
||||
imageRepository.save(image);
|
||||
|
||||
user = new User();
|
||||
user.setUsername("testuser");
|
||||
user.setPassword("secret");
|
||||
user.setEmail("test@ex.com");
|
||||
user.setCreation(LocalDate.now());
|
||||
user.setImage(image);
|
||||
userRepository.save(user);
|
||||
|
||||
Caracter caracter = new Caracter();
|
||||
caracter.setCaracter("Sérieux");
|
||||
caracter.setImage(image);
|
||||
caracterRepository.save(caracter);
|
||||
|
||||
Source source = new Source();
|
||||
source.setTitle("Source X");
|
||||
source.setDateS(1999);
|
||||
sourceRepository.save(source);
|
||||
|
||||
quote = new Quote();
|
||||
quote.setContent("La connaissance est le pouvoir.");
|
||||
quote.setLangue("fr");
|
||||
quote.setIsValide(true);
|
||||
quote.setLikes(100);
|
||||
quote.setReason("Validée");
|
||||
quote.setCaracter(caracter);
|
||||
quote.setSource(source);
|
||||
quote.setUserVerif(user);
|
||||
quoteRepository.save(quote);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddAndGetFavorite() {
|
||||
ResponseEntity<Void> postResponse = restTemplate.postForEntity(
|
||||
getUrl("?user=" + user.getId() + ""e=" + quote.getId()),
|
||||
null,
|
||||
Void.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
|
||||
ResponseEntity<Favorite[]> getResponse = restTemplate.getForEntity(
|
||||
getUrl("/" + user.getId()),
|
||||
Favorite[].class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals(1, getResponse.getBody().length);
|
||||
assertEquals(quote.getId(), getResponse.getBody()[0].getQuote().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteFavorite() {
|
||||
Favorite favorite = new Favorite();
|
||||
favorite.setId(new FavoriteId(user.getId(), quote.getId()));
|
||||
favorite.setUser(user);
|
||||
favorite.setQuote(quote);
|
||||
favoriteRepository.save(favorite);
|
||||
|
||||
ResponseEntity<Void> response = restTemplate.exchange(
|
||||
getUrl("?user=" + user.getId() + ""e=" + quote.getId()),
|
||||
HttpMethod.DELETE,
|
||||
null,
|
||||
Void.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertFalse(favoriteRepository.existsById(new FavoriteId(user.getId(), quote.getId())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteFavoriteNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
getUrl("?user=9999"e=9999"),
|
||||
HttpMethod.DELETE,
|
||||
null,
|
||||
String.class
|
||||
);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Favorite non trouvée"));
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.Image;
|
||||
import com.example.wfwebapi.repository.ImageRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class ImageControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/image" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
imageRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAndGetImage() {
|
||||
Image image = new Image();
|
||||
image.setImgPath("assets/img/test.png");
|
||||
|
||||
ResponseEntity<Image> postResponse = restTemplate.postForEntity(
|
||||
getUrl(""), image, Image.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
Image created = postResponse.getBody();
|
||||
assertNotNull(created);
|
||||
assertNotNull(created.getId());
|
||||
|
||||
ResponseEntity<Image> getResponse = restTemplate.getForEntity(
|
||||
getUrl("/" + created.getId()), Image.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals("assets/img/test.png", getResponse.getBody().getImgPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateImage() {
|
||||
Image image = new Image();
|
||||
image.setImgPath("original.png");
|
||||
image = imageRepository.save(image);
|
||||
|
||||
image.setImgPath("updated.png");
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Image> entity = new HttpEntity<>(image, headers);
|
||||
|
||||
ResponseEntity<Image> putResponse = restTemplate.exchange(
|
||||
getUrl(""), HttpMethod.PUT, entity, Image.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, putResponse.getStatusCode());
|
||||
assertEquals("updated.png", putResponse.getBody().getImgPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteImage() {
|
||||
Image image = new Image();
|
||||
image.setImgPath("todelete.png");
|
||||
image = imageRepository.save(image);
|
||||
|
||||
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||
getUrl("?id=" + image.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||
assertFalse(imageRepository.existsById(image.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetImageNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||
getUrl("/9999"), String.class);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Image non trouvée"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteImageNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Image non trouvée"));
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.Question;
|
||||
import com.example.wfwebapi.repository.QuestionRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class QuestionControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired private QuestionRepository questionRepository;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/question" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
questionRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAndGetQuestion() {
|
||||
Question question = new Question();
|
||||
question.setTexte("Quel est le langage de ce test ?");
|
||||
question.setAnswerA("Python");
|
||||
question.setAnswerB("Java");
|
||||
question.setAnswerC("C++");
|
||||
question.setAnswerD("JavaScript");
|
||||
question.setCAnswer("Java");
|
||||
|
||||
ResponseEntity<Question> postResponse = restTemplate.postForEntity(getUrl(""), question, Question.class);
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
assertNotNull(postResponse.getBody());
|
||||
Long id = postResponse.getBody().getId();
|
||||
assertNotNull(id);
|
||||
|
||||
ResponseEntity<Question> getResponse = restTemplate.getForEntity(getUrl("/" + id), Question.class);
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals("Java", getResponse.getBody().getCAnswer());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateQuestion() {
|
||||
Question q = new Question();
|
||||
q.setTexte("Old Q");
|
||||
q.setAnswerA("A");
|
||||
q.setAnswerB("B");
|
||||
q.setAnswerC("C");
|
||||
q.setAnswerD("D");
|
||||
q.setCAnswer("B");
|
||||
q = questionRepository.save(q);
|
||||
|
||||
q.setTexte("Updated Q");
|
||||
q.setCAnswer("A");
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Question> entity = new HttpEntity<>(q, headers);
|
||||
|
||||
ResponseEntity<Question> response = restTemplate.exchange(
|
||||
getUrl(""), HttpMethod.PUT, entity, Question.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals("Updated Q", response.getBody().getTexte());
|
||||
assertEquals("A", response.getBody().getCAnswer());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteQuestion() {
|
||||
Question q = new Question();
|
||||
q.setTexte("À supprimer");
|
||||
q.setAnswerA("1");
|
||||
q.setAnswerB("2");
|
||||
q.setAnswerC("3");
|
||||
q.setAnswerD("4");
|
||||
q.setCAnswer("2");
|
||||
q = questionRepository.save(q);
|
||||
|
||||
ResponseEntity<Void> delete = restTemplate.exchange(
|
||||
getUrl("?id=" + q.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, delete.getStatusCode());
|
||||
assertFalse(questionRepository.existsById(q.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetQuestionNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/9999"), String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Question non trouvée"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteQuestionNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Question non trouvée"));
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.Image;
|
||||
import com.example.wfwebapi.model.Quiz;
|
||||
import com.example.wfwebapi.repository.ImageRepository;
|
||||
import com.example.wfwebapi.repository.QuizRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class QuizControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired private QuizRepository quizRepository;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/quiz" + path;
|
||||
}
|
||||
|
||||
private Image image;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
quizRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
image = new Image();
|
||||
image.setImgPath("quiz/image.png");
|
||||
imageRepository.save(image);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateAndGetQuiz() {
|
||||
Quiz quiz = new Quiz();
|
||||
quiz.setTitle("Histoire");
|
||||
quiz.setImage(image);
|
||||
quiz.setNbQuest(5);
|
||||
|
||||
ResponseEntity<Quiz> postResponse = restTemplate.postForEntity(getUrl(""), quiz, Quiz.class);
|
||||
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
||||
Quiz created = postResponse.getBody();
|
||||
assertNotNull(created);
|
||||
assertNotNull(created.getId());
|
||||
|
||||
ResponseEntity<Quiz> getResponse = restTemplate.getForEntity(getUrl("/" + created.getId()), Quiz.class);
|
||||
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
||||
assertEquals("Histoire", getResponse.getBody().getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateQuiz() {
|
||||
Quiz quiz = new Quiz();
|
||||
quiz.setTitle("Sciences");
|
||||
quiz.setImage(image);
|
||||
quiz.setNbQuest(10);
|
||||
quiz = quizRepository.save(quiz);
|
||||
|
||||
quiz.setTitle("Sciences - Maj");
|
||||
quiz.setNbQuest(12);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
HttpEntity<Quiz> entity = new HttpEntity<>(quiz, headers);
|
||||
|
||||
ResponseEntity<Quiz> putResponse = restTemplate.exchange(getUrl(""), HttpMethod.PUT, entity, Quiz.class);
|
||||
assertEquals(HttpStatus.OK, putResponse.getStatusCode());
|
||||
assertEquals("Sciences - Maj", putResponse.getBody().getTitle());
|
||||
assertEquals(12, putResponse.getBody().getNbQuest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteQuiz() {
|
||||
Quiz quiz = new Quiz();
|
||||
quiz.setTitle("À supprimer");
|
||||
quiz.setImage(image);
|
||||
quiz.setNbQuest(3);
|
||||
quiz = quizRepository.save(quiz);
|
||||
|
||||
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||
getUrl("?id=" + quiz.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
||||
assertFalse(quizRepository.existsById(quiz.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetQuizNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(getUrl("/9999"), String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Quiz non trouvé"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteQuizNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
getUrl("?id=9999"), HttpMethod.DELETE, null, String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Quiz non trouvé"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllQuizzes() {
|
||||
Quiz q1 = new Quiz();
|
||||
q1.setTitle("Test 1");
|
||||
q1.setImage(image);
|
||||
q1.setNbQuest(5);
|
||||
|
||||
Quiz q2 = new Quiz();
|
||||
q2.setTitle("Test 2");
|
||||
q2.setImage(image);
|
||||
q2.setNbQuest(10);
|
||||
|
||||
quizRepository.save(q1);
|
||||
quizRepository.save(q2);
|
||||
|
||||
ResponseEntity<Quiz[]> response = restTemplate.getForEntity(getUrl(""), Quiz[].class);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(2, response.getBody().length);
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.model.*;
|
||||
import com.example.wfwebapi.repository.*;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.http.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class QuoteControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
@Autowired private QuoteRepository quoteRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
@Autowired private SourceRepository sourceRepository;
|
||||
@Autowired private CaracterRepository caracterRepository;
|
||||
@Autowired private ImageRepository imageRepository;
|
||||
|
||||
private User user;
|
||||
private Source source;
|
||||
private Caracter caracter;
|
||||
|
||||
private String getUrl(String path) {
|
||||
return "http://localhost:" + port + "/api/v1/quote" + path;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
quoteRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
sourceRepository.deleteAll();
|
||||
caracterRepository.deleteAll();
|
||||
imageRepository.deleteAll();
|
||||
|
||||
Image img = new Image();
|
||||
img.setImgPath("img.png");
|
||||
img = imageRepository.save(img); // correction ici
|
||||
|
||||
caracter = new Caracter();
|
||||
caracter.setCaracter("Test Character");
|
||||
caracter.setImage(img);
|
||||
caracter = caracterRepository.save(caracter);
|
||||
|
||||
source = new Source();
|
||||
source.setTitle("Test Source");
|
||||
source.setDateS(2020);
|
||||
source = sourceRepository.save(source);
|
||||
|
||||
user = new User();
|
||||
user.setUsername("verif");
|
||||
user.setEmail("v@v.fr");
|
||||
user.setPassword("pass");
|
||||
user.setImage(img);
|
||||
user.setCreation(java.time.LocalDate.now());
|
||||
user = userRepository.save(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteQuote() {
|
||||
Quote quote = new Quote();
|
||||
quote.setContent("À supprimer");
|
||||
quote.setLangue("fr");
|
||||
quote.setLikes(0);
|
||||
quote.setIsValide(false);
|
||||
quote.setReason("test");
|
||||
quote.setCaracter(caracter);
|
||||
quote.setSource(source);
|
||||
quote.setUserVerif(user);
|
||||
quote = quoteRepository.save(quote);
|
||||
|
||||
ResponseEntity<Void> response = restTemplate.exchange(
|
||||
getUrl("/delete?idQuote=" + quote.getId()), HttpMethod.DELETE, null, Void.class);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertFalse(quoteRepository.existsById(quote.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetQuoteNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||
getUrl("/9999"), String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Citation non trouvée"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllQuotes() {
|
||||
Quote quote1 = new Quote();
|
||||
quote1.setContent("Q1");
|
||||
quote1.setLangue("fr");
|
||||
quote1.setLikes(5);
|
||||
quote1.setIsValide(true);
|
||||
quote1.setReason("ok");
|
||||
quote1.setCaracter(caracter);
|
||||
quote1.setSource(source);
|
||||
quote1.setUserVerif(user);
|
||||
|
||||
Quote quote2 = new Quote();
|
||||
quote2.setContent("Q2");
|
||||
quote2.setLangue("en");
|
||||
quote2.setLikes(10);
|
||||
quote2.setIsValide(true);
|
||||
quote2.setReason("ok");
|
||||
quote2.setCaracter(caracter);
|
||||
quote2.setSource(source);
|
||||
quote2.setUserVerif(user);
|
||||
|
||||
quoteRepository.save(quote1);
|
||||
quoteRepository.save(quote2);
|
||||
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||
getUrl("/all?index=0&count=10"), String.class);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetDailyQuoteNotFound() {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(
|
||||
getUrl("/dailyquote?year=2023&month=12&day=1&lang=zz"), String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertTrue(response.getBody().contains("Aucune citation trouvée"));
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package com.example.wfwebapi.controller;
|
||||
|
||||
import com.example.wfwebapi.exception.ResourceNotFoundException;
|
||||
import com.example.wfwebapi.model.RecordQuiz;
|
||||
import com.example.wfwebapi.model.RecordQuizId;
|
||||
import com.example.wfwebapi.repository.RecordQuizRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class RecordQuizControllerTest {
|
||||
|
||||
@Mock
|
||||
private RecordQuizRepository recordQuizRepository;
|
||||
|
||||
@InjectMocks
|
||||
private RecordQuizController recordQuizController;
|
||||
|
||||
private RecordQuizId recordQuizId;
|
||||
private RecordQuiz recordQuiz;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
recordQuizId = new RecordQuizId(1L, 1L);
|
||||
recordQuiz = new RecordQuiz();
|
||||
recordQuiz.setId(recordQuizId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRecord_success() {
|
||||
when(recordQuizRepository.findById(recordQuizId)).thenReturn(Optional.of(recordQuiz));
|
||||
|
||||
RecordQuiz result = recordQuizController.getRecord(1L, 1L);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(recordQuizId, result.getId());
|
||||
verify(recordQuizRepository, times(1)).findById(recordQuizId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRecord_notFound() {
|
||||
when(recordQuizRepository.findById(recordQuizId)).thenReturn(Optional.empty());
|
||||
|
||||
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||
recordQuizController.getRecord(1L, 1L);
|
||||
});
|
||||
|
||||
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||
verify(recordQuizRepository, times(1)).findById(recordQuizId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateRecord_success() {
|
||||
when(recordQuizRepository.save(recordQuiz)).thenReturn(recordQuiz);
|
||||
|
||||
RecordQuiz result = recordQuizController.createRecord(recordQuiz);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(recordQuizId, result.getId());
|
||||
verify(recordQuizRepository, times(1)).save(recordQuiz);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateRecord_success() {
|
||||
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(true);
|
||||
when(recordQuizRepository.save(recordQuiz)).thenReturn(recordQuiz);
|
||||
|
||||
RecordQuiz result = recordQuizController.updateRecord(recordQuiz);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(recordQuizId, result.getId());
|
||||
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||
verify(recordQuizRepository, times(1)).save(recordQuiz);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateRecord_notFound() {
|
||||
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(false);
|
||||
|
||||
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||
recordQuizController.updateRecord(recordQuiz);
|
||||
});
|
||||
|
||||
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||
verify(recordQuizRepository, times(0)).save(recordQuiz);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteRecord_success() {
|
||||
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(true);
|
||||
|
||||
recordQuizController.deleteRecord(1L, 1L);
|
||||
|
||||
verify(recordQuizRepository, times(1)).deleteById(recordQuizId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteRecord_notFound() {
|
||||
when(recordQuizRepository.existsById(recordQuizId)).thenReturn(false);
|
||||
|
||||
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> {
|
||||
recordQuizController.deleteRecord(1L, 1L);
|
||||
});
|
||||
|
||||
assertEquals("RecordQuiz non trouvé", exception.getMessage());
|
||||
verify(recordQuizRepository, times(1)).existsById(recordQuizId);
|
||||
verify(recordQuizRepository, times(0)).deleteById(recordQuizId);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AdminTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Admin admin = new Admin();
|
||||
User user = new User();
|
||||
|
||||
admin.setUserId(123L);
|
||||
admin.setUser(user);
|
||||
|
||||
assertEquals(123L, admin.getUserId());
|
||||
assertEquals(user, admin.getUser());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CaracterTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Caracter caracter = new Caracter();
|
||||
Image image = new Image();
|
||||
|
||||
caracter.setId(1L);
|
||||
caracter.setCaracter("Guerrier");
|
||||
caracter.setImage(image);
|
||||
|
||||
assertEquals(1L, caracter.getId());
|
||||
assertEquals("Guerrier", caracter.getCaracter());
|
||||
assertEquals(image, caracter.getImage());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CommentaryTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Commentary commentary = new Commentary();
|
||||
Quote quote = new Quote();
|
||||
User user = new User();
|
||||
LocalDate date = LocalDate.of(2024, 4, 5);
|
||||
String text = "Ceci est un commentaire";
|
||||
|
||||
commentary.setId(1L);
|
||||
commentary.setQuote(quote);
|
||||
commentary.setUser(user);
|
||||
commentary.setDateC(date);
|
||||
commentary.setComment(text);
|
||||
|
||||
assertEquals(1L, commentary.getId());
|
||||
assertEquals(quote, commentary.getQuote());
|
||||
assertEquals(user, commentary.getUser());
|
||||
assertEquals(date, commentary.getDateC());
|
||||
assertEquals(text, commentary.getComment());
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DailyQuoteTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
DailyQuote dailyQuote = new DailyQuote();
|
||||
Quote quote = new Quote();
|
||||
|
||||
dailyQuote.setCitationId(1L);
|
||||
dailyQuote.setQuote(quote);
|
||||
|
||||
assertEquals(1L, dailyQuote.getCitationId());
|
||||
assertEquals(quote, dailyQuote.getQuote());
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FavoriteIdTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
FavoriteId id = new FavoriteId();
|
||||
id.setUser(1L);
|
||||
id.setQuote(2L);
|
||||
|
||||
assertEquals(1L, id.getUser());
|
||||
assertEquals(2L, id.getQuote());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
FavoriteId id = new FavoriteId(3L, 4L);
|
||||
|
||||
assertEquals(3L, id.getUser());
|
||||
assertEquals(4L, id.getQuote());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEqualsAndHashCode() {
|
||||
FavoriteId id1 = new FavoriteId(1L, 2L);
|
||||
FavoriteId id2 = new FavoriteId(1L, 2L);
|
||||
FavoriteId id3 = new FavoriteId(2L, 1L);
|
||||
|
||||
assertEquals(id1, id2);
|
||||
assertEquals(id1.hashCode(), id2.hashCode());
|
||||
assertNotEquals(id1, id3);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FavoriteTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Favorite favorite = new Favorite();
|
||||
FavoriteId favoriteId = new FavoriteId();
|
||||
User user = new User();
|
||||
Quote quote = new Quote();
|
||||
|
||||
favorite.setId(favoriteId);
|
||||
favorite.setUser(user);
|
||||
favorite.setQuote(quote);
|
||||
|
||||
assertEquals(favoriteId, favorite.getId());
|
||||
assertEquals(user, favorite.getUser());
|
||||
assertEquals(quote, favorite.getQuote());
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ImageTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Image image = new Image();
|
||||
image.setId(1L);
|
||||
image.setImgPath("/images/avatar.png");
|
||||
|
||||
assertEquals(1L, image.getId());
|
||||
assertEquals("/images/avatar.png", image.getImgPath());
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QuestionTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Question question = new Question();
|
||||
|
||||
question.setId(1L);
|
||||
question.setTexte("Quelle est la capitale de la France ?");
|
||||
question.setAnswerA("Paris");
|
||||
question.setAnswerB("Lyon");
|
||||
question.setAnswerC("Marseille");
|
||||
question.setAnswerD("Nice");
|
||||
question.setCAnswer("Paris");
|
||||
|
||||
assertEquals(1L, question.getId());
|
||||
assertEquals("Quelle est la capitale de la France ?", question.getTexte());
|
||||
assertEquals("Paris", question.getAnswerA());
|
||||
assertEquals("Lyon", question.getAnswerB());
|
||||
assertEquals("Marseille", question.getAnswerC());
|
||||
assertEquals("Nice", question.getAnswerD());
|
||||
assertEquals("Paris", question.getCAnswer());
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QuizQuestionIdTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
QuizQuestionId id = new QuizQuestionId();
|
||||
id.setQuiz(1L);
|
||||
id.setQuestion(2L);
|
||||
|
||||
assertEquals(1L, id.getQuiz());
|
||||
assertEquals(2L, id.getQuestion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
QuizQuestionId id = new QuizQuestionId(3L, 4L);
|
||||
|
||||
assertEquals(3L, id.getQuiz());
|
||||
assertEquals(4L, id.getQuestion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEqualsAndHashCode() {
|
||||
QuizQuestionId id1 = new QuizQuestionId(1L, 2L);
|
||||
QuizQuestionId id2 = new QuizQuestionId(1L, 2L);
|
||||
QuizQuestionId id3 = new QuizQuestionId(2L, 1L);
|
||||
|
||||
assertEquals(id1, id2);
|
||||
assertEquals(id1.hashCode(), id2.hashCode());
|
||||
assertNotEquals(id1, id3);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QuizQuestionTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
QuizQuestion quizQuestion = new QuizQuestion();
|
||||
QuizQuestionId id = new QuizQuestionId();
|
||||
Quiz quiz = new Quiz();
|
||||
Question question = new Question();
|
||||
|
||||
quizQuestion.setId(id);
|
||||
quizQuestion.setQuiz(quiz);
|
||||
quizQuestion.setQuestion(question);
|
||||
|
||||
assertEquals(id, quizQuestion.getId());
|
||||
assertEquals(quiz, quizQuestion.getQuiz());
|
||||
assertEquals(question, quizQuestion.getQuestion());
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QuizTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Quiz quiz = new Quiz();
|
||||
Image image = new Image();
|
||||
|
||||
quiz.setId(1L);
|
||||
quiz.setTitle("Histoire de France");
|
||||
quiz.setImage(image);
|
||||
quiz.setNbQuest(10);
|
||||
|
||||
assertEquals(1L, quiz.getId());
|
||||
assertEquals("Histoire de France", quiz.getTitle());
|
||||
assertEquals(image, quiz.getImage());
|
||||
assertEquals(10, quiz.getNbQuest());
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class QuoteTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Quote quote = new Quote();
|
||||
Caracter caracter = new Caracter();
|
||||
Source source = new Source();
|
||||
User user = new User();
|
||||
|
||||
quote.setId(1L);
|
||||
quote.setContent("Ceci est une citation.");
|
||||
quote.setLikes(42);
|
||||
quote.setLangue("fr");
|
||||
quote.setIsValide(true);
|
||||
quote.setReason("Vérifiée manuellement");
|
||||
quote.setCaracter(caracter);
|
||||
quote.setSource(source);
|
||||
quote.setUserVerif(user);
|
||||
|
||||
assertEquals(1L, quote.getId());
|
||||
assertEquals("Ceci est une citation.", quote.getContent());
|
||||
assertEquals(42, quote.getLikes());
|
||||
assertEquals("fr", quote.getLangue());
|
||||
assertTrue(quote.getIsValide());
|
||||
assertEquals("Vérifiée manuellement", quote.getReason());
|
||||
assertEquals(caracter, quote.getCaracter());
|
||||
assertEquals(source, quote.getSource());
|
||||
assertEquals(user, quote.getUserVerif());
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RecordQuizIdTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
RecordQuizId id = new RecordQuizId();
|
||||
id.setUser(1L);
|
||||
id.setQuiz(2L);
|
||||
|
||||
assertEquals(1L, id.getUser());
|
||||
assertEquals(2L, id.getQuiz());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConstructor() {
|
||||
RecordQuizId id = new RecordQuizId(3L, 4L);
|
||||
|
||||
assertEquals(3L, id.getUser());
|
||||
assertEquals(4L, id.getQuiz());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEqualsAndHashCode() {
|
||||
RecordQuizId id1 = new RecordQuizId(1L, 2L);
|
||||
RecordQuizId id2 = new RecordQuizId(1L, 2L);
|
||||
RecordQuizId id3 = new RecordQuizId(2L, 1L);
|
||||
|
||||
assertEquals(id1, id2);
|
||||
assertEquals(id1.hashCode(), id2.hashCode());
|
||||
assertNotEquals(id1, id3);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RecordQuizTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
RecordQuiz record = new RecordQuiz();
|
||||
RecordQuizId id = new RecordQuizId();
|
||||
User user = new User();
|
||||
Quiz quiz = new Quiz();
|
||||
|
||||
record.setId(id);
|
||||
record.setUser(user);
|
||||
record.setQuiz(quiz);
|
||||
record.setNbPoint(8);
|
||||
record.setTimeQ(120);
|
||||
|
||||
assertEquals(id, record.getId());
|
||||
assertEquals(user, record.getUser());
|
||||
assertEquals(quiz, record.getQuiz());
|
||||
assertEquals(8, record.getNbPoint());
|
||||
assertEquals(120, record.getTimeQ());
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SourceTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
Source source = new Source();
|
||||
|
||||
source.setId(1L);
|
||||
source.setTitle("Discours historique");
|
||||
source.setDateS(1945);
|
||||
|
||||
assertEquals(1L, source.getId());
|
||||
assertEquals("Discours historique", source.getTitle());
|
||||
assertEquals(1945, source.getDateS());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UserTest {
|
||||
|
||||
@Test
|
||||
void testGettersAndSetters() {
|
||||
User user = new User();
|
||||
Image image = new Image();
|
||||
LocalDate creationDate = LocalDate.of(2024, 1, 1);
|
||||
|
||||
user.setId(1L);
|
||||
user.setUsername("testuser");
|
||||
user.setEmail("test@example.com");
|
||||
user.setPassword("securePassword123");
|
||||
user.setImage(image);
|
||||
user.setCreation(creationDate);
|
||||
|
||||
assertEquals(1L, user.getId());
|
||||
assertEquals("testuser", user.getUsername());
|
||||
assertEquals("test@example.com", user.getEmail());
|
||||
assertEquals("securePassword123", user.getPassword());
|
||||
assertEquals(image, user.getImage());
|
||||
assertEquals(creationDate, user.getCreation());
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
/mvnw text eol=lf
|
||||
*.cmd text eol=crlf
|
@ -1,33 +0,0 @@
|
||||
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/
|
@ -1,97 +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/>
|
||||
</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>
|
||||
<version>1.18.38</version>
|
||||
<scope>provided</scope>
|
||||
</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>
|
@ -1,13 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceForbidden;
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Character;
|
||||
import com.example.wtf.repository.CharacterRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
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("/characters")
|
||||
public class CharacterController {
|
||||
|
||||
@Autowired
|
||||
private CharacterRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Character>> getCharacters() {
|
||||
List<EntityModel<Character>> characters = new ArrayList<>();
|
||||
|
||||
for (Character character : repository.findAll()) {
|
||||
EntityModel<Character> characterResource = EntityModel.of(
|
||||
character,
|
||||
linkTo(methodOn(CharacterController.class).getCharacter(character.getId())).withSelfRel(),
|
||||
linkTo(methodOn(CharacterController.class).updateCharacter(character.getId(), new Character())).withRel("Update character"),
|
||||
linkTo(methodOn(CharacterController.class).deleteCharacter(character.getId())).withRel("Delete character")
|
||||
);
|
||||
characters.add(characterResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
characters,
|
||||
linkTo(methodOn(CharacterController.class).addCharacter(new Character())).withRel("Add Character")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Character> getCharacter(@PathVariable Long id) {
|
||||
Character character = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Character not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
character,
|
||||
linkTo(methodOn(CharacterController.class).updateCharacter(character.getId(), new Character())).withRel("Update character"),
|
||||
linkTo(methodOn(CharacterController.class).deleteCharacter(character.getId())).withRel("Delete character"),
|
||||
linkTo(methodOn(CharacterController.class).getCharacters()).withRel("Characters")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Character> addCharacter(@RequestBody Character character) {
|
||||
|
||||
if (repository.existsByName(character.getName())) {
|
||||
throw new ResourceForbidden("Character forbidden");
|
||||
}
|
||||
|
||||
repository.save(character);
|
||||
|
||||
return EntityModel.of(
|
||||
character,
|
||||
linkTo(methodOn(CharacterController.class).getCharacter(character.getId())).withSelfRel(),
|
||||
linkTo(methodOn(CharacterController.class).updateCharacter(character.getId(), new Character())).withRel("Update character"),
|
||||
linkTo(methodOn(CharacterController.class).deleteCharacter(character.getId())).withRel("Delete character"),
|
||||
linkTo(methodOn(CharacterController.class).getCharacters()).withRel("Characters")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Character> updateCharacter(@PathVariable Long id,
|
||||
@RequestBody Character update) {
|
||||
Character character = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Character not found"));
|
||||
|
||||
if (update.getName() != null && !update.getName().isEmpty()) {
|
||||
if (repository.existsByName(character.getName())) {
|
||||
throw new ResourceForbidden("Character forbidden");
|
||||
}
|
||||
character.setName(update.getName());
|
||||
}
|
||||
if (update.getImage() != null) {
|
||||
character.setImage(update.getImage());
|
||||
}
|
||||
|
||||
repository.save(character);
|
||||
|
||||
return EntityModel.of(
|
||||
character,
|
||||
linkTo(methodOn(CharacterController.class).getCharacter(id)).withSelfRel(),
|
||||
linkTo(methodOn(CharacterController.class).deleteCharacter(id)).withRel("Delete character"),
|
||||
linkTo(methodOn(CharacterController.class).getCharacters()).withRel("Characters")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public @ResponseBody EntityModel<Character> deleteCharacter(@PathVariable Long id) {
|
||||
Character character = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Character not found"));
|
||||
|
||||
repository.delete(character);
|
||||
|
||||
return EntityModel.of(
|
||||
character,
|
||||
linkTo(methodOn(CharacterController.class).getCharacters()).withRel("Characters")
|
||||
);
|
||||
}
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Commentary;
|
||||
import com.example.wtf.repository.CommentaryRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
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("/commentaries")
|
||||
public class CommentaryController {
|
||||
@Autowired
|
||||
private CommentaryRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Commentary>> getCommentaries() {
|
||||
List<EntityModel<Commentary>> commentaries = new ArrayList<>();
|
||||
|
||||
for (Commentary commentary : repository.findAll()) {
|
||||
EntityModel<Commentary> commentaryResource = EntityModel.of(
|
||||
commentary,
|
||||
linkTo(methodOn(CommentaryController.class).getCommentary(commentary.getId())).withSelfRel(),
|
||||
linkTo(methodOn(CommentaryController.class).deleteCommentary(commentary.getId())).withRel("Delete Commentary")
|
||||
);
|
||||
commentaries.add(commentaryResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
commentaries,
|
||||
linkTo(methodOn(CommentaryController.class).addCommentary(new Commentary())).withRel("Add Commentary")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Commentary> getCommentary(@PathVariable Long id) {
|
||||
Commentary commentary = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Commentary not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
commentary,
|
||||
linkTo(methodOn(CommentaryController.class).deleteCommentary(commentary.getId())).withRel("Delete Commentary"),
|
||||
linkTo(methodOn(CommentaryController.class).getCommentaries()).withRel("Commentaries")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Commentary> addCommentary(@RequestBody Commentary commentary) {
|
||||
repository.save(commentary);
|
||||
return EntityModel.of(
|
||||
commentary,
|
||||
linkTo(methodOn(CommentaryController.class).getCommentary(commentary.getId())).withSelfRel(),
|
||||
linkTo(methodOn(CommentaryController.class).deleteCommentary(commentary.getId())).withRel("Delete Commentary"),
|
||||
linkTo(methodOn(CommentaryController.class).getCommentaries()).withRel("Commentaries")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Commentary> updateCommentary(@PathVariable Long id, @RequestBody Commentary update) {
|
||||
Commentary commentary = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Commentary not found"));
|
||||
|
||||
if (update.getUser() != null) {
|
||||
commentary.setUser(update.getUser());
|
||||
}
|
||||
if (update.getQuote() != null) {
|
||||
commentary.setQuote(update.getQuote());
|
||||
}
|
||||
if (update.getComment() != null && !update.getComment().isEmpty()) {
|
||||
commentary.setComment(update.getComment());
|
||||
}
|
||||
if (update.getDateC() != null) {
|
||||
commentary.setDateC(update.getDateC());
|
||||
}
|
||||
|
||||
repository.save(commentary);
|
||||
|
||||
return EntityModel.of(
|
||||
commentary,
|
||||
linkTo(methodOn(CommentaryController.class).updateCommentary(id, new Commentary())).withSelfRel()
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public @ResponseBody EntityModel<Commentary> deleteCommentary(@PathVariable Long id) {
|
||||
Commentary commentary = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Commentary not found"));
|
||||
|
||||
repository.delete(commentary);
|
||||
|
||||
return EntityModel.of(
|
||||
commentary,
|
||||
linkTo(methodOn(CommentaryController.class).getCommentaries()).withRel("Commentaries")
|
||||
);
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Favorite;
|
||||
import com.example.wtf.repository.FavoriteRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
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("/favorites")
|
||||
public class FavoriteController {
|
||||
@Autowired
|
||||
private FavoriteRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Favorite>> getFavorites() {
|
||||
List<EntityModel<Favorite>> favorites = new ArrayList<>();
|
||||
|
||||
for (Favorite favorite : repository.findAll()) {
|
||||
EntityModel<Favorite> favoriteResource = EntityModel.of(
|
||||
favorite,
|
||||
linkTo(methodOn(FavoriteController.class).getFavorite(favorite.getId())).withSelfRel(),
|
||||
linkTo(methodOn(FavoriteController.class).deleteFavorite(favorite.getId())).withRel("Delete Favorite")
|
||||
);
|
||||
favorites.add(favoriteResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
favorites,
|
||||
linkTo(methodOn(FavoriteController.class).addFavorite(new Favorite())).withRel("Add Favorite")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Favorite> getFavorite(@PathVariable Long id) {
|
||||
Favorite favorite = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Favorite not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
favorite,
|
||||
linkTo(methodOn(FavoriteController.class).deleteFavorite(favorite.getId())).withRel("Delete Favorite"),
|
||||
linkTo(methodOn(FavoriteController.class).getFavorites()).withRel("Favorites")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Favorite> addFavorite(@RequestBody Favorite favorite) {
|
||||
repository.save(favorite);
|
||||
return EntityModel.of(
|
||||
favorite,
|
||||
linkTo(methodOn(FavoriteController.class).getFavorite(favorite.getId())).withSelfRel(),
|
||||
linkTo(methodOn(FavoriteController.class).deleteFavorite(favorite.getId())).withRel("Delete Favorite"),
|
||||
linkTo(methodOn(FavoriteController.class).getFavorites()).withRel("Favorites")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Favorite> updateFavorite(@PathVariable Long id, @RequestBody Favorite update) {
|
||||
Favorite favorite = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Favorite not found"));
|
||||
|
||||
if (update.getUser() != null) {
|
||||
favorite.setUser(update.getUser());
|
||||
}
|
||||
if (update.getQuote() != null) {
|
||||
favorite.setQuote(update.getQuote());
|
||||
}
|
||||
|
||||
repository.save(favorite);
|
||||
|
||||
return EntityModel.of(
|
||||
favorite,
|
||||
linkTo(methodOn(FavoriteController.class).updateFavorite(id, new Favorite())).withSelfRel()
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public @ResponseBody EntityModel<Favorite> deleteFavorite(@PathVariable Long id) {
|
||||
Favorite favorite = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Favorite not found"));
|
||||
|
||||
repository.delete(favorite);
|
||||
|
||||
return EntityModel.of(
|
||||
favorite,
|
||||
linkTo(methodOn(FavoriteController.class).getFavorites()).withRel("Favorites")
|
||||
);
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue