diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/model/NivelEscolar.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/model/NivelEscolar.java new file mode 100644 index 0000000..36dd895 --- /dev/null +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/model/NivelEscolar.java @@ -0,0 +1,42 @@ +package ailtonbsj.sauteweb.sauteapi.model; + +import java.time.LocalDateTime; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrePersist; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import lombok.Getter; +import lombok.Setter; + +@Entity +@Getter +@Setter +public class NivelEscolar { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column + private String nivelEscolar; + + @Column + @JsonFormat(pattern = "dd/MM/yyyy HH:mm") + private LocalDateTime createdAt; + + @Column + @JsonFormat(pattern = "dd/MM/yyyy HH:mm") + private LocalDateTime updatedAt; + + @PrePersist + public void beforeSave() { + LocalDateTime now = LocalDateTime.now(); + setCreatedAt(now); + setUpdatedAt(now); + } +} diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/NivelEscolarRepository.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/NivelEscolarRepository.java new file mode 100644 index 0000000..7c23cc4 --- /dev/null +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/repository/NivelEscolarRepository.java @@ -0,0 +1,8 @@ +package ailtonbsj.sauteweb.sauteapi.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import ailtonbsj.sauteweb.sauteapi.model.NivelEscolar; + +public interface NivelEscolarRepository extends JpaRepository { +} diff --git a/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/NivelEscolarController.java b/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/NivelEscolarController.java new file mode 100644 index 0000000..bc1ba0d --- /dev/null +++ b/src/main/java/ailtonbsj/sauteweb/sauteapi/rest/NivelEscolarController.java @@ -0,0 +1,58 @@ +package ailtonbsj.sauteweb.sauteapi.rest; + +import java.time.LocalDateTime; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +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 org.springframework.web.server.ResponseStatusException; + +import ailtonbsj.sauteweb.sauteapi.model.NivelEscolar; +import ailtonbsj.sauteweb.sauteapi.repository.NivelEscolarRepository; + +@RestController +@RequestMapping("/api/nivelescolar") +public class NivelEscolarController { + + @Autowired + private NivelEscolarRepository repository; + + @GetMapping + public List index() { + return repository.findAll(); + } + + @PostMapping + public NivelEscolar store(@RequestBody NivelEscolar nivelEscolar) { + return repository.save(nivelEscolar); + } + + @GetMapping("{id}") + public NivelEscolar show(@PathVariable Long id) { + return repository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND)); + } + + @PatchMapping + public NivelEscolar update(@RequestBody NivelEscolar nivelEscolar) { + NivelEscolar ent = repository.findById(nivelEscolar.getId()).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND)); + ent.setNivelEscolar(nivelEscolar.getNivelEscolar()); + ent.setUpdatedAt(LocalDateTime.now()); + return repository.save(ent); + } + + @DeleteMapping("{id}") + public void destroy(@PathVariable Long id) { + repository.deleteById(id); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..bf21d21 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,12 @@ +spring: + datasource: + url: jdbc:mysql://localhost:3306/saute + username: root + password: YOUR_PASS + jpa: + show-sql: true + hibernate: + ddl-auto: update + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL8Dialect