diff --git a/wtf/src/main/java/com/example/wtf/controller/QuizController.java b/wtf/src/main/java/com/example/wtf/controller/QuizController.java index 63b9a24..18b23a5 100644 --- a/wtf/src/main/java/com/example/wtf/controller/QuizController.java +++ b/wtf/src/main/java/com/example/wtf/controller/QuizController.java @@ -17,6 +17,7 @@ import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; @RestController @RequestMapping("/quizzes") public class QuizController { + @Autowired private QuizRepository repository; diff --git a/wtf/src/main/java/com/example/wtf/controller/QuoteController.java b/wtf/src/main/java/com/example/wtf/controller/QuoteController.java new file mode 100644 index 0000000..3f8a25d --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/controller/QuoteController.java @@ -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> getQuotes() { + List> quotes = new ArrayList<>(); + + for (Quote quote : repository.findAll()) { + EntityModel 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 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 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 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 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") + ); + } +} \ No newline at end of file diff --git a/wtf/src/main/java/com/example/wtf/controller/SourceController.java b/wtf/src/main/java/com/example/wtf/controller/SourceController.java new file mode 100644 index 0000000..62fd757 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/controller/SourceController.java @@ -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> getSources() { + List> sources = new ArrayList<>(); + + for (Source source : repository.findAll()) { + EntityModel 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 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 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 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 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") + ); + } +} \ No newline at end of file diff --git a/wtf/src/main/java/com/example/wtf/model/Quote.java b/wtf/src/main/java/com/example/wtf/model/Quote.java index 1f1a7a0..34b8e49 100644 --- a/wtf/src/main/java/com/example/wtf/model/Quote.java +++ b/wtf/src/main/java/com/example/wtf/model/Quote.java @@ -1,4 +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; } + } diff --git a/wtf/src/main/java/com/example/wtf/model/Source.java b/wtf/src/main/java/com/example/wtf/model/Source.java new file mode 100644 index 0000000..d0cbda7 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/model/Source.java @@ -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; + } + +} \ No newline at end of file diff --git a/wtf/src/main/java/com/example/wtf/repository/QuoteRepository.java b/wtf/src/main/java/com/example/wtf/repository/QuoteRepository.java new file mode 100644 index 0000000..90faae2 --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/repository/QuoteRepository.java @@ -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 { + Optional findById(Long id); +} diff --git a/wtf/src/main/java/com/example/wtf/repository/SourceRepository.java b/wtf/src/main/java/com/example/wtf/repository/SourceRepository.java new file mode 100644 index 0000000..3917f2a --- /dev/null +++ b/wtf/src/main/java/com/example/wtf/repository/SourceRepository.java @@ -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 { + Optional findById(Long id); + boolean existsByTitle(String title); +} diff --git a/wtf/src/main/java/com/example/wtf/repository/UserRepository.java b/wtf/src/main/java/com/example/wtf/repository/UserRepository.java index 72c8633..95802cb 100644 --- a/wtf/src/main/java/com/example/wtf/repository/UserRepository.java +++ b/wtf/src/main/java/com/example/wtf/repository/UserRepository.java @@ -10,6 +10,5 @@ import java.util.Optional; public interface UserRepository extends CrudRepository { Optional findById(Long id); boolean existsByUsername(String username); - boolean existsByEmail(String email); }