Skip to content

Commit

Permalink
Implemented Professor entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ailtonbsj committed Aug 29, 2022
1 parent ad1aea6 commit 34e0ce4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ailtonbsj.sauteweb.sauteapi.model;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

import javax.persistence.Column;
Expand All @@ -27,9 +28,9 @@ public class Professor implements Serializable {
@Column(nullable = false)
String nome;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm")
@JsonFormat(pattern = "yyyy-MM-dd")
@Column(nullable = false)
LocalDateTime nascimento;
LocalDate nascimento;

@Column(nullable = false)
String naturalidade;
Expand Down Expand Up @@ -67,7 +68,7 @@ public class Professor implements Serializable {
@Column
String categoriaCNH;

@Column(length = 10240)
@Column(length = 100000)
String foto;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import ailtonbsj.sauteweb.sauteapi.model.Professor;

public interface ProfessorRepository extends JpaRepository<Professor, Long> {

Iterable<Professor> findByNomeContainingIgnoreCase(String professor);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package ailtonbsj.sauteweb.sauteapi.rest;

import java.time.LocalDateTime;
import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import ailtonbsj.sauteweb.sauteapi.model.Professor;
import ailtonbsj.sauteweb.sauteapi.repository.ProfessorRepository;
import ailtonbsj.sauteweb.sauteapi.utils.Utils;

@RestController
@RequestMapping("/api/professor")
Expand All @@ -23,8 +31,8 @@ public class ProfessorController {
private ProfessorRepository rep;

@PostMapping
public Professor save(@RequestBody Professor professor) {
return rep.save(professor);
public Long save(@RequestBody Professor professor) {
return rep.save(professor).getId();
}

@GetMapping("{id}")
Expand All @@ -33,4 +41,27 @@ public Professor findById(@PathVariable Long id) {
() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}

@GetMapping
public Iterable<Professor> index(@RequestParam Optional<String> q) {
if (q.isEmpty())
return rep.findAll();
else
return rep.findByNomeContainingIgnoreCase(q.get());
}

@PatchMapping
public Long update(@RequestBody Professor professor) {
Professor ent = rep.findById(professor.getId()).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
professor.setId(null);
BeanUtils.copyProperties(professor, ent, Utils.getNullPropertyNames(professor));
ent.setUpdatedAt(LocalDateTime.now());
return rep.save(ent).getId();
}

@DeleteMapping("{id}")
public void deleteById(@PathVariable Long id) {
rep.deleteById(id);
}

}
26 changes: 26 additions & 0 deletions src/main/java/ailtonbsj/sauteweb/sauteapi/utils/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ailtonbsj.sauteweb.sauteapi.utils;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class Utils {
/**
* Returns an array of null properties of an object
*/
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<>();
for (java.beans.PropertyDescriptor pd : pds) {
// check if value of this property is null then add it to the collection
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null)
emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
jpa:
show-sql: true
hibernate:
ddl-auto: create
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect

0 comments on commit 34e0ce4

Please sign in to comment.