``` package com.alkemyApi.app.entity;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name="characters")
public class Character {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id_character;
@Basic
private int age;
private String image;
private String history;
private double weight;
@JsonManagedReference
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name="characters_shooting",
joinColumns = {@JoinColumn(name="id_character", nullable=false)},
inverseJoinColumns= {@JoinColumn(name="id_shooting", nullable=false)})
private List<Shooting> shootingList;
public Character(long id_character, int age, String image, String history, double weight,
List<Shooting> shootingList) {
super();
this.id_character = id_character;
this.age = age;
this.image = image;
this.history = history;
this.weight = weight;
this.shootingList = shootingList;
}
Servicios;
Interfaz
```package
Servicios; Interfaz
package com.alkemyApi.app.service;
import java.util.List;
public interface ICharacterService {
public void saveCharacters(Character character);
public void deleteCharacters(Character character);
public List<Character> findAllCharacters();
public Character findCharactersById(long id);
}
```
clase: }
```package
Clase:
package com.alkemyApi.app.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alkemyApi.app.repository.ICharacter;
@Service
public class CharacterService implements ICharacterService {
@Autowired
private ICharacter characRep;
@Override
public void saveCharacters(Character character) {
characRep.save(character);
}
@Override
public void deleteCharacters(Character character) {
characRep.delete(character);
}
@Override
public List<Character> findAllCharacters() {
List<Character> listCharacters = characRep.findAll();
return listCharacters;
}
@Override
public Character findCharactersById(long id) {
Optional<Character> opt = characRep.findById(id);
Character character = null;
if(opt.isPresent()) {
character = opt.get();
}else {
throw new RuntimeException("Character doesn't exist.");
}
return character;
}
} ```
Controlador:
```
Controlador:
package com.alkemyApi.app.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alkemyApi.app.service.ICharacterService;
@RestController
@RequestMapping(value = "character")
public class CharacterController {
@Autowired
private ICharacterService characServ;
@PostMapping("/save")
public void saveCharacter(@RequestBody Character character) {
characServ.saveCharacters(character);
System.out.println("Character successfully saved");
}
}
```
Repositorio
```
Repositorio
package com.alkemyApi.app.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alkemyApi.app.service.ICharacterService;
@RestController
@RequestMapping(value = "character")
public class CharacterController {
@Autowired
private ICharacterService characServ;
@PostMapping("/save")
public void saveCharacter(@RequestBody Character character) {
characServ.saveCharacters(character);
System.out.println("Character successfully saved");
}
}