// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Status {
// 
//     draft, published, hidden
// }
// 
// Path: src/main/java/eu/euregjug/site/support/ResourceNotFoundException.java
// @ResponseStatus(HttpStatus.NOT_FOUND)
// public class ResourceNotFoundException extends RuntimeException {
// 
//     private static final long serialVersionUID = -4819169062859178748L;
// }


import eu.euregjug.site.posts.PostEntity.Status;
import eu.euregjug.site.support.ResourceNotFoundException;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.HttpStatus.CREATED;
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;
import org.springframework.web.bind.annotation.RequestParam;
/*
 * 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.posts;



/**
 * @author Michael J. Simons, 2015-12-28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/posts")
class PostApiController {

    private final PostRepository postRepository;

    private final PostIndexService postIndexService;

    @RequestMapping(method = POST)
    @PreAuthorize("isAuthenticated()")
    @ResponseStatus(CREATED)
    public PostEntity create(@Valid @RequestBody final PostEntity newPost) {
        newPost.setLocale(Optional.ofNullable(newPost.getLocale()).orElseGet(() -> new Locale("en", "US")));
// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Status {
// 
//     draft, published, hidden
// }
// 
// Path: src/main/java/eu/euregjug/site/support/ResourceNotFoundException.java
// @ResponseStatus(HttpStatus.NOT_FOUND)
// public class ResourceNotFoundException extends RuntimeException {
// 
//     private static final long serialVersionUID = -4819169062859178748L;
// }


// Path: src/main/java/eu/euregjug/site/posts/PostApiController.java
import eu.euregjug.site.posts.PostEntity.Status;
import eu.euregjug.site.support.ResourceNotFoundException;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.HttpStatus.CREATED;
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;
import org.springframework.web.bind.annotation.RequestParam;

/*
 * 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.posts;



/**
 * @author Michael J. Simons, 2015-12-28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/posts")
class PostApiController {

    private final PostRepository postRepository;

    private final PostIndexService postIndexService;

    @RequestMapping(method = POST)
    @PreAuthorize("isAuthenticated()")
    @ResponseStatus(CREATED)
    public PostEntity create(@Valid @RequestBody final PostEntity newPost) {
        newPost.setLocale(Optional.ofNullable(newPost.getLocale()).orElseGet(() -> new Locale("en", "US")));
        newPost.setStatus(Optional.ofNullable(newPost.getStatus()).orElse(Status.draft));