This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Repository, Service & Controller da classe Chamada
Adicionar Repository, Service & Controller da classe Chamada
- Loading branch information
1 parent
a4390b9
commit 8d37d67
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/senac/gestaocurso/repository/ChamadaRepository.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.senac.gestaocurso.repository; | ||
|
||
|
||
|
||
import com.senac.gestaocurso.models.Chamada; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
|
||
|
||
@Repository | ||
public interface ChamadaRepository extends JpaRepository<Chamada, Long> { | ||
} |
57 changes: 57 additions & 0 deletions
57
backend/src/main/java/com/senac/gestaocurso/resource/ChamadaController.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.senac.gestaocurso.resource; | ||
|
||
|
||
|
||
import com.senac.gestaocurso.models.Chamada; | ||
import com.senac.gestaocurso.service.ChamadaService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import java.awt.print.Pageable; | ||
|
||
|
||
|
||
@RestController | ||
@RequestMapping("api/chamada") | ||
public class ChamadaController extends AbstractController { | ||
@Autowired | ||
private ChamadaService service; | ||
|
||
|
||
|
||
@PostMapping | ||
public ResponseEntity salvar(@RequestBody Chamada chamada) { | ||
return ResponseEntity.ok(service.salvar(chamada)); | ||
} | ||
|
||
|
||
|
||
@GetMapping | ||
public ResponseEntity buscaTodos(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "1") int size) { | ||
Pageable pageable = (Pageable) PageRequest.of(page, size); | ||
Page<Chamada> chamadas = service.buscaTodos(pageable); | ||
return ResponseEntity.ok(chamadas); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity buscaPorId(@RequestParam("id") Long id) { | ||
return ResponseEntity.ok().body(service.buscaPorId(id)); | ||
} | ||
|
||
|
||
|
||
@PostMapping("/{id}") | ||
public ResponseEntity atualizar(@RequestParam("id") Long id, @RequestBody Chamada chamada) { | ||
return ResponseEntity.ok().body(service.alterar(id, chamada)); | ||
} | ||
|
||
|
||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity remover(@RequestParam("id") Long id) { | ||
service.remover(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
backend/src/main/java/com/senac/gestaocurso/service/ChamadaService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.senac.gestaocurso.service; | ||
|
||
|
||
|
||
import com.senac.gestaocurso.models.Chamada; | ||
import com.senac.gestaocurso.repository.ChamadaRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Service; | ||
import java.awt.print.Pageable; | ||
import java.util.Optional; | ||
|
||
|
||
@Service | ||
public class ChamadaService { | ||
@Autowired | ||
private ChamadaRepository repository; | ||
|
||
|
||
|
||
public Chamada salvar(Chamada chamada) { | ||
return repository.save(chamada); | ||
} | ||
|
||
|
||
|
||
public Page<Chamada> buscaTodos(Pageable pageable) { | ||
return repository.findAll((org.springframework.data.domain.Pageable) pageable); | ||
} | ||
|
||
public Chamada buscaPorId(Long id) { | ||
return repository.findById(id).orElse(null); | ||
} | ||
|
||
|
||
|
||
public Chamada alterar(Long id, Chamada chamada) { | ||
Optional<Chamada> alterado = repository.findById(id); | ||
|
||
if (alterado.isPresent()) { | ||
var encontrado = alterado.get(); | ||
|
||
chamada.setFrequencias(encontrado.getFrequencias()); | ||
chamada.setProfessor(encontrado.getProfessor()); | ||
|
||
return repository.save(chamada); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
|
||
public void remover(Long id) { | ||
repository.deleteById(id); | ||
} | ||
} |