diff --git a/wtf/src/main/java/com/example/wtf/controller/CommentaryController.java b/wtf/src/main/java/com/example/wtf/controller/CommentaryController.java new file mode 100644 index 0000000..db20272 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/controller/CommentaryController.java @@ -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> getCommentaries() { + List> commentaries = new ArrayList<>(); + + for (Commentary commentary : repository.findAll()) { + EntityModel 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 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 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 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 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") + ); + } +} diff --git a/wtf/src/main/java/com/example/wtf/controller/FavoriteController.java b/wtf/src/main/java/com/example/wtf/controller/FavoriteController.java new file mode 100644 index 0000000..441a109 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/controller/FavoriteController.java @@ -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> getFavorites() { + List> favorites = new ArrayList<>(); + + for (Favorite favorite : repository.findAll()) { + EntityModel 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 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 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 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 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") + ); + } +} diff --git a/wtf/src/main/java/com/example/wtf/controller/QuizController.java b/wtf/src/main/java/com/example/wtf/controller/QuizController.java new file mode 100644 index 0000000..63b9a24 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/controller/QuizController.java @@ -0,0 +1,100 @@ +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> getQuizzes() { + List> quizzes = new ArrayList<>(); + + for (Quiz quiz : repository.findAll()) { + EntityModel 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 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 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 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 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") + ); + } +} diff --git a/wtf/src/main/java/com/example/wtf/model/Commentary.java b/wtf/src/main/java/com/example/wtf/model/Commentary.java new file mode 100644 index 0000000..e722916 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/model/Commentary.java @@ -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; + } +} diff --git a/wtf/src/main/java/com/example/wtf/model/Favorite.java b/wtf/src/main/java/com/example/wtf/model/Favorite.java new file mode 100644 index 0000000..56d7b77 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/model/Favorite.java @@ -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; + } +} + diff --git a/wtf/src/main/java/com/example/wtf/model/Quiz.java b/wtf/src/main/java/com/example/wtf/model/Quiz.java new file mode 100644 index 0000000..afd18ef --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/model/Quiz.java @@ -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; + } +} diff --git a/wtf/src/main/java/com/example/wtf/model/Quote.java b/wtf/src/main/java/com/example/wtf/model/Quote.java new file mode 100644 index 0000000..1f1a7a0 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/model/Quote.java @@ -0,0 +1,4 @@ +package com.example.wtf.model; + +public class Quote { +} diff --git a/wtf/src/main/java/com/example/wtf/repository/CommentaryRepository.java b/wtf/src/main/java/com/example/wtf/repository/CommentaryRepository.java new file mode 100644 index 0000000..03cd5ba --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/repository/CommentaryRepository.java @@ -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 { + Optional findById(Long id); +} \ No newline at end of file diff --git a/wtf/src/main/java/com/example/wtf/repository/FavoriteRepository.java b/wtf/src/main/java/com/example/wtf/repository/FavoriteRepository.java new file mode 100644 index 0000000..2e7e0e2 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/repository/FavoriteRepository.java @@ -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 { + Optional findById(Long id); +} \ No newline at end of file diff --git a/wtf/src/main/java/com/example/wtf/repository/QuizRepository.java b/wtf/src/main/java/com/example/wtf/repository/QuizRepository.java new file mode 100644 index 0000000..b63b4ee --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/repository/QuizRepository.java @@ -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 { + Optional findById(Long id); +} \ No newline at end of file