parent
53f9a0b8aa
commit
b6e2601b90
@ -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,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,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);
|
||||
}
|
Loading…
Reference in new issue