diff --git a/events/src/main/java/ba/steleks/controller/ActualEventsController.java b/events/src/main/java/ba/steleks/controller/ActualEventsController.java new file mode 100644 index 0000000..4b6ed4e --- /dev/null +++ b/events/src/main/java/ba/steleks/controller/ActualEventsController.java @@ -0,0 +1,45 @@ +package ba.steleks.controller; + +import ba.steleks.error.exception.ExternalServiceException; +import ba.steleks.model.Event; +import ba.steleks.model.EventRequest; +import ba.steleks.repository.EventTypeJpaRepository; +import ba.steleks.repository.EventsJpaRepository; +import ba.steleks.repository.MediaJpaRepository; +import ba.steleks.util.ProxyHeaders; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class ActualEventsController { + private static final long EVENT_TYPE_EVENT = 2; + + private EventsJpaRepository repository; + private EventController eventController; + + @Autowired + public ActualEventsController(EventsJpaRepository repository, EventController eventController) { + this.repository = repository; + this.eventController = eventController; + } + + @RequestMapping(path = "/realEvents", method = RequestMethod.GET) + public ResponseEntity getRealEvents() { + Iterable result; + result = repository.findByEventTypeId(EVENT_TYPE_EVENT); + return ResponseEntity + .status(HttpStatus.OK) + .body(new Object() { + public Object _embedded = new Object() { + public Object events = result; + }; + }); + } + + @RequestMapping(path = "/realEvents", method = RequestMethod.POST) + public ResponseEntity addEvents(@RequestBody EventRequest eventRequest, @RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException { + return eventController.addEventWithType(eventRequest, userId, EVENT_TYPE_EVENT); + } +} diff --git a/events/src/main/java/ba/steleks/controller/EventController.java b/events/src/main/java/ba/steleks/controller/EventController.java index 8853360..6a2c2c5 100644 --- a/events/src/main/java/ba/steleks/controller/EventController.java +++ b/events/src/main/java/ba/steleks/controller/EventController.java @@ -3,7 +3,9 @@ package ba.steleks.controller; import ba.steleks.error.exception.ExternalServiceException; import ba.steleks.model.Event; import ba.steleks.model.EventRequest; +import ba.steleks.model.EventType; import ba.steleks.model.Media; +import ba.steleks.repository.EventTypeJpaRepository; import ba.steleks.repository.EventsJpaRepository; import ba.steleks.repository.MediaJpaRepository; import ba.steleks.util.ProxyHeaders; @@ -25,18 +27,22 @@ import java.util.Set; @RepositoryRestController public class EventController { - + private EventsJpaRepository repository; private MediaJpaRepository mediaJpaRepository; + private EventTypeJpaRepository eventTypeJpaRepository; @Autowired - public EventController(EventsJpaRepository repository, MediaJpaRepository mediaJpaRepository) { + public EventController(EventsJpaRepository repository, + MediaJpaRepository mediaJpaRepository, + EventTypeJpaRepository eventTypeJpaRepository) { this.repository = repository; this.mediaJpaRepository = mediaJpaRepository; + this.eventTypeJpaRepository = eventTypeJpaRepository; } - @RequestMapping(path = "/events", method = RequestMethod.POST) - public ResponseEntity add(@RequestBody EventRequest eventRequest, @RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException { + public ResponseEntity addEventWithType(EventRequest eventRequest, String userId, long eventTypeId) throws ExternalServiceException { + EventType eventType = eventTypeJpaRepository.findOne(eventTypeId); System.out.println(eventRequest); try { Event event = new Event(); @@ -44,11 +50,14 @@ public class EventController { event.setShortText(eventRequest.getShortText()); event.setLongText(eventRequest.getLongText()); event.setCreatedById(Long.parseLong(userId)); + event.setEventType(eventType); Set savedMedia = new HashSet<>(); - for (Media media : eventRequest.getMedias()) { - Media loadedMedia = mediaJpaRepository.findOne(media.getId()); - savedMedia.add(loadedMedia); - System.out.println("Loaded media: " + loadedMedia); + if (eventRequest.getMedias() != null) { + for (Media media : eventRequest.getMedias()) { + Media loadedMedia = mediaJpaRepository.findOne(media.getId()); + savedMedia.add(loadedMedia); + System.out.println("Loaded media: " + loadedMedia); + } } Event result = repository.save(event); result.setMediaSet(savedMedia); @@ -67,20 +76,14 @@ public class EventController { } @RequestMapping(path = "/events/{id}", method = RequestMethod.GET) - public ResponseEntity getEventsById(@RequestParam Long typeId) { - Iterable result; - if (typeId == null) { - result = repository.findAll(); - } else { - result = repository.findByEventTypeId(typeId); + public ResponseEntity getEventsById(@PathVariable("id") String eventId) { + Event result; + result = repository.findOne(Long.parseLong(eventId)); + if (result == null) { + return ResponseEntity.notFound().build(); } return ResponseEntity .status(HttpStatus.OK) - .body(new Object() { - public Object _embedded = new Object() { - public Object events = result; - }; - }); + .body(result); } - } diff --git a/events/src/main/java/ba/steleks/controller/NewsController.java b/events/src/main/java/ba/steleks/controller/NewsController.java new file mode 100644 index 0000000..81bb8e4 --- /dev/null +++ b/events/src/main/java/ba/steleks/controller/NewsController.java @@ -0,0 +1,46 @@ +package ba.steleks.controller; + +import ba.steleks.error.exception.ExternalServiceException; +import ba.steleks.model.Event; +import ba.steleks.model.EventRequest; +import ba.steleks.repository.EventTypeJpaRepository; +import ba.steleks.repository.EventsJpaRepository; +import ba.steleks.repository.MediaJpaRepository; +import ba.steleks.util.ProxyHeaders; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class NewsController { + private static final long EVENT_TYPE_NEWS = 1; + + private EventsJpaRepository repository; + private EventController eventController; + + @Autowired + public NewsController(EventsJpaRepository repository, + EventController eventController) { + this.repository = repository; + this.eventController = eventController; + } + + @RequestMapping(path = "/news", method = RequestMethod.POST) + public ResponseEntity addNews(@RequestBody EventRequest eventRequest, @RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException { + return eventController.addEventWithType(eventRequest, userId, EVENT_TYPE_NEWS); + } + + @RequestMapping(path = "/news", method = RequestMethod.GET) + public ResponseEntity getNews() { + Iterable result; + result = repository.findByEventTypeId(EVENT_TYPE_NEWS); + return ResponseEntity + .status(HttpStatus.OK) + .body(new Object() { + public Object _embedded = new Object() { + public Object events = result; + }; + }); + } +}