/*
 * Copyright 2015-2016 EuregJUG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.euregjug.site.events;

import eu.euregjug.site.posts.PostEntity;
import eu.euregjug.site.posts.PostRepository;
import eu.euregjug.site.support.ResourceNotFoundException;
import java.util.List;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

/**
 * @author Michael J. Simons, 2015-12-27
 */
@RestController
@RequestMapping("/api/events")
class EventApiController {

    private final EventRepository eventRepository;

    private final PostRepository postRepository;

    private final RegistrationRepository registrationRepository;

    EventApiController(final EventRepository eventRepository, final PostRepository postRepository, final RegistrationRepository registrationRepository) {
        this.eventRepository = eventRepository;
        this.postRepository = postRepository;
        this.registrationRepository = registrationRepository;
    }

    @RequestMapping(method = POST)
    @PreAuthorize("isAuthenticated()")
    public EventEntity create(@Valid @RequestBody final EventEntity newEvent) {
        return this.eventRepository.save(newEvent);
    }

    // TODO: Choose wether to create a completly new posts or not. For the time being: just select one
    @RequestMapping(value = "/{id:\\d+}/post/{postId:\\d+}", method = PUT)
    @PreAuthorize("isAuthenticated()")
    @Transactional
    public EventEntity addPost(@PathVariable final Integer id, @PathVariable final Integer postId) {
        final EventEntity eventEntity = this.eventRepository.findOne(id).orElse(null);
        final PostEntity postEntity = this.postRepository.findOne(postId).orElse(null);
        if (eventEntity == null || postEntity == null) {
            throw new ResourceNotFoundException();
        }
        eventEntity.setPost(postEntity);
        return eventEntity;
    }

    @RequestMapping(method = GET)
    public Page<EventEntity> get(final Pageable pageable) {
        return this.eventRepository.findAll(pageable);
    }

    @RequestMapping(value = "/{eventId}/registrations", method = GET)
    @PreAuthorize("isAuthenticated()")
    public List<RegistrationEntity> getRegistrations(@PathVariable final Integer eventId) {
        return this.registrationRepository.findAllByEventId(eventId);
    }

    @RequestMapping(value = "/{id:\\d+}", method = PUT)
    @PreAuthorize("isAuthenticated()")
    @Transactional
    public EventEntity update(@PathVariable final Integer id, @Valid @RequestBody final EventEntity updatedEvent) {
        final EventEntity eventEntity = this.eventRepository.findOne(id).orElseThrow(ResourceNotFoundException::new);
        eventEntity.setDescription(updatedEvent.getDescription());
        eventEntity.setDuration(updatedEvent.getDuration());
        eventEntity.setNeedsRegistration(updatedEvent.isNeedsRegistration());
        eventEntity.setType(updatedEvent.getType());
        return eventEntity;
    }
}

@@ -917,24 +917,83 @@
 n.Pageable;%0A
+import static org.springframework.http.HttpStatus.CREATED;%0A
 import org.s
@@ -1045,16 +1045,16 @@
 horize;%0A
-
 import o
@@ -1577,16 +1577,79 @@
 hod.PUT;
+%0Aimport org.springframework.web.bind.annotation.ResponseStatus;
 %0A%0A/**%0A *
@@ -2286,16 +2286,16 @@
 = POST)%0A
-
     @Pre
@@ -2317,32 +2317,61 @@
 thenticated()%22)%0A
+    @ResponseStatus(CREATED)%0A
     public Event

Change Response code to 201