cz.muni.fi.editor.webapp.controllers.ajax.AjaxOrganizationController.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.webapp.controllers.ajax.AjaxOrganizationController.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * 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 cz.muni.fi.editor.webapp.controllers.ajax;

import cz.muni.fi.editor.api.dto.OrganizationDTO;
import cz.muni.fi.editor.api.dto.UserDTO;
import cz.muni.fi.editor.api.exceptions.FieldException;
import cz.muni.fi.editor.api.OrganizationService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 22.8.2016.
 */
@Controller
@Log4j2
@RequestMapping("/auth/ajax/organization")
public class AjaxOrganizationController {
    private static final ResponseEntity<String> OK = new ResponseEntity<>("success", HttpStatus.OK);
    //    private static final ResponseEntity<String> CREATED = new ResponseEntity<>("success", HttpStatus.CREATED);
    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "/changeName/", method = RequestMethod.POST)
    public ResponseEntity<String> changeOrganizationName(@RequestParam Long organization,
            @RequestParam String name) {
        OrganizationDTO org = new OrganizationDTO();
        org.setId(organization);
        org.setOrganizationName(name);

        try {
            organizationService.rename(org);

            return OK;
        } catch (FieldException ex) {
            log.warn(ex);
            return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT);
        }
    }

    @RequestMapping(value = "/kick/")
    public ResponseEntity<String> kick(@RequestParam Long organization, @RequestParam Long user) {
        OrganizationDTO org = new OrganizationDTO();
        org.setId(organization);

        UserDTO userDTO = new UserDTO();
        userDTO.setId(user);

        try {
            organizationService.kick(org, userDTO);

            return OK;
        } catch (FieldException ex) {
            log.warn(ex);
            return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT);
        }
    }
}