Last active
September 20, 2018 14:20
-
-
Save falehenrique/f4b51bf5e53e689a87b8bb4069bcf907 to your computer and use it in GitHub Desktop.
Spring MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class AulaServiceImpl implements AulaService { | |
@Autowired | |
private ProfessorRepository professorRepository; | |
@Override | |
public Professor createAula(ProfessorDomain professorDomain) { | |
Professor professorEntity = new Professor(); | |
professorEntity.setNome(professorDomain.getNome()); | |
professorEntity.setIdade(professorDomain.getIdade()); | |
final List<Aluno> listAlunos = professorDomain.getAlunos().stream() | |
.map( | |
aluno -> { | |
Aluno a = new Aluno(); | |
a.setIdade(aluno.getIdade()); | |
a.setNome(aluno.getNome()); | |
a.setProfessor(professorEntity); | |
return a; | |
} | |
) | |
.collect(Collectors.toList()); | |
professorEntity.setAlunos(listAlunos); | |
Professor ret = professorRepository.save(professorEntity); | |
return ret; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface AulaService { | |
public Professor createAula(ProfessorDomain professorDomain); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AlunoDomain { | |
@JsonProperty("nome") | |
private String nome; | |
@JsonProperty("idade") | |
private String idade; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getIdade() { | |
return idade; | |
} | |
public void setIdade(String idade) { | |
this.idade = idade; | |
} | |
} | |
public class ProfessorDomain { | |
@JsonProperty("nome") | |
private String nome; | |
@JsonProperty("idade") | |
private String idade; | |
@JsonProperty("alunos") | |
private List<AlunoDomain> alunos; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getIdade() { | |
return idade; | |
} | |
public void setIdade(String idade) { | |
this.idade = idade; | |
} | |
public List<AlunoDomain> getAlunos() { | |
return alunos; | |
} | |
public void setAlunos(List<AlunoDomain> alunos) { | |
this.alunos = alunos; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
@Table(name = "Aluno") | |
public class Aluno { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Integer id; | |
@Column(name = "nome") | |
private String nome; | |
@Column(name = "idade") | |
private String idade; | |
@ManyToOne | |
@JoinColumn(name = "id_professor") | |
private Professor professor; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getIdade() { | |
return idade; | |
} | |
public void setIdade(String idade) { | |
this.idade = idade; | |
} | |
public Professor getProfessor() { | |
return professor; | |
} | |
public void setProfessor(Professor professor) { | |
this.professor = professor; | |
} | |
} | |
@Entity | |
@Table(name = "Professor") | |
public class Professor { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Integer id; | |
@Column(name = "nome") | |
private String nome; | |
@Column(name = "idade") | |
private String idade; | |
@OneToMany(mappedBy = "professor", targetEntity = Aluno.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL) | |
private List<Aluno> alunos; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getIdade() { | |
return idade; | |
} | |
public void setIdade(String idade) { | |
this.idade = idade; | |
} | |
public List<Aluno> getAlunos() { | |
return alunos; | |
} | |
public void setAlunos(List<Aluno> alunos) { | |
this.alunos = alunos; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
public class AulaController { | |
@Autowired | |
private AulaService service; | |
@RequestMapping(value = "/aula", method = RequestMethod.POST) | |
public ResponseEntity<List<JSONObject>> postLog(@RequestBody ProfessorDomain professorDomain) { | |
service.createAula(professorDomain); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ProfessorRepository extends CrudRepository<Professor, Integer> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment