Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Main APP*/
- package com.jp.pasteleria.pasteleria;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.domain.EntityScan;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- @SpringBootApplication
- @EnableJpaAuditing
- @ComponentScan("com.jp.pasteleria.controller")
- @EntityScan("com.jp.pasteleria.model")
- @EnableJpaRepositories("com.jp.pasteleria.repository")
- public class PasteleriaApplication {
- public static void main(String[] args) {
- SpringApplication.run(PasteleriaApplication.class, args);
- }
- }
- /* Controller */
- package com.jp.pasteleria.controller;
- import java.util.List;
- import javax.validation.Valid;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.jp.pasteleria.exception.ResourceNotFoundException;
- import com.jp.pasteleria.model.User;
- import com.jp.pasteleria.repository.UserRepository;
- @RestController
- @RequestMapping("/api")
- public class UserController {
- // private Logger log = LogManager.getLogger(UserController.class.getName());
- @Autowired
- UserRepository userRepository;
- @GetMapping("/users")
- public List<User> getAllNotes() {
- return userRepository.findAll();
- }
- @PostMapping("/users")
- public User createUser(@Valid @RequestBody User user) {
- return userRepository.save(user);
- }
- @PutMapping("/users/{id}")
- public User updateNote(@PathVariable(value = "id") Long userId, @Valid @RequestBody User userDetails) {
- User user = userRepository.findById(userId)
- .orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
- user.setNombre(userDetails.getNombre());
- user.setApellido(userDetails.getApellido());
- user.setEdad(userDetails.getEdad());
- user.setEmail(userDetails.getEmail());
- User updatedUser = userRepository.save(user);
- return updatedUser;
- }
- }
- /* Model */
- package com.jp.pasteleria.model;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.*;
- import javax.validation.constraints.NotBlank;
- import org.springframework.data.annotation.CreatedDate;
- import org.springframework.data.annotation.LastModifiedDate;
- import org.springframework.data.jpa.domain.support.AuditingEntityListener;
- import org.springframework.lang.Nullable;
- import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
- @Entity
- @Table(name = "users")
- @EntityListeners(AuditingEntityListener.class)
- @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
- public class User implements Serializable{
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @NotBlank
- private String nombre;
- @NotBlank
- private String apellido;
- @Nullable
- private Integer edad;
- @NotBlank
- private String email;
- @Column(name = "created_on", nullable = false, updatable = false)
- @Temporal(TemporalType.TIMESTAMP)
- @CreatedDate
- private Date createdAt;
- @Column(name = "updated_on", nullable = false)
- @Temporal(TemporalType.TIMESTAMP)
- @LastModifiedDate
- private Date updatedAt;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public String getApellido() {
- return apellido;
- }
- public void setApellido(String apellido) {
- this.apellido = apellido;
- }
- public int getEdad() {
- return edad;
- }
- public void setEdad(int edad) {
- this.edad = edad;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Date getCreatedAt() {
- return createdAt;
- }
- public void setCreatedAt(Date createdAt) {
- this.createdAt = createdAt;
- }
- public Date getUpdatedAt() {
- return updatedAt;
- }
- public void setUpdatedAt(Date updatedAt) {
- this.updatedAt = updatedAt;
- }
- }
- /* Repository */
- package com.jp.pasteleria.repository;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import com.jp.pasteleria.model.User;
- @Repository
- public interface UserRepository extends JpaRepository<User, Long>{
- }
- /* application.properties */
- ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
- spring.datasource.url = jdbc:mysql://localhost:3306/pasteleria?useSSL=false
- spring.datasource.username = root
- spring.datasource.password =
- ## Hibernate Properties
- # The SQL dialect makes Hibernate generate better SQL for the chosen database
- spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
- # Hibernate ddl auto (create, create-drop, validate, update)
- spring.jpa.hibernate.ddl-auto = update
Add Comment
Please, Sign In to add comment