Compare commits
7 Commits
master
...
TommyVersi
Author | SHA1 | Date |
---|---|---|
![]() |
b639600f76 | 3 weeks ago |
|
546cb5dfc4 | 3 weeks ago |
![]() |
b6e2601b90 | 3 weeks ago |
![]() |
53f9a0b8aa | 3 weeks ago |
![]() |
236378e0cb | 3 weeks ago |
|
fa1a716e6a | 3 weeks ago |
![]() |
20b068d6bf | 3 weeks ago |
@ -1,13 +0,0 @@
|
||||
# Étape de build
|
||||
FROM maven:3.8.6-openjdk-21 AS build
|
||||
WORKDIR /app
|
||||
COPY pom.xml .
|
||||
COPY src ./src
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
# Étape d'exécution
|
||||
FROM openjdk:21-jdk
|
||||
WORKDIR /app
|
||||
COPY --from=build /app/target/demo-1.0.0.jar app.jar
|
||||
EXPOSE 8080
|
||||
ENTRYPOINT ["java", "-jar", "app.jar"]
|
@ -1,14 +0,0 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
image: maven:3.8.6-openjdk-21
|
||||
commands:
|
||||
- mvn clean package -DskipTests
|
||||
|
||||
- name: test
|
||||
image: maven:3.8.6-openjdk-21
|
||||
commands:
|
||||
- mvn test
|
@ -1,88 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.4</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>WF-WEBAPI</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>WF-WEBAPI</name>
|
||||
<description>WF-WEBAPI</description>
|
||||
<url/>
|
||||
<licenses>
|
||||
<license/>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer/>
|
||||
</developers>
|
||||
<scm>
|
||||
<connection/>
|
||||
<developerConnection/>
|
||||
<tag/>
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-api</artifactId>
|
||||
<version>0.11.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-impl</artifactId>
|
||||
<version>0.11.5</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt-jackson</artifactId>
|
||||
<version>0.11.5</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -1,11 +0,0 @@
|
||||
package com.example.wfwebapi;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WfWebapiApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WfWebapiApplication.class, args);
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package com.example.wfwebapi.config;
|
||||
|
||||
import com.example.wfwebapi.security.JwtAuthenticationFilter;
|
||||
import com.example.wfwebapi.security.JwtTokenProvider;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
private final JwtTokenProvider tokenProvider;
|
||||
|
||||
public SecurityConfig(JwtTokenProvider tokenProvider) {
|
||||
this.tokenProvider = tokenProvider;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf(csrf -> csrf.disable())
|
||||
.authorizeHttpRequests(authz -> authz
|
||||
.requestMatchers("/api/v1/auth/**").permitAll() // par exemple, pour la connexion
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
.addFilterBefore(new JwtAuthenticationFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class)
|
||||
.httpBasic(Customizer.withDefaults());
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
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.*;
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
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.repository.FavoriteRepository;
|
||||
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;
|
||||
|
||||
@GetMapping("/{userId}")
|
||||
public List<Favorite> getFavoritesByUser(@PathVariable Long userId) {
|
||||
return favoriteRepository.findByUser_Id(userId);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Favorite addFavorite(@RequestParam Long user, @RequestParam Long quote) {
|
||||
Favorite favorite = new Favorite();
|
||||
favorite.setId(new FavoriteId(user, quote));
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
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.*;
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
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) {
|
||||
// Exemple : renvoi la première citation correspondant à la langue
|
||||
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 quote = quoteRepository.save(newQuote);
|
||||
return assembler.toModel(quote);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
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,7 +0,0 @@
|
||||
package com.example.wfwebapi.exception;
|
||||
|
||||
public class ResourceNotFoundException extends RuntimeException {
|
||||
public ResourceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.example.wfwebapi.exception;
|
||||
|
||||
public class UnauthorizedException extends RuntimeException {
|
||||
public UnauthorizedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Admin")
|
||||
public class Admin {
|
||||
@Id
|
||||
@Column(name = "users")
|
||||
private Long userId;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "users", insertable = false, updatable = false)
|
||||
private User user;
|
||||
|
||||
// Getters et setters
|
||||
public Long getUserId() { return userId; }
|
||||
public void setUserId(Long userId) { this.userId = userId; }
|
||||
public User getUser() { return user; }
|
||||
public void setUser(User user) { this.user = user; }
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Caracter")
|
||||
public class Caracter {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_caracter")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "caracter", nullable = false, length = 100)
|
||||
private String caracter;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_img", nullable = false)
|
||||
private Image image;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getCaracter() { return caracter; }
|
||||
public void setCaracter(String caracter) { this.caracter = caracter; }
|
||||
public Image getImage() { return image; }
|
||||
public void setImage(Image image) { this.image = image; }
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Commentary")
|
||||
public class Commentary {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_comment")
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "quote", nullable = false)
|
||||
private Quote quote;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "users", nullable = false)
|
||||
private User user;
|
||||
|
||||
@Column(name = "dateC", nullable = false)
|
||||
private LocalDate dateC;
|
||||
|
||||
@Column(name = "comment", nullable = false, columnDefinition = "text")
|
||||
private String comment;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public Quote getQuote() { return quote; }
|
||||
public void setQuote(Quote quote) { this.quote = quote; }
|
||||
public User getUser() { return user; }
|
||||
public void setUser(User user) { this.user = user; }
|
||||
public LocalDate getDateC() { return dateC; }
|
||||
public void setDateC(LocalDate dateC) { this.dateC = dateC; }
|
||||
public String getComment() { return comment; }
|
||||
public void setComment(String comment) { this.comment = comment; }
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "DailyQuote")
|
||||
public class DailyQuote {
|
||||
@Id
|
||||
@Column(name = "citation_id")
|
||||
private Long citationId;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "citation_id", insertable = false, updatable = false)
|
||||
private Quote quote;
|
||||
|
||||
// Getters et setters
|
||||
public Long getCitationId() { return citationId; }
|
||||
public void setCitationId(Long citationId) { this.citationId = citationId; }
|
||||
public Quote getQuote() { return quote; }
|
||||
public void setQuote(Quote quote) { this.quote = quote; }
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Favorite")
|
||||
public class Favorite {
|
||||
@EmbeddedId
|
||||
private FavoriteId id;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("user")
|
||||
@JoinColumn(name = "users")
|
||||
private User user;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("quote")
|
||||
@JoinColumn(name = "quote")
|
||||
private Quote quote;
|
||||
|
||||
// Getters et setters
|
||||
public FavoriteId getId() { return id; }
|
||||
public void setId(FavoriteId id) { this.id = id; }
|
||||
public User getUser() { return user; }
|
||||
public void setUser(User user) { this.user = user; }
|
||||
public Quote getQuote() { return quote; }
|
||||
public void setQuote(Quote quote) { this.quote = quote; }
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class FavoriteId implements Serializable {
|
||||
private Long user;
|
||||
private Long quote;
|
||||
|
||||
public FavoriteId() {}
|
||||
|
||||
public FavoriteId(Long user, Long quote) {
|
||||
this.user = user;
|
||||
this.quote = quote;
|
||||
}
|
||||
|
||||
public Long getUser() { return user; }
|
||||
public void setUser(Long user) { this.user = user; }
|
||||
public Long getQuote() { return quote; }
|
||||
public void setQuote(Long quote) { this.quote = quote; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof FavoriteId)) return false;
|
||||
FavoriteId that = (FavoriteId) o;
|
||||
return Objects.equals(getUser(), that.getUser()) &&
|
||||
Objects.equals(getQuote(), that.getQuote());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getUser(), getQuote());
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Image")
|
||||
public class Image {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_img")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "imgPath", nullable = false, unique = true, length = 300)
|
||||
private String imgPath;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getImgPath() { return imgPath; }
|
||||
public void setImgPath(String imgPath) { this.imgPath = imgPath; }
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Question")
|
||||
public class Question {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_question")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "texte", nullable = false, columnDefinition = "text", unique = true)
|
||||
private String texte;
|
||||
|
||||
@Column(name = "answerA", nullable = false, length = 30)
|
||||
private String answerA;
|
||||
|
||||
@Column(name = "answerB", nullable = false, length = 30)
|
||||
private String answerB;
|
||||
|
||||
@Column(name = "answerC", nullable = false, length = 30)
|
||||
private String answerC;
|
||||
|
||||
@Column(name = "answerD", nullable = false, length = 30)
|
||||
private String answerD;
|
||||
|
||||
@Column(name = "cAnswer", nullable = false, length = 30)
|
||||
private String cAnswer;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getTexte() { return texte; }
|
||||
public void setTexte(String texte) { this.texte = texte; }
|
||||
public String getAnswerA() { return answerA; }
|
||||
public void setAnswerA(String answerA) { this.answerA = answerA; }
|
||||
public String getAnswerB() { return answerB; }
|
||||
public void setAnswerB(String answerB) { this.answerB = answerB; }
|
||||
public String getAnswerC() { return answerC; }
|
||||
public void setAnswerC(String answerC) { this.answerC = answerC; }
|
||||
public String getAnswerD() { return answerD; }
|
||||
public void setAnswerD(String answerD) { this.answerD = answerD; }
|
||||
public String getCAnswer() { return cAnswer; }
|
||||
public void setCAnswer(String cAnswer) { this.cAnswer = cAnswer; }
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Quiz")
|
||||
public class Quiz {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_quiz")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "title", nullable = false, length = 40)
|
||||
private String title;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "img", nullable = false)
|
||||
private Image image;
|
||||
|
||||
@Column(name = "nb_quest")
|
||||
private Integer nbQuest;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
public Image getImage() { return image; }
|
||||
public void setImage(Image image) { this.image = image; }
|
||||
public Integer getNbQuest() { return nbQuest; }
|
||||
public void setNbQuest(Integer nbQuest) { this.nbQuest = nbQuest; }
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Quiz_Question")
|
||||
public class QuizQuestion {
|
||||
@EmbeddedId
|
||||
private QuizQuestionId id;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("quiz")
|
||||
@JoinColumn(name = "quiz")
|
||||
private Quiz quiz;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("question")
|
||||
@JoinColumn(name = "question")
|
||||
private Question question;
|
||||
|
||||
// Getters et setters
|
||||
public QuizQuestionId getId() { return id; }
|
||||
public void setId(QuizQuestionId id) { this.id = id; }
|
||||
public Quiz getQuiz() { return quiz; }
|
||||
public void setQuiz(Quiz quiz) { this.quiz = quiz; }
|
||||
public Question getQuestion() { return question; }
|
||||
public void setQuestion(Question question) { this.question = question; }
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class QuizQuestionId implements Serializable {
|
||||
private Long quiz;
|
||||
private Long question;
|
||||
|
||||
public QuizQuestionId() {}
|
||||
|
||||
public QuizQuestionId(Long quiz, Long question) {
|
||||
this.quiz = quiz;
|
||||
this.question = question;
|
||||
}
|
||||
|
||||
public Long getQuiz() { return quiz; }
|
||||
public void setQuiz(Long quiz) { this.quiz = quiz; }
|
||||
public Long getQuestion() { return question; }
|
||||
public void setQuestion(Long question) { this.question = question; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof QuizQuestionId)) return false;
|
||||
QuizQuestionId that = (QuizQuestionId) o;
|
||||
return Objects.equals(getQuiz(), that.getQuiz()) &&
|
||||
Objects.equals(getQuestion(), that.getQuestion());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getQuiz(), getQuestion());
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Quote")
|
||||
public class Quote {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_quote")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "content", nullable = false, columnDefinition = "text")
|
||||
private String content;
|
||||
|
||||
@Column(name = "likes")
|
||||
private Integer likes;
|
||||
|
||||
@Column(name = "langue", nullable = false, length = 2)
|
||||
private String langue;
|
||||
|
||||
@Column(name = "isValide", nullable = false)
|
||||
private Boolean isValide;
|
||||
|
||||
@Column(name = "reason", nullable = false, length = 100)
|
||||
private String reason;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_caracter", nullable = false)
|
||||
private Caracter caracter;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_source", nullable = false)
|
||||
private Source source;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_user_verif", nullable = false)
|
||||
private User userVerif;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getContent() { return content; }
|
||||
public void setContent(String content) { this.content = content; }
|
||||
public Integer getLikes() { return likes; }
|
||||
public void setLikes(Integer likes) { this.likes = likes; }
|
||||
public String getLangue() { return langue; }
|
||||
public void setLangue(String langue) { this.langue = langue; }
|
||||
public Boolean getIsValide() { return isValide; }
|
||||
public void setIsValide(Boolean isValide) { this.isValide = isValide; }
|
||||
public String getReason() { return reason; }
|
||||
public void setReason(String reason) { this.reason = reason; }
|
||||
public Caracter getCaracter() { return caracter; }
|
||||
public void setCaracter(Caracter caracter) { this.caracter = caracter; }
|
||||
public Source getSource() { return source; }
|
||||
public void setSource(Source source) { this.source = source; }
|
||||
public User getUserVerif() { return userVerif; }
|
||||
public void setUserVerif(User userVerif) { this.userVerif = userVerif; }
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Record_quiz")
|
||||
public class RecordQuiz {
|
||||
@EmbeddedId
|
||||
private RecordQuizId id;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("user")
|
||||
@JoinColumn(name = "users")
|
||||
private User user;
|
||||
|
||||
@ManyToOne
|
||||
@MapsId("quiz")
|
||||
@JoinColumn(name = "quiz")
|
||||
private Quiz quiz;
|
||||
|
||||
@Column(name = "nbPoint")
|
||||
private Integer nbPoint;
|
||||
|
||||
@Column(name = "timeQ")
|
||||
private Integer timeQ;
|
||||
|
||||
// Getters et setters
|
||||
public RecordQuizId getId() { return id; }
|
||||
public void setId(RecordQuizId id) { this.id = id; }
|
||||
public User getUser() { return user; }
|
||||
public void setUser(User user) { this.user = user; }
|
||||
public Quiz getQuiz() { return quiz; }
|
||||
public void setQuiz(Quiz quiz) { this.quiz = quiz; }
|
||||
public Integer getNbPoint() { return nbPoint; }
|
||||
public void setNbPoint(Integer nbPoint) { this.nbPoint = nbPoint; }
|
||||
public Integer getTimeQ() { return timeQ; }
|
||||
public void setTimeQ(Integer timeQ) { this.timeQ = timeQ; }
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class RecordQuizId implements Serializable {
|
||||
private Long user;
|
||||
private Long quiz;
|
||||
|
||||
public RecordQuizId() {}
|
||||
|
||||
public RecordQuizId(Long user, Long quiz) {
|
||||
this.user = user;
|
||||
this.quiz = quiz;
|
||||
}
|
||||
|
||||
public Long getUser() { return user; }
|
||||
public void setUser(Long user) { this.user = user; }
|
||||
public Long getQuiz() { return quiz; }
|
||||
public void setQuiz(Long quiz) { this.quiz = quiz; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof RecordQuizId)) return false;
|
||||
RecordQuizId that = (RecordQuizId) o;
|
||||
return Objects.equals(getUser(), that.getUser()) &&
|
||||
Objects.equals(getQuiz(), that.getQuiz());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getUser(), getQuiz());
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.example.wfwebapi.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Source")
|
||||
public class Source {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_source")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "title", nullable = false, length = 100)
|
||||
private String title;
|
||||
|
||||
@Column(name = "dateS", nullable = false)
|
||||
private Integer dateS;
|
||||
|
||||
// Getters et setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
public Integer getDateS() { return dateS; }
|
||||
public void setDateS(Integer dateS) { this.dateS = dateS; }
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
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> {
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
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> {
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
spring.application.name=WF-WEBAPI
|
||||
|
||||
# Utilisation de H2 avec le script d'initialisation
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.sql.init.mode=always
|
||||
spring.sql.init.schema-locations=classpath:init.sql
|
||||
spring.jpa.hibernate.ddl-auto=none
|
||||
spring.h2.console.enabled=true
|
||||
server.port=8080
|
@ -1,338 +0,0 @@
|
||||
-- Suppression des tables
|
||||
DROP TABLE IF EXISTS Commentary;
|
||||
DROP TABLE IF EXISTS Favorite;
|
||||
DROP TABLE IF EXISTS DailyQuote;
|
||||
DROP TABLE IF EXISTS Quote;
|
||||
DROP TABLE IF EXISTS Caracter;
|
||||
DROP TABLE IF EXISTS Source;
|
||||
DROP TABLE IF EXISTS Record_quiz;
|
||||
DROP TABLE IF EXISTS Quiz_Question;
|
||||
DROP TABLE IF EXISTS Quiz;
|
||||
DROP TABLE IF EXISTS Question;
|
||||
DROP TABLE IF EXISTS Admin;
|
||||
DROP TABLE IF EXISTS Users;
|
||||
DROP TABLE IF EXISTS Image;
|
||||
|
||||
-- Création des tables
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Image(
|
||||
id_img NUMERIC PRIMARY KEY,
|
||||
imgPath varchar(300) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Users(
|
||||
id_user SERIAL PRIMARY KEY,
|
||||
username varchar(50) NOT NULL,
|
||||
email varchar(50) NOT NULL,
|
||||
password varchar(100) NOT NULL,
|
||||
img NUMERIC NOT NULL,
|
||||
creation date NOT NULL,
|
||||
CONSTRAINT unique_col UNIQUE (email),
|
||||
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
|
||||
);
|
||||
|
||||
Create OR REPLACE Function IfUserIsAdmin() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
|
||||
BEGIN
|
||||
Delete From Admin
|
||||
where users = OLD.id_user;
|
||||
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
|
||||
Create Trigger IfUserIsAdmin BEFORE DELETE on Users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION IfUserIsAdmin();
|
||||
|
||||
|
||||
Create OR REPLACE Function DeleteUserFavorite() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
|
||||
BEGIN
|
||||
Delete From Favorite
|
||||
where users = OLD.id_user;
|
||||
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
|
||||
Create Trigger DeleteUserFavorite BEFORE DELETE on Users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION DeleteUserFavorite();
|
||||
|
||||
Create OR REPLACE Function DeleteUserCommentary() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
|
||||
BEGIN
|
||||
Delete From Commentary
|
||||
where users = OLD.id_user;
|
||||
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
|
||||
Create Trigger DeleteUserCommentary BEFORE DELETE on Users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION DeleteUserCommentary();
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Admin(
|
||||
users SERIAL PRIMARY KEY,
|
||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Question(
|
||||
id_question SERIAL PRIMARY KEY,
|
||||
texte text NOT NULL UNIQUE,
|
||||
answerA varchar(30) NOT NULL,
|
||||
answerB varchar(30) NOT NULL,
|
||||
answerC varchar(30) NOT NULL,
|
||||
answerD varchar(30) NOT NULL,
|
||||
cAnswer varchar(30) NOT NULL,
|
||||
CONSTRAINT check_cAnswer CHECK (cAnswer = answerA OR cAnswer = answerB OR cAnswer = answerC OR cAnswer = answerD)
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Quiz(
|
||||
id_quiz SERIAL PRIMARY KEY,
|
||||
title varchar(40) NOT NULL,
|
||||
img NUMERIC NOT NULL,
|
||||
nb_quest numeric Default 0,
|
||||
CONSTRAINT fk_img FOREIGN KEY(img) REFERENCES Image(id_img)
|
||||
);
|
||||
|
||||
Create OR REPLACE Function DeleteQuiz() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
|
||||
BEGIN
|
||||
Delete From Quiz_Question
|
||||
where quiz=OLD.id_quiz;
|
||||
|
||||
Delete From Record_quiz
|
||||
where quiz=OLD.id_quiz;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
Create Trigger DeleteQuiz BEFORE DELETE on Quiz
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION DeleteQuiz();
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Quiz_Question(
|
||||
quiz SERIAL NOT NULL,
|
||||
question SERIAL NOT NULL,
|
||||
PRIMARY KEY (quiz, question),
|
||||
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
|
||||
CONSTRAINT fk_question FOREIGN KEY(question) REFERENCES Question(id_question)
|
||||
);
|
||||
|
||||
Create OR REPLACE Function NombreQuestionQuiz() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
nb numeric;
|
||||
BEGIN
|
||||
|
||||
IF TG_OP='DELETE' Then
|
||||
SELECT count(quiz) INTO nb
|
||||
FROM Quiz_Question
|
||||
WHERE quiz = OLD.quiz;
|
||||
Else
|
||||
SELECT count(quiz) INTO nb
|
||||
FROM Quiz_Question
|
||||
WHERE quiz = NEW.quiz;
|
||||
End IF;
|
||||
|
||||
|
||||
Update Quiz
|
||||
set nb_quest=nb
|
||||
where id_quiz=NEW.quiz;
|
||||
|
||||
Return OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
Create Trigger NombreQuestionQuiz AFTER INSERT or UPDATE or DELETE on Quiz_Question
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION NombreQuestionQuiz();
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Record_quiz(
|
||||
users SERIAL NOT NULL,
|
||||
quiz SERIAL NOT NULL,
|
||||
nbPoint numeric DEFAULT 0,
|
||||
timeQ numeric DEFAULT 0,
|
||||
PRIMARY KEY (users, quiz),
|
||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user),
|
||||
CONSTRAINT fk_quiz FOREIGN KEY(quiz) REFERENCES Quiz(id_quiz),
|
||||
CONSTRAINT err_nbPoint CHECK(nbPoint >= 0),
|
||||
CONSTRAINT err_timeQ CHECK(timeQ >= 0)
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Source(
|
||||
id_source SERIAL PRIMARY KEY,
|
||||
title varchar(100) NOT NULL,
|
||||
dateS numeric(4) NOT NULL
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Caracter(
|
||||
id_caracter SERIAL PRIMARY KEY,
|
||||
caracter varchar(100) NOT NULL,
|
||||
id_img NUMERIC NOT NULL
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Quote(
|
||||
id_quote SERIAL PRIMARY KEY,
|
||||
content text NOT NULL,
|
||||
likes numeric DEFAULT '0',
|
||||
langue char(2) NOT NULL,
|
||||
isValide boolean NOT NULL DEFAULT 'false',
|
||||
reason varchar(100) NOT NULL,
|
||||
id_caracter SERIAL NOT NULL,
|
||||
id_source SERIAL NOT NULL,
|
||||
id_user_verif SERIAL NOT NULL,
|
||||
CONSTRAINT fk_caracter FOREIGN KEY(id_caracter) REFERENCES Caracter(id_caracter),
|
||||
CONSTRAINT fk_source FOREIGN KEY(id_source) REFERENCES Source(id_source),
|
||||
CONSTRAINT fk_userverif FOREIGN KEY(id_user_verif) REFERENCES Users(id_user),
|
||||
CONSTRAINT err_nbLike CHECK (likes >= 0),
|
||||
CONSTRAINT err_language CHECK (langue = 'fr' OR langue = 'en')
|
||||
);
|
||||
|
||||
Create OR REPLACE Function DeleteQuoteBEFORE() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
|
||||
BEGIN
|
||||
Delete From Favorite
|
||||
where quote=OLD.id_quote;
|
||||
|
||||
|
||||
Delete From Commentary
|
||||
where quote=OLD.id_quote;
|
||||
|
||||
|
||||
If OLD.id_quote in (Select citation_id From DailyQuote) Then
|
||||
Update DailyQuote
|
||||
set citation_id = (Select id_quote
|
||||
From Quote
|
||||
Where id_quote!=OLD.id_quote
|
||||
ORDER BY RAND()
|
||||
LIMIT 1)
|
||||
Where citation_id=OLD.id_quote;
|
||||
END IF;
|
||||
|
||||
Return OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
Create Trigger DeleteQuoteBEFORE BEFORE DELETE on Quote
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION DeleteQuoteBEFORE();
|
||||
|
||||
|
||||
Create OR REPLACE Function DeleteQuoteAFTER() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
nb numeric;
|
||||
BEGIN
|
||||
Select count(id_caracter) into nb
|
||||
from Quote
|
||||
where id_caracter=OLD.id_caracter;
|
||||
|
||||
IF nb <= 1 Then
|
||||
Delete from Caracter
|
||||
where id_caracter=OLD.id_caracter;
|
||||
END IF;
|
||||
|
||||
|
||||
Select count(id_source) into nb
|
||||
from Quote
|
||||
where id_source=OLD.id_source;
|
||||
|
||||
IF nb <= 1 Then
|
||||
Delete from Source
|
||||
where id_source=OLD.id_source;
|
||||
END IF;
|
||||
|
||||
Return OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
Create Trigger DeleteQuoteAFTER AFTER DELETE on Quote
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION DeleteQuoteAFTER();
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE DailyQuote(
|
||||
citation_id INT PRIMARY KEY,
|
||||
FOREIGN KEY (citation_id) REFERENCES Quote(id_quote) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
Create OR REPLACE Function UniqueDailyQuote() RETURNS trigger AS $$
|
||||
DECLARE
|
||||
nb numeric;
|
||||
BEGIN
|
||||
Select count(*) into nb
|
||||
from DailyQuote;
|
||||
|
||||
IF nb = 0 Then
|
||||
INSERT INTO DailyQuote (citation_id)
|
||||
VALUES( (Select id_quote
|
||||
From Quote
|
||||
Where id_quote!=OLD.id_quote
|
||||
ORDER BY RAND()
|
||||
LIMIT 1 ) );
|
||||
|
||||
ELSIF nb>1 then
|
||||
|
||||
DELETE From DailyQuote
|
||||
where citation_id!=NEW.citation_id;
|
||||
END IF;
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql ;
|
||||
|
||||
Create Trigger UniqueDailyQuote AFTER INSERT or DELETE on DailyQuote
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION UniqueDailyQuote();
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
|
||||
CREATE TABLE Favorite(
|
||||
users SERIAL NOT NULL,
|
||||
quote SERIAL NOT NULL,
|
||||
PRIMARY KEY (users, quote),
|
||||
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
|
||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
CREATE TABLE Commentary(
|
||||
id_comment SERIAL PRIMARY KEY,
|
||||
quote SERIAL NOT NULL,
|
||||
users SERIAL NOT NULL,
|
||||
dateC date NOT NULL,
|
||||
comment text NOT NULL,
|
||||
CONSTRAINT fk_quote FOREIGN KEY(quote) REFERENCES Quote(id_quote),
|
||||
CONSTRAINT fk_user FOREIGN KEY(users) REFERENCES Users(id_user)
|
||||
);
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
@ -0,0 +1,2 @@
|
||||
/mvnw text eol=lf
|
||||
*.cmd text eol=crlf
|
@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
@ -0,0 +1,97 @@
|
||||
<?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>
|
@ -0,0 +1,13 @@
|
||||
package com.example.wtf;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WtfApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WtfApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.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")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
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")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
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")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Image;
|
||||
import com.example.wtf.repository.ImageRepository;
|
||||
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("/images")
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
private ImageRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Image>> getImages() {
|
||||
List<EntityModel<Image>> images = new ArrayList<>();
|
||||
|
||||
for (Image image : repository.findAll()) {
|
||||
EntityModel<Image> imageResource = EntityModel.of(
|
||||
image,
|
||||
linkTo(methodOn(ImageController.class).getImage(image.getId())).withSelfRel(),
|
||||
linkTo(methodOn(ImageController.class).updateImage(image.getId(), new Image())).withRel("Update Image"),
|
||||
linkTo(methodOn(ImageController.class).deleteImage(image.getId())).withRel("Delete Image")
|
||||
);
|
||||
images.add(imageResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
images,
|
||||
linkTo(methodOn(ImageController.class).addImage(new Image())).withRel("Add Image")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Image> getImage(@PathVariable Long id) {
|
||||
Image image = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Image not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
image,
|
||||
linkTo(methodOn(ImageController.class).updateImage(image.getId(), new Image())).withRel("Update Image"),
|
||||
linkTo(methodOn(ImageController.class).deleteImage(image.getId())).withRel("Delete Image"),
|
||||
linkTo(methodOn(ImageController.class).getImages()).withRel("Images")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Image> addImage(@RequestBody Image image) {
|
||||
repository.save(image);
|
||||
return EntityModel.of(
|
||||
image,
|
||||
linkTo(methodOn(ImageController.class).getImage(image.getId())).withSelfRel(),
|
||||
linkTo(methodOn(ImageController.class).updateImage(image.getId(), new Image())).withRel("Update Image"),
|
||||
linkTo(methodOn(ImageController.class).deleteImage(image.getId())).withRel("Delete Image"),
|
||||
linkTo(methodOn(ImageController.class).getImages()).withRel("Images")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Image> updateImage(@PathVariable Long id,
|
||||
@RequestBody Image update) {
|
||||
Image image = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Image not found"));
|
||||
|
||||
if (update.getPath() != null && !update.getPath().isEmpty()) {
|
||||
image.setPath(update.getPath());
|
||||
}
|
||||
|
||||
repository.save(image);
|
||||
|
||||
return EntityModel.of(
|
||||
image,
|
||||
linkTo(methodOn(ImageController.class).getImage(id)).withSelfRel(),
|
||||
linkTo(methodOn(ImageController.class).deleteImage(id)).withRel("Delete Image"),
|
||||
linkTo(methodOn(ImageController.class).getImages()).withRel("Images")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("delete/{id}")
|
||||
public @ResponseBody EntityModel<Image> deleteImage(@PathVariable Long id) {
|
||||
Image image = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Image not found"));
|
||||
|
||||
repository.delete(image);
|
||||
|
||||
return EntityModel.of(
|
||||
image,
|
||||
linkTo(methodOn(ImageController.class).getImages()).withRel("Images")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Question;
|
||||
import com.example.wtf.repository.QuestionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.CollectionModel;
|
||||
import org.springframework.hateoas.EntityModel;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/questions")
|
||||
public class QuestionController {
|
||||
@Autowired
|
||||
private QuestionRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Question>> getQuestions() {
|
||||
List<EntityModel<Question>> questions = new ArrayList<>();
|
||||
|
||||
for (Question question : repository.findAll()) {
|
||||
EntityModel<Question> questionResource = EntityModel.of(
|
||||
question,
|
||||
linkTo(methodOn(QuestionController.class).getQuestion(question.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuestionController.class).updateQuestion(question.getId(), new Question())).withRel("Update Question"),
|
||||
linkTo(methodOn(QuestionController.class).deleteQuestion(question.getId())).withRel("Delete Question")
|
||||
);
|
||||
questions.add(questionResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
questions,
|
||||
linkTo(methodOn(QuestionController.class).addQuestion(new Question())).withRel("Add Question")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Question> getQuestion(@PathVariable Long id) {
|
||||
Question question = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Question not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
question,
|
||||
linkTo(methodOn(QuestionController.class).updateQuestion(question.getId(), new Question())).withRel("Update Question"),
|
||||
linkTo(methodOn(QuestionController.class).deleteQuestion(question.getId())).withRel("Delete Question"),
|
||||
linkTo(methodOn(QuestionController.class).getQuestions()).withRel("Questions")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Question> addQuestion(@RequestBody Question question) {
|
||||
repository.save(question);
|
||||
return EntityModel.of(
|
||||
question,
|
||||
linkTo(methodOn(QuestionController.class).getQuestion(question.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuestionController.class).updateQuestion(question.getId(), new Question())).withRel("Update Question"),
|
||||
linkTo(methodOn(QuestionController.class).deleteQuestion(question.getId())).withRel("Delete Question"),
|
||||
linkTo(methodOn(QuestionController.class).getQuestions()).withRel("Questions")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Question> updateQuestion(@PathVariable Long id,
|
||||
@RequestBody Question update) {
|
||||
Question question = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Question not found"));
|
||||
|
||||
if (update.getContent() != null && !update.getContent().isEmpty()) {
|
||||
question.setContent(update.getContent());
|
||||
}
|
||||
if (update.getAnswerA() != null && !update.getAnswerA().isEmpty()) {
|
||||
question.setAnswerA(update.getAnswerA());
|
||||
}
|
||||
if (update.getAnswerB() != null && !update.getAnswerB().isEmpty()) {
|
||||
question.setAnswerB(update.getAnswerB());
|
||||
}
|
||||
if (update.getAnswerC() != null && !update.getAnswerC().isEmpty()) {
|
||||
question.setAnswerC(update.getAnswerC());
|
||||
}
|
||||
if (update.getAnswerD() != null && !update.getAnswerD().isEmpty()) {
|
||||
question.setAnswerD(update.getAnswerD());
|
||||
}
|
||||
if (update.getcAnswer() != null && !update.getcAnswer().isEmpty()) {
|
||||
question.setcAnswer(update.getcAnswer());
|
||||
}
|
||||
|
||||
repository.save(question);
|
||||
|
||||
return EntityModel.of(
|
||||
question,
|
||||
linkTo(methodOn(QuestionController.class).getQuestion(id)).withSelfRel(),
|
||||
linkTo(methodOn(QuestionController.class).deleteQuestion(id)).withRel("Delete Question"),
|
||||
linkTo(methodOn(QuestionController.class).getQuestions()).withRel("Questions")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("delete/{id}")
|
||||
public @ResponseBody EntityModel<Question> deleteQuestion(@PathVariable Long id) {
|
||||
Question question = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Question not found"));
|
||||
|
||||
repository.delete(question);
|
||||
|
||||
return EntityModel.of(
|
||||
question,
|
||||
linkTo(methodOn(QuestionController.class).getQuestions()).withRel("Questions")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Quiz;
|
||||
import com.example.wtf.repository.QuizRepository;
|
||||
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("/quizzes")
|
||||
public class QuizController {
|
||||
|
||||
@Autowired
|
||||
private QuizRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Quiz>> getQuizzes() {
|
||||
List<EntityModel<Quiz>> quizzes = new ArrayList<>();
|
||||
|
||||
for (Quiz quiz : repository.findAll()) {
|
||||
EntityModel<Quiz> quizResource = EntityModel.of(
|
||||
quiz,
|
||||
linkTo(methodOn(QuizController.class).getQuiz(quiz.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuizController.class).deleteQuiz(quiz.getId())).withRel("Delete Quiz")
|
||||
);
|
||||
quizzes.add(quizResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
quizzes,
|
||||
linkTo(methodOn(QuizController.class).addQuiz(new Quiz())).withRel("Add Quiz")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Quiz> getQuiz(@PathVariable Long id) {
|
||||
Quiz quiz = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quiz not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
quiz,
|
||||
linkTo(methodOn(QuizController.class).deleteQuiz(quiz.getId())).withRel("Delete Quiz"),
|
||||
linkTo(methodOn(QuizController.class).getQuizzes()).withRel("Quizzes")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Quiz> addQuiz(@RequestBody Quiz quiz) {
|
||||
repository.save(quiz);
|
||||
return EntityModel.of(
|
||||
quiz,
|
||||
linkTo(methodOn(QuizController.class).getQuiz(quiz.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuizController.class).deleteQuiz(quiz.getId())).withRel("Delete Quiz"),
|
||||
linkTo(methodOn(QuizController.class).getQuizzes()).withRel("Quizzes")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Quiz> updateQuiz(@PathVariable Long id, @RequestBody Quiz update) {
|
||||
Quiz quiz = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quiz not found"));
|
||||
|
||||
if (update.getTitle() != null && !update.getTitle().isEmpty()) {
|
||||
quiz.setTitle(update.getTitle());
|
||||
}
|
||||
if (update.getNbQuest() != null) {
|
||||
quiz.setNbQuest(update.getNbQuest());
|
||||
}
|
||||
if (update.getImage() != null) {
|
||||
quiz.setImage(update.getImage());
|
||||
}
|
||||
|
||||
repository.save(quiz);
|
||||
|
||||
return EntityModel.of(
|
||||
quiz,
|
||||
linkTo(methodOn(QuizController.class).updateQuiz(id, new Quiz())).withSelfRel()
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public @ResponseBody EntityModel<Quiz> deleteQuiz(@PathVariable Long id) {
|
||||
Quiz quiz = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quiz not found"));
|
||||
|
||||
repository.delete(quiz);
|
||||
|
||||
return EntityModel.of(
|
||||
quiz,
|
||||
linkTo(methodOn(QuizController.class).getQuizzes()).withRel("Quizzes")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceForbidden;
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Quote;
|
||||
import com.example.wtf.repository.QuoteRepository;
|
||||
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("/quotes")
|
||||
public class QuoteController {
|
||||
|
||||
@Autowired
|
||||
private QuoteRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Quote>> getQuotes() {
|
||||
List<EntityModel<Quote>> quotes = new ArrayList<>();
|
||||
|
||||
for (Quote quote : repository.findAll()) {
|
||||
EntityModel<Quote> quoteResource = EntityModel.of(
|
||||
quote,
|
||||
linkTo(methodOn(QuoteController.class).getQuote(quote.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuoteController.class).updateQuote(quote.getId(), new Quote())).withRel("Update quote"),
|
||||
linkTo(methodOn(QuoteController.class).deleteQuote(quote.getId())).withRel("Delete quote")
|
||||
);
|
||||
quotes.add(quoteResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
quotes,
|
||||
linkTo(methodOn(QuoteController.class).addQuote(new Quote())).withRel("Add Quote")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Quote> getQuote(@PathVariable Long id) {
|
||||
Quote quote = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quote not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
quote,
|
||||
linkTo(methodOn(QuoteController.class).updateQuote(quote.getId(), new Quote())).withRel("Update quote"),
|
||||
linkTo(methodOn(QuoteController.class).deleteQuote(quote.getId())).withRel("Delete quote"),
|
||||
linkTo(methodOn(QuoteController.class).getQuotes()).withRel("Quotes")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Quote> addQuote(@RequestBody Quote quote) {
|
||||
|
||||
repository.save(quote);
|
||||
|
||||
return EntityModel.of(
|
||||
quote,
|
||||
linkTo(methodOn(QuoteController.class).getQuote(quote.getId())).withSelfRel(),
|
||||
linkTo(methodOn(QuoteController.class).updateQuote(quote.getId(), new Quote())).withRel("Update quote"),
|
||||
linkTo(methodOn(QuoteController.class).deleteQuote(quote.getId())).withRel("Delete quote"),
|
||||
linkTo(methodOn(QuoteController.class).getQuotes()).withRel("Quotes")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Quote> updateQuote(@PathVariable Long id,
|
||||
@RequestBody Quote update) {
|
||||
Quote quote = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quote not found"));
|
||||
|
||||
if (update.getContent() != null && !update.getContent().isEmpty()) {
|
||||
quote.setContent(update.getContent());
|
||||
}
|
||||
if (update.getLikes() != null) {
|
||||
quote.setLikes(update.getLikes());
|
||||
}
|
||||
if (update.getLanguage() != null && !update.getLanguage().isEmpty()) {
|
||||
quote.setLanguage(update.getLanguage());
|
||||
}
|
||||
if (update.getIsValid() != null) {
|
||||
quote.setIsValid(update.getIsValid());
|
||||
}
|
||||
if (update.getReason() != null && !update.getReason().isEmpty()) {
|
||||
quote.setReason(update.getReason());
|
||||
}
|
||||
|
||||
repository.save(quote);
|
||||
|
||||
return EntityModel.of(
|
||||
quote,
|
||||
linkTo(methodOn(QuoteController.class).getQuote(id)).withSelfRel(),
|
||||
linkTo(methodOn(QuoteController.class).deleteQuote(id)).withRel("Delete quote"),
|
||||
linkTo(methodOn(QuoteController.class).getQuotes()).withRel("Quotes")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public @ResponseBody EntityModel<Quote> deleteQuote(@PathVariable Long id) {
|
||||
Quote quote = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Quote not found"));
|
||||
|
||||
repository.delete(quote);
|
||||
|
||||
return EntityModel.of(
|
||||
quote,
|
||||
linkTo(methodOn(QuoteController.class).getQuotes()).withRel("Quotes")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceForbidden;
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Source;
|
||||
import com.example.wtf.repository.SourceRepository;
|
||||
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.time.LocalDate;
|
||||
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("/sources")
|
||||
public class SourceController {
|
||||
|
||||
@Autowired
|
||||
private SourceRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<Source>> getSources() {
|
||||
List<EntityModel<Source>> sources = new ArrayList<>();
|
||||
|
||||
for (Source source : repository.findAll()) {
|
||||
EntityModel<Source> sourceResource = EntityModel.of(
|
||||
source,
|
||||
linkTo(methodOn(SourceController.class).getSource(source.getId())).withSelfRel(),
|
||||
linkTo(methodOn(SourceController.class).updateSource(source.getId(), new Source())).withRel("Update source"),
|
||||
linkTo(methodOn(SourceController.class).deleteSource(source.getId())).withRel("Delete source")
|
||||
);
|
||||
sources.add(sourceResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
sources,
|
||||
linkTo(methodOn(SourceController.class).addSource(new Source())).withRel("Add Source")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<Source> getSource(@PathVariable Long id) {
|
||||
Source source = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Source not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
source,
|
||||
linkTo(methodOn(SourceController.class).updateSource(source.getId(), new Source())).withRel("Update source"),
|
||||
linkTo(methodOn(SourceController.class).deleteSource(source.getId())).withRel("Delete source"),
|
||||
linkTo(methodOn(SourceController.class).getSources()).withRel("Sources")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<Source> addSource(@RequestBody Source source) {
|
||||
|
||||
if (repository.existsByTitle(source.getTitle())) {
|
||||
throw new ResourceForbidden("Source forbidden");
|
||||
}
|
||||
|
||||
repository.save(source);
|
||||
|
||||
return EntityModel.of(
|
||||
source,
|
||||
linkTo(methodOn(SourceController.class).getSource(source.getId())).withSelfRel(),
|
||||
linkTo(methodOn(SourceController.class).updateSource(source.getId(), new Source())).withRel("Update source"),
|
||||
linkTo(methodOn(SourceController.class).deleteSource(source.getId())).withRel("Delete source"),
|
||||
linkTo(methodOn(SourceController.class).getSources()).withRel("Sources")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<Source> updateSource(@PathVariable Long id,
|
||||
@RequestBody Source update) {
|
||||
Source source = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Source not found"));
|
||||
|
||||
if (update.getTitle() != null && !update.getTitle().isEmpty()) {
|
||||
if (repository.existsByTitle(source.getTitle())) {
|
||||
throw new ResourceForbidden("Source forbidden");
|
||||
}
|
||||
source.setTitle(update.getTitle());
|
||||
}
|
||||
if (update.getDate() != null) {
|
||||
source.setDate(update.getDate());
|
||||
}
|
||||
|
||||
repository.save(source);
|
||||
|
||||
return EntityModel.of(
|
||||
source,
|
||||
linkTo(methodOn(SourceController.class).getSource(id)).withSelfRel(),
|
||||
linkTo(methodOn(SourceController.class).deleteSource(id)).withRel("Delete source"),
|
||||
linkTo(methodOn(SourceController.class).getSources()).withRel("Sources")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("delete/{id}")
|
||||
public @ResponseBody EntityModel<Source> deleteSource(@PathVariable Long id) {
|
||||
Source source = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("Source not found"));
|
||||
|
||||
repository.delete(source);
|
||||
|
||||
return EntityModel.of(
|
||||
source,
|
||||
linkTo(methodOn(SourceController.class).getSources()).withRel("Sources")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
package com.example.wtf.controller;
|
||||
|
||||
import com.example.wtf.exception.ResourceForbidden;
|
||||
import com.example.wtf.exception.ResourceNotFound;
|
||||
import com.example.wtf.model.Image;
|
||||
import com.example.wtf.model.User;
|
||||
import com.example.wtf.repository.UserRepository;
|
||||
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.time.LocalDate;
|
||||
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("/users")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
@GetMapping("/get")
|
||||
public @ResponseBody CollectionModel<EntityModel<User>> getUsers() {
|
||||
List<EntityModel<User>> users = new ArrayList<>();
|
||||
|
||||
for (User user : repository.findAll()) {
|
||||
EntityModel<User> userResource = EntityModel.of(
|
||||
user,
|
||||
linkTo(methodOn(UserController.class).getUser(user.getId())).withSelfRel(),
|
||||
linkTo(methodOn(UserController.class).updateUser(user.getId(), new User())).withRel("Update user"),
|
||||
linkTo(methodOn(UserController.class).deleteUser(user.getId())).withRel("Delete user")
|
||||
);
|
||||
users.add(userResource);
|
||||
}
|
||||
|
||||
return CollectionModel.of(
|
||||
users,
|
||||
linkTo(methodOn(UserController.class).addUser(new User())).withRel("Add User")
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public @ResponseBody EntityModel<User> getUser(@PathVariable Long id) {
|
||||
User user = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("User not found"));
|
||||
|
||||
return EntityModel.of(
|
||||
user,
|
||||
linkTo(methodOn(UserController.class).updateUser(user.getId(), new User())).withRel("Update user"),
|
||||
linkTo(methodOn(UserController.class).deleteUser(user.getId())).withRel("Delete user"),
|
||||
linkTo(methodOn(UserController.class).getUsers()).withRel("Users")
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public @ResponseBody EntityModel<User> addUser(@RequestBody User user) {
|
||||
|
||||
if (repository.existsByUsername(user.getUsername())) {
|
||||
throw new ResourceForbidden("Username forbidden");
|
||||
}
|
||||
if (repository.existsByEmail(user.getEmail())) {
|
||||
throw new ResourceForbidden("Email forbidden");
|
||||
}
|
||||
|
||||
if (user.getImage() == null) {
|
||||
user.setImage(new Image("/default/path"));
|
||||
}
|
||||
if (user.getCreation() == null) {
|
||||
user.setCreation(LocalDate.now());
|
||||
}
|
||||
|
||||
repository.save(user);
|
||||
|
||||
return EntityModel.of(
|
||||
user,
|
||||
linkTo(methodOn(UserController.class).getUser(user.getId())).withSelfRel(),
|
||||
linkTo(methodOn(UserController.class).updateUser(user.getId(), new User())).withRel("Update user"),
|
||||
linkTo(methodOn(UserController.class).deleteUser(user.getId())).withRel("Delete user"),
|
||||
linkTo(methodOn(UserController.class).getUsers()).withRel("Users")
|
||||
);
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
public @ResponseBody EntityModel<User> updateUser(@PathVariable Long id,
|
||||
@RequestBody User update) {
|
||||
User user = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("User not found"));
|
||||
|
||||
if (update.getUsername() != null && !update.getUsername().isEmpty()) {
|
||||
if (repository.existsByUsername(user.getUsername())) {
|
||||
throw new ResourceForbidden("Username forbidden");
|
||||
}
|
||||
user.setUsername(update.getUsername());
|
||||
}
|
||||
if (update.getEmail() != null && !update.getEmail().isEmpty()) {
|
||||
if (repository.existsByEmail(user.getEmail())) {
|
||||
throw new ResourceForbidden("Email forbidden");
|
||||
}
|
||||
user.setEmail(update.getEmail());
|
||||
}
|
||||
if (update.getPassword() != null && !update.getPassword().isEmpty()) {
|
||||
user.setPassword(update.getPassword());
|
||||
}
|
||||
if (update.getImage() != null) {
|
||||
user.setImage(update.getImage());
|
||||
}
|
||||
if (update.getCreation() != null) {
|
||||
user.setCreation(update.getCreation());
|
||||
}
|
||||
|
||||
repository.save(user);
|
||||
|
||||
return EntityModel.of(
|
||||
user,
|
||||
linkTo(methodOn(UserController.class).getUser(id)).withSelfRel(),
|
||||
linkTo(methodOn(UserController.class).deleteUser(id)).withRel("Delete user"),
|
||||
linkTo(methodOn(UserController.class).getUsers()).withRel("Users")
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("delete/{id}")
|
||||
public @ResponseBody EntityModel<User> deleteUser(@PathVariable Long id) {
|
||||
User user = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFound("User not found"));
|
||||
|
||||
repository.delete(user);
|
||||
|
||||
return EntityModel.of(
|
||||
user,
|
||||
linkTo(methodOn(UserController.class).getUsers()).withRel("Users")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.example.wtf.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;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<String> handleAllExceptions(Exception ex) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred : " + ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(NullPointerException.class)
|
||||
public ResponseEntity<String> handleNullPointerException(NullPointerException ex) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null pointer exception caught : " + ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<String> handleResourceNotFoundException(ResourceNotFoundException ex) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found : " + ex.getMessage());
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.wtf.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||
public class ResourceForbidden extends RuntimeException {
|
||||
public ResourceForbidden(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.wtf.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public class ResourceNotFound extends RuntimeException {
|
||||
public ResourceNotFound(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Character {
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Image image;
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public Image getImage() { return image; }
|
||||
public void setImage(Image image) { this.image = image; }
|
||||
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
@Entity
|
||||
public class Commentary {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private User user;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Quote quote;
|
||||
|
||||
private String comment;
|
||||
|
||||
private LocalDateTime dateC;
|
||||
|
||||
public Commentary() {
|
||||
}
|
||||
|
||||
public Commentary(User user, Quote quote, String comment, LocalDateTime dateC) {
|
||||
this.user = user;
|
||||
this.quote = quote;
|
||||
this.comment = comment;
|
||||
this.dateC = dateC;
|
||||
}
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long 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;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public LocalDateTime getDateC() {
|
||||
return dateC;
|
||||
}
|
||||
|
||||
public void setDateC(LocalDateTime dateC) {
|
||||
this.dateC = dateC;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
|
||||
public class Favorite {
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private User user;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Quote quote;
|
||||
|
||||
public Favorite() {}
|
||||
|
||||
public Favorite(User user, Quote quote) {
|
||||
this.user = user;
|
||||
this.quote = quote;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long 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,29 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Image {
|
||||
|
||||
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String path;
|
||||
|
||||
public Image(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getPath() { return path; }
|
||||
public void setPath(String path) { this.path = path; }
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.*;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Question {
|
||||
|
||||
@GeneratedValue(strategy= GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String content;
|
||||
private String answerA;
|
||||
private String answerB;
|
||||
private String answerC;
|
||||
private String answerD;
|
||||
private String cAnswer;
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getContent() { return content; }
|
||||
public void setContent(String content) { this.content = content; }
|
||||
|
||||
public String getAnswerA() { return answerA; }
|
||||
public void setAnswerA(String answerA) { this.answerA = answerA; }
|
||||
|
||||
public String getAnswerB() { return answerB; }
|
||||
public void setAnswerB(String answerB) { this.answerB = answerB; }
|
||||
|
||||
public String getAnswerC() { return answerC; }
|
||||
public void setAnswerC(String answerC) { this.answerC = answerC; }
|
||||
|
||||
public String getAnswerD() { return answerD; }
|
||||
public void setAnswerD(String answerD) { this.answerD = answerD; }
|
||||
|
||||
public String getcAnswer() { return cAnswer; }
|
||||
public void setcAnswer(String cAnswer) { this.cAnswer = cAnswer; }
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Quiz {
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private Integer nbQuest;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Image image;
|
||||
|
||||
public Quiz() {
|
||||
}
|
||||
|
||||
public Quiz(String title, Integer nbQuest, Image image) {
|
||||
this.title = title;
|
||||
this.nbQuest = nbQuest;
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
|
||||
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 getNbQuest() {
|
||||
return nbQuest;
|
||||
}
|
||||
|
||||
public void setNbQuest(Integer nbQuest) {
|
||||
this.nbQuest = nbQuest;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(Image image) {
|
||||
this.image = image;
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Quote {
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String content;
|
||||
|
||||
private Integer likes;
|
||||
|
||||
private String language;
|
||||
|
||||
private Boolean isValid;
|
||||
|
||||
private String reason;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Character character;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Source source;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private User userVerif;
|
||||
|
||||
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 getLanguage() { return language; }
|
||||
public void setLanguage(String language) { this.language = language; }
|
||||
|
||||
public Boolean getIsValid() { return isValid; }
|
||||
public void setIsValid(Boolean isValid) { this.isValid = isValid; }
|
||||
|
||||
public String getReason() { return reason; }
|
||||
public void setReason(String reason) { this.reason = reason; }
|
||||
|
||||
public Character getCharacter() { return character; }
|
||||
public void setCharacter(Character character) { this.character = character; }
|
||||
|
||||
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,47 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Source {
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private @Id Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
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 LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +1,49 @@
|
||||
package com.example.wfwebapi.model;
|
||||
package com.example.wtf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Users", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "app_user")
|
||||
public class User {
|
||||
@Id
|
||||
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id_user")
|
||||
private Long id;
|
||||
private @Id Long id;
|
||||
|
||||
@Column(name = "username", nullable = false, length = 50)
|
||||
private String username;
|
||||
|
||||
@Column(name = "email", nullable = false, length = 50)
|
||||
private String email;
|
||||
|
||||
@Column(name = "password", nullable = false, length = 100)
|
||||
private String password;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "img", nullable = false)
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
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,10 @@
|
||||
package com.example.wtf.repository;
|
||||
import com.example.wtf.model.Character;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CharacterRepository extends CrudRepository<Character, Long> {
|
||||
Optional<Character> findById(Long id);
|
||||
boolean existsByName(String name);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Commentary;
|
||||
import com.example.wtf.model.Image;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CommentaryRepository extends CrudRepository<Commentary, Long> {
|
||||
Optional<Commentary> findById(Long id);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Commentary;
|
||||
import com.example.wtf.model.Favorite;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface FavoriteRepository extends CrudRepository<Favorite, Long> {
|
||||
Optional<Favorite> findById(Long id);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Image;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface ImageRepository extends CrudRepository<Image, Long> {
|
||||
Optional<Image> findById(Long id);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Question;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface QuestionRepository extends CrudRepository<Question, Long> {
|
||||
Optional<Question> findById(Long id);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Commentary;
|
||||
import com.example.wtf.model.Quiz;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface QuizRepository extends CrudRepository<Quiz, Long> {
|
||||
Optional<Quiz> findById(Long id);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Quote;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface QuoteRepository extends CrudRepository<Quote, Long> {
|
||||
Optional<Quote> findById(Long id);
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.Source;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface SourceRepository extends CrudRepository<Source, Long> {
|
||||
Optional<Source> findById(Long id);
|
||||
boolean existsByTitle(String title);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.example.wtf.repository;
|
||||
|
||||
import com.example.wtf.model.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
Optional<User> findById(Long id);
|
||||
boolean existsByUsername(String username);
|
||||
boolean existsByEmail(String email);
|
||||
}
|
@ -0,0 +1 @@
|
||||
spring.application.name=wtf
|
@ -1,13 +1,13 @@
|
||||
package com.example.wfwebapi;
|
||||
package com.example.wtf;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class WfWebapiApplicationTests {
|
||||
class WtfApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue