Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
feat: add Repository, Service & Controller da classe Chamada
Browse files Browse the repository at this point in the history
Adicionar Repository, Service & Controller da classe Chamada
  • Loading branch information
lucas0headshot committed Mar 20, 2024
1 parent a4390b9 commit 8d37d67
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
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> {
}
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();
}
}
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);
}
}

0 comments on commit 8d37d67

Please sign in to comment.