Add routes for news and real events

master
esensar 2018-09-16 21:48:55 +02:00
parent b81f9ea543
commit 63bb3473c6
3 changed files with 114 additions and 20 deletions

View File

@ -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<Event> 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);
}
}

View File

@ -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<Media> 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<Event> 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);
}
}

View File

@ -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<Event> result;
result = repository.findByEventTypeId(EVENT_TYPE_NEWS);
return ResponseEntity
.status(HttpStatus.OK)
.body(new Object() {
public Object _embedded = new Object() {
public Object events = result;
};
});
}
}