From 917e6bf5d7a434e19f18c7afbbaaa5ed97ab612d Mon Sep 17 00:00:00 2001 From: dizda13 Date: Sun, 2 Apr 2017 16:26:14 +0200 Subject: [PATCH] EventController & UserController --- .../main/java/ba/steleks/EventController.java | 53 +++++++++++++++++++ .../ba/steleks/controller/UserController.java | 37 +++++++++++++ .../steleks/repository/RestErrorHandler.java | 30 +++++------ .../repository/UserRolesJpaRepository.java | 3 +- .../repository/UsersJpaRepository.java | 12 ++++- 5 files changed, 118 insertions(+), 17 deletions(-) create mode 100644 users/src/main/java/ba/steleks/controller/UserController.java diff --git a/events/src/main/java/ba/steleks/EventController.java b/events/src/main/java/ba/steleks/EventController.java index a72ab32..e688329 100644 --- a/events/src/main/java/ba/steleks/EventController.java +++ b/events/src/main/java/ba/steleks/EventController.java @@ -1,7 +1,60 @@ package ba.steleks; +import ba.steleks.repository.EventsJpaRepository; +import ba.steleks.repository.model.Event; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.data.rest.webmvc.RepositoryRestController; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import javax.xml.ws.http.HTTPException; +import java.net.URI; +import java.util.HashMap; + /** * Created by admin on 01/04/2017. */ + +@RepositoryRestController public class EventController { + + private EventsJpaRepository repository; + + private RestTemplate restTemplate; + + @Autowired + public EventController(RestTemplateBuilder restTemplateBuilder, EventsJpaRepository repository) { + this.restTemplate = restTemplateBuilder.build(); + this.repository = repository; + } + + @RequestMapping(path = "/events", method = RequestMethod.POST) + public ResponseEntity add(@RequestBody Event event) { + + String oviUseriNeki = "http://localhost:8090/users/{id}"; + try { + + String response = restTemplate.getForObject(oviUseriNeki, String.class, event.getCreatedById()); + Event result = repository.save(event); + URI location = ServletUriComponentsBuilder + .fromCurrentRequest().path("/{id}") + .buildAndExpand(result.getId()).toUri(); + return ResponseEntity.created(location).body(result); + } catch (Exception ex) { + if (ex instanceof HttpClientErrorException && ((HttpClientErrorException) ex).getStatusCode() == HttpStatus.NOT_FOUND) { + return ResponseEntity.badRequest().body(HttpStatus.BAD_REQUEST); + } else { + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Users service is dead! It's dead! There is no hope. It's all gone!"); + } + } + } + } diff --git a/users/src/main/java/ba/steleks/controller/UserController.java b/users/src/main/java/ba/steleks/controller/UserController.java new file mode 100644 index 0000000..4c334d4 --- /dev/null +++ b/users/src/main/java/ba/steleks/controller/UserController.java @@ -0,0 +1,37 @@ +package ba.steleks.controller; + +import ba.steleks.repository.UsersJpaRepository; +import ba.steleks.repository.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.RepositoryRestController; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractController; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.net.URI; + +/** + * Created by admin on 02/04/2017. + */ +@RepositoryRestController +public class UserController { + + @Autowired + private UsersJpaRepository repository; + + @RequestMapping(path = "/users", method = RequestMethod.POST) + public ResponseEntity add(@RequestBody User user) { + System.out.println("doslo je do poziva"); + User result = repository.save(user); + URI location = ServletUriComponentsBuilder + .fromCurrentRequest().path("/{id}") + .buildAndExpand(result.getId()).toUri(); + return ResponseEntity.created(location).body(result); + } +} diff --git a/users/src/main/java/ba/steleks/repository/RestErrorHandler.java b/users/src/main/java/ba/steleks/repository/RestErrorHandler.java index 32c42d3..cba63b5 100644 --- a/users/src/main/java/ba/steleks/repository/RestErrorHandler.java +++ b/users/src/main/java/ba/steleks/repository/RestErrorHandler.java @@ -1,5 +1,7 @@ package ba.steleks.repository; +import com.fasterxml.jackson.databind.util.JSONPObject; +import jdk.nashorn.internal.runtime.JSONFunctions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.MessageSource; @@ -13,35 +15,33 @@ 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 org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; /** * Created by admin on 01/04/2017. */ @ControllerAdvice -public class GlobalExceptionController { +public class RestErrorHandler extends ResponseEntityExceptionHandler { - @ExceptionHandler(CustomGenericException.class) - public ModelAndView handleCustomException(CustomGenericException ex) { + @ExceptionHandler(javax.validation.ConstraintViolationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ResponseBody + public Map handleCustomException(javax.validation.ConstraintViolationException ex) { - ModelAndView model = new ModelAndView("error/generic_error"); - model.addObject("errCode", ex.getErrCode()); - model.addObject("errMsg", ex.getErrMsg()); + Map map= new HashMap(); + map.put("status", "400"); + map.put("error", "Bad request"); - return model; + return map; } - @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/UserRolesJpaRepository.java b/users/src/main/java/ba/steleks/repository/UserRolesJpaRepository.java index 4997577..e1caecd 100644 --- a/users/src/main/java/ba/steleks/repository/UserRolesJpaRepository.java +++ b/users/src/main/java/ba/steleks/repository/UserRolesJpaRepository.java @@ -5,8 +5,9 @@ package ba.steleks.repository; */ import ba.steleks.repository.model.User; +import ba.steleks.repository.model.UserRole; import org.springframework.data.repository.PagingAndSortingRepository; -public interface UserRolesJpaRepository extends PagingAndSortingRepository { +public interface UserRolesJpaRepository extends PagingAndSortingRepository { } diff --git a/users/src/main/java/ba/steleks/repository/UsersJpaRepository.java b/users/src/main/java/ba/steleks/repository/UsersJpaRepository.java index 4aa5d61..15334af 100644 --- a/users/src/main/java/ba/steleks/repository/UsersJpaRepository.java +++ b/users/src/main/java/ba/steleks/repository/UsersJpaRepository.java @@ -4,8 +4,18 @@ package ba.steleks.repository; * Created by ensar on 22/03/17. */ +import ba.steleks.repository.model.User; import ba.steleks.repository.model.UserRole; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.annotation.Transient; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.repository.PagingAndSortingRepository; -public interface UsersJpaRepository extends PagingAndSortingRepository { +import javax.transaction.Transactional; + +public interface UsersJpaRepository extends PagingAndSortingRepository { + } +