Add routes for news and real events
parent
b81f9ea543
commit
63bb3473c6
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,9 @@ package ba.steleks.controller;
|
||||||
import ba.steleks.error.exception.ExternalServiceException;
|
import ba.steleks.error.exception.ExternalServiceException;
|
||||||
import ba.steleks.model.Event;
|
import ba.steleks.model.Event;
|
||||||
import ba.steleks.model.EventRequest;
|
import ba.steleks.model.EventRequest;
|
||||||
|
import ba.steleks.model.EventType;
|
||||||
import ba.steleks.model.Media;
|
import ba.steleks.model.Media;
|
||||||
|
import ba.steleks.repository.EventTypeJpaRepository;
|
||||||
import ba.steleks.repository.EventsJpaRepository;
|
import ba.steleks.repository.EventsJpaRepository;
|
||||||
import ba.steleks.repository.MediaJpaRepository;
|
import ba.steleks.repository.MediaJpaRepository;
|
||||||
import ba.steleks.util.ProxyHeaders;
|
import ba.steleks.util.ProxyHeaders;
|
||||||
|
@ -25,18 +27,22 @@ import java.util.Set;
|
||||||
|
|
||||||
@RepositoryRestController
|
@RepositoryRestController
|
||||||
public class EventController {
|
public class EventController {
|
||||||
|
|
||||||
private EventsJpaRepository repository;
|
private EventsJpaRepository repository;
|
||||||
private MediaJpaRepository mediaJpaRepository;
|
private MediaJpaRepository mediaJpaRepository;
|
||||||
|
private EventTypeJpaRepository eventTypeJpaRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public EventController(EventsJpaRepository repository, MediaJpaRepository mediaJpaRepository) {
|
public EventController(EventsJpaRepository repository,
|
||||||
|
MediaJpaRepository mediaJpaRepository,
|
||||||
|
EventTypeJpaRepository eventTypeJpaRepository) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
this.mediaJpaRepository = mediaJpaRepository;
|
this.mediaJpaRepository = mediaJpaRepository;
|
||||||
|
this.eventTypeJpaRepository = eventTypeJpaRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/events", method = RequestMethod.POST)
|
public ResponseEntity<?> addEventWithType(EventRequest eventRequest, String userId, long eventTypeId) throws ExternalServiceException {
|
||||||
public ResponseEntity<?> add(@RequestBody EventRequest eventRequest, @RequestHeader(ProxyHeaders.USER_ID) String userId) throws ExternalServiceException {
|
EventType eventType = eventTypeJpaRepository.findOne(eventTypeId);
|
||||||
System.out.println(eventRequest);
|
System.out.println(eventRequest);
|
||||||
try {
|
try {
|
||||||
Event event = new Event();
|
Event event = new Event();
|
||||||
|
@ -44,11 +50,14 @@ public class EventController {
|
||||||
event.setShortText(eventRequest.getShortText());
|
event.setShortText(eventRequest.getShortText());
|
||||||
event.setLongText(eventRequest.getLongText());
|
event.setLongText(eventRequest.getLongText());
|
||||||
event.setCreatedById(Long.parseLong(userId));
|
event.setCreatedById(Long.parseLong(userId));
|
||||||
|
event.setEventType(eventType);
|
||||||
Set<Media> savedMedia = new HashSet<>();
|
Set<Media> savedMedia = new HashSet<>();
|
||||||
for (Media media : eventRequest.getMedias()) {
|
if (eventRequest.getMedias() != null) {
|
||||||
Media loadedMedia = mediaJpaRepository.findOne(media.getId());
|
for (Media media : eventRequest.getMedias()) {
|
||||||
savedMedia.add(loadedMedia);
|
Media loadedMedia = mediaJpaRepository.findOne(media.getId());
|
||||||
System.out.println("Loaded media: " + loadedMedia);
|
savedMedia.add(loadedMedia);
|
||||||
|
System.out.println("Loaded media: " + loadedMedia);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Event result = repository.save(event);
|
Event result = repository.save(event);
|
||||||
result.setMediaSet(savedMedia);
|
result.setMediaSet(savedMedia);
|
||||||
|
@ -67,20 +76,14 @@ public class EventController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/events/{id}", method = RequestMethod.GET)
|
@RequestMapping(path = "/events/{id}", method = RequestMethod.GET)
|
||||||
public ResponseEntity<?> getEventsById(@RequestParam Long typeId) {
|
public ResponseEntity<?> getEventsById(@PathVariable("id") String eventId) {
|
||||||
Iterable<Event> result;
|
Event result;
|
||||||
if (typeId == null) {
|
result = repository.findOne(Long.parseLong(eventId));
|
||||||
result = repository.findAll();
|
if (result == null) {
|
||||||
} else {
|
return ResponseEntity.notFound().build();
|
||||||
result = repository.findByEventTypeId(typeId);
|
|
||||||
}
|
}
|
||||||
return ResponseEntity
|
return ResponseEntity
|
||||||
.status(HttpStatus.OK)
|
.status(HttpStatus.OK)
|
||||||
.body(new Object() {
|
.body(result);
|
||||||
public Object _embedded = new Object() {
|
|
||||||
public Object events = result;
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue