io.curly.gathering.mention.MentionController.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.gathering.mention.MentionController.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.mention;

import java.util.concurrent.Callable;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Joo Pedro Evangelista
 */
@RestController
@RequestMapping("/list/{listId}/mention")
public class MentionController {

    private final ListInteraction interaction;

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

    @RequestMapping(value = "", method = { RequestMethod.PUT, RequestMethod.POST })
    public Callable<HttpEntity<?>> mention(@Valid @RequestBody MentionBody body, @PathVariable String listId,
            @GitHubAuthentication User user, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return () -> new ResponseEntity<Object>(new ModelErrors(bindingResult), HttpStatus.BAD_REQUEST);
        } else {
            return () -> {
                interaction.mention(new Mention(body.getParticipant(), listId), user);
                return new ResponseEntity<>(HttpStatus.CREATED);
            };
        }
    }

    @RequestMapping(value = "/{participant}", method = RequestMethod.DELETE)
    public Callable<HttpEntity<?>> unMention(@PathVariable String participant, @PathVariable String listId,
            @GitHubAuthentication User user) {
        return () -> {
            interaction.unMention(new UnMention(participant, listId), user);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        };
    }

}