io.curly.gathering.item.ItemController.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.gathering.item.ItemController.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    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 io.curly.gathering.item;

import io.curly.commons.github.GitHubAuthentication;
import io.curly.commons.github.User;
import io.curly.commons.web.ModelErrors;
import io.curly.gathering.list.ListInteraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.concurrent.Callable;

/**
 * @author Joo Pedro Evangelista
 */
@RestController
@RequestMapping("/lists/{listId}")
public class ItemController {

    private final ListInteraction interaction;

    @Autowired
    public ItemController(@NotNull ListInteraction interaction) {
        this.interaction = interaction;
    }

    /**
     * Method that handle user's intent to add a new item to a list
     * @param body valid body for request
     * @param listId list to be added to
     * @param user current user on context
     * @param bindingResult result of validation of body
     * @return 400 if result returns errors or 201 if ok
     */
    @RequestMapping(value = "/add/artifact", method = { RequestMethod.PUT, RequestMethod.POST })
    public Callable<HttpEntity<?>> addItem(@Valid @RequestBody AddableItemBody body, @PathVariable String listId,
            @GitHubAuthentication User user, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return () -> new ResponseEntity<Object>(new ModelErrors(bindingResult), HttpStatus.BAD_REQUEST);
        } else {
            return () -> {
                this.interaction.addItem(new AddableItem(body.getItem(), listId, ItemType.ARTIFACT), user);
                return new ResponseEntity<>(HttpStatus.CREATED);
            };
        }
    }

    @RequestMapping(value = "/delete/artifact/{artifact}", method = RequestMethod.DELETE)
    public Callable<HttpEntity<?>> removeItem(@PathVariable String listId, @PathVariable String artifact,
            @GitHubAuthentication User user) {

        return () -> {
            this.interaction.removeItem(new RemovableItem(artifact, listId, ItemType.ARTIFACT), user);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        };
    }
}