Skip to content

Commit

Permalink
basic crud of NivelEscolar
Browse files Browse the repository at this point in the history
  • Loading branch information
ailtonbsj committed Aug 26, 2022
1 parent c6d66c3 commit 21d0931
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/main/java/ailtonbsj/sauteweb/sauteapi/model/NivelEscolar.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<NivelEscolar, Long> {
}
Original file line number Diff line number Diff line change
@@ -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<NivelEscolar> 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);
}

}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 21d0931

Please sign in to comment.