Guest User

Untitled

a guest
Jul 2nd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. /*Main APP*/
  2.  
  3. package com.jp.pasteleria.pasteleria;
  4.  
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.boot.autoconfigure.domain.EntityScan;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
  10. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  11.  
  12. @SpringBootApplication
  13. @EnableJpaAuditing
  14. @ComponentScan("com.jp.pasteleria.controller")
  15. @EntityScan("com.jp.pasteleria.model")
  16. @EnableJpaRepositories("com.jp.pasteleria.repository")
  17. public class PasteleriaApplication {
  18.  
  19. public static void main(String[] args) {
  20. SpringApplication.run(PasteleriaApplication.class, args);
  21. }
  22. }
  23.  
  24.  
  25. /* Controller */
  26.  
  27. package com.jp.pasteleria.controller;
  28.  
  29. import java.util.List;
  30. import javax.validation.Valid;
  31.  
  32. import org.apache.logging.log4j.LogManager;
  33. import org.apache.logging.log4j.Logger;
  34.  
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.web.bind.annotation.*;
  37.  
  38. import com.jp.pasteleria.exception.ResourceNotFoundException;
  39. import com.jp.pasteleria.model.User;
  40. import com.jp.pasteleria.repository.UserRepository;
  41.  
  42. @RestController
  43. @RequestMapping("/api")
  44. public class UserController {
  45.  
  46. // private Logger log = LogManager.getLogger(UserController.class.getName());
  47.  
  48. @Autowired
  49. UserRepository userRepository;
  50.  
  51. @GetMapping("/users")
  52. public List<User> getAllNotes() {
  53. return userRepository.findAll();
  54. }
  55.  
  56. @PostMapping("/users")
  57. public User createUser(@Valid @RequestBody User user) {
  58.  
  59. return userRepository.save(user);
  60. }
  61.  
  62. @PutMapping("/users/{id}")
  63. public User updateNote(@PathVariable(value = "id") Long userId, @Valid @RequestBody User userDetails) {
  64.  
  65. User user = userRepository.findById(userId)
  66. .orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
  67.  
  68. user.setNombre(userDetails.getNombre());
  69. user.setApellido(userDetails.getApellido());
  70. user.setEdad(userDetails.getEdad());
  71. user.setEmail(userDetails.getEmail());
  72.  
  73. User updatedUser = userRepository.save(user);
  74. return updatedUser;
  75. }
  76. }
  77.  
  78. /* Model */
  79.  
  80. package com.jp.pasteleria.model;
  81.  
  82. import java.io.Serializable;
  83. import java.util.Date;
  84.  
  85. import javax.persistence.*;
  86. import javax.validation.constraints.NotBlank;
  87.  
  88. import org.springframework.data.annotation.CreatedDate;
  89. import org.springframework.data.annotation.LastModifiedDate;
  90. import org.springframework.data.jpa.domain.support.AuditingEntityListener;
  91. import org.springframework.lang.Nullable;
  92.  
  93. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  94.  
  95. @Entity
  96. @Table(name = "users")
  97. @EntityListeners(AuditingEntityListener.class)
  98. @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
  99. public class User implements Serializable{
  100. @Id
  101. @GeneratedValue(strategy = GenerationType.AUTO)
  102. private Long id;
  103.  
  104. @NotBlank
  105. private String nombre;
  106.  
  107. @NotBlank
  108. private String apellido;
  109.  
  110. @Nullable
  111. private Integer edad;
  112.  
  113. @NotBlank
  114. private String email;
  115.  
  116. @Column(name = "created_on", nullable = false, updatable = false)
  117. @Temporal(TemporalType.TIMESTAMP)
  118. @CreatedDate
  119. private Date createdAt;
  120.  
  121. @Column(name = "updated_on", nullable = false)
  122. @Temporal(TemporalType.TIMESTAMP)
  123. @LastModifiedDate
  124. private Date updatedAt;
  125.  
  126. public Long getId() {
  127. return id;
  128. }
  129.  
  130. public void setId(Long id) {
  131. this.id = id;
  132. }
  133.  
  134. public String getNombre() {
  135. return nombre;
  136. }
  137.  
  138. public void setNombre(String nombre) {
  139. this.nombre = nombre;
  140. }
  141.  
  142. public String getApellido() {
  143. return apellido;
  144. }
  145.  
  146. public void setApellido(String apellido) {
  147. this.apellido = apellido;
  148. }
  149.  
  150. public int getEdad() {
  151. return edad;
  152. }
  153.  
  154. public void setEdad(int edad) {
  155. this.edad = edad;
  156. }
  157.  
  158. public String getEmail() {
  159. return email;
  160. }
  161.  
  162. public void setEmail(String email) {
  163. this.email = email;
  164. }
  165.  
  166. public Date getCreatedAt() {
  167. return createdAt;
  168. }
  169.  
  170. public void setCreatedAt(Date createdAt) {
  171. this.createdAt = createdAt;
  172. }
  173.  
  174. public Date getUpdatedAt() {
  175. return updatedAt;
  176. }
  177.  
  178. public void setUpdatedAt(Date updatedAt) {
  179. this.updatedAt = updatedAt;
  180. }
  181. }
  182.  
  183.  
  184. /* Repository */
  185.  
  186. package com.jp.pasteleria.repository;
  187.  
  188. import org.springframework.data.jpa.repository.JpaRepository;
  189. import org.springframework.stereotype.Repository;
  190.  
  191. import com.jp.pasteleria.model.User;
  192.  
  193. @Repository
  194. public interface UserRepository extends JpaRepository<User, Long>{
  195.  
  196. }
  197.  
  198. /* application.properties */
  199.  
  200. ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
  201. spring.datasource.url = jdbc:mysql://localhost:3306/pasteleria?useSSL=false
  202. spring.datasource.username = root
  203. spring.datasource.password =
  204.  
  205.  
  206. ## Hibernate Properties
  207. # The SQL dialect makes Hibernate generate better SQL for the chosen database
  208. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
  209.  
  210. # Hibernate ddl auto (create, create-drop, validate, update)
  211. spring.jpa.hibernate.ddl-auto = update
Add Comment
Please, Sign In to add comment