parent
b6e2601b90
commit
546cb5dfc4
@ -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,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<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,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,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,4 @@
|
||||
package com.example.wtf.model;
|
||||
|
||||
public class Quote {
|
||||
}
|
@ -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,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);
|
||||
}
|
Loading…
Reference in new issue