EventController & UserController
parent
e5a7e4e01f
commit
917e6bf5d7
|
@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String, String> handleCustomException(javax.validation.ConstraintViolationException ex) {
|
||||
|
||||
ModelAndView model = new ModelAndView("error/generic_error");
|
||||
model.addObject("errCode", ex.getErrCode());
|
||||
model.addObject("errMsg", ex.getErrMsg());
|
||||
Map<String, String> map= new HashMap<String, String>();
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<User, Long> {
|
||||
public interface UserRolesJpaRepository extends PagingAndSortingRepository<UserRole, Long> {
|
||||
}
|
||||
|
|
|
@ -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<UserRole, Long> {
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
public interface UsersJpaRepository extends PagingAndSortingRepository<User, Long> {
|
||||
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue