parent
546cb5dfc4
commit
b639600f76
@ -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")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,63 @@
|
|||||||
package com.example.wtf.model;
|
package com.example.wtf.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class Quote {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in new issue