From e5a7e4e01fca83000a4a975a85c022408ededf72 Mon Sep 17 00:00:00 2001 From: dizda13 Date: Sat, 1 Apr 2017 16:04:20 +0200 Subject: [PATCH] slaba baterija --- .../main/java/ba/steleks/EventController.java | 7 +++ .../java/ba/steleks/EventsApplication.java | 2 +- .../src/main/resources/application.properties | 2 +- users/build.gradle | 1 + .../steleks/repository/RestErrorHandler.java | 47 +++++++++++++++++++ .../ba/steleks/repository/model/User.java | 27 +++++++++-- 6 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 events/src/main/java/ba/steleks/EventController.java create mode 100644 users/src/main/java/ba/steleks/repository/RestErrorHandler.java diff --git a/events/src/main/java/ba/steleks/EventController.java b/events/src/main/java/ba/steleks/EventController.java new file mode 100644 index 0000000..a72ab32 --- /dev/null +++ b/events/src/main/java/ba/steleks/EventController.java @@ -0,0 +1,7 @@ +package ba.steleks; + +/** + * Created by admin on 01/04/2017. + */ +public class EventController { +} diff --git a/events/src/main/java/ba/steleks/EventsApplication.java b/events/src/main/java/ba/steleks/EventsApplication.java index a7a81f6..d9c7715 100644 --- a/events/src/main/java/ba/steleks/EventsApplication.java +++ b/events/src/main/java/ba/steleks/EventsApplication.java @@ -37,7 +37,7 @@ class MessageRestController { // return "temp " + Long.toString(id); //} - @RequestMapping(value = "/whoami/{user}", method = RequestMethod.GET) + @RequestMapping(value = "/whoami/{username}", method = RequestMethod.GET) public String whoami(@PathVariable("username") String username) { return String.format("Hello! You're %s and you'll become a(n) root, " + "but only if your password is '%s'!\n", diff --git a/events/src/main/resources/application.properties b/events/src/main/resources/application.properties index f6be723..bfe3465 100644 --- a/events/src/main/resources/application.properties +++ b/events/src/main/resources/application.properties @@ -3,7 +3,7 @@ spring.datasource.url = jdbc:mysql://localhost:3306/events spring.datasource.username = root spring.datasource.password = skorpion spring.jpa.generate-ddl=true - +user.password=dizda # #spring.datasource.url = jdbc:mysql://localhost:3306/events #spring.datasource.username = root diff --git a/users/build.gradle b/users/build.gradle index 7558c1a..f9490af 100644 --- a/users/build.gradle +++ b/users/build.gradle @@ -28,4 +28,5 @@ dependencies { runtime('com.h2database:h2') testCompile('org.springframework.boot:spring-boot-starter-test') compile('mysql:mysql-connector-java') + compile('org.hibernate:hibernate-validator') } diff --git a/users/src/main/java/ba/steleks/repository/RestErrorHandler.java b/users/src/main/java/ba/steleks/repository/RestErrorHandler.java new file mode 100644 index 0000000..32c42d3 --- /dev/null +++ b/users/src/main/java/ba/steleks/repository/RestErrorHandler.java @@ -0,0 +1,47 @@ +package ba.steleks.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.data.rest.webmvc.support.RepositoryConstraintViolationExceptionMessage; +import org.springframework.http.HttpStatus; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import java.util.List; +import java.util.Locale; + +/** + * Created by admin on 01/04/2017. + */ +@ControllerAdvice +public class GlobalExceptionController { + + @ExceptionHandler(CustomGenericException.class) + public ModelAndView handleCustomException(CustomGenericException ex) { + + ModelAndView model = new ModelAndView("error/generic_error"); + model.addObject("errCode", ex.getErrCode()); + model.addObject("errMsg", ex.getErrMsg()); + + return model; + + } + + @ExceptionHandler(Exception.class) + public ModelAndView handleAllException(Exception ex) { + + ModelAndView model = new ModelAndView("error/generic_error"); + model.addObject("errMsg", "this is Exception.class"); + + return model; + + } + +} diff --git a/users/src/main/java/ba/steleks/repository/model/User.java b/users/src/main/java/ba/steleks/repository/model/User.java index ed06425..889114a 100644 --- a/users/src/main/java/ba/steleks/repository/model/User.java +++ b/users/src/main/java/ba/steleks/repository/model/User.java @@ -2,7 +2,13 @@ package ba.steleks.repository.model;/** * Created by ensar on 22/03/17. */ +import org.hibernate.validator.constraints.NotBlank; + import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Date; import java.util.Set; import java.util.logging.Logger; @@ -15,14 +21,22 @@ public class User { @GeneratedValue(strategy = GenerationType.AUTO) private long id; + @NotNull private int cardNumber; + @NotNull private String firstName; + @NotNull private String lastName; - private String registrationDate; + @NotNull + @Column(updatable = false, insertable = false) + private Timestamp registrationDate; + @NotNull private String email; + @NotNull private String contactNumber; + @NotNull private String passwordHash; - + @NotNull private String username; private String profilePictureUrl; @@ -71,11 +85,11 @@ public class User { this.lastName = lastName; } - public String getRegistrationDate() { + public Timestamp getRegistrationDate() { return registrationDate; } - public void setRegistrationDate(String registrationDate) { + public void setRegistrationDate(Timestamp registrationDate) { this.registrationDate = registrationDate; } @@ -142,4 +156,9 @@ public class User { public void setUserRoles(Set userRoles) { this.userRoles = userRoles; } + + @PrePersist + protected void onCreate() { + this.registrationDate = new Timestamp(new Date().getTime()); + } }