package com.github.jacek99.springbootcucumber.controller.admin;

import com.github.jacek99.springbootcucumber.dao.TenantUserDao;
import com.github.jacek99.springbootcucumber.domain.Tenant;
import com.github.jacek99.springbootcucumber.domain.TenantUser;
import com.github.jacek99.springbootcucumber.security.TenantToken;
import com.google.common.base.Preconditions;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * REST APIs for creating tenantTokens within a tenant
 * For tenant admins only
 * @author Jacek Furmankiewicz *
 */
@RestController
@RequestMapping(value = "/myapp/admin/tenantTokens", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TenantUserController {

    @Autowired
    private TenantUserDao dao;

    @RequestMapping(method = RequestMethod.GET)
    public List<TenantUser> getAll(@AuthenticationPrincipal TenantToken tenantToken) {
        return dao.findAll(tenantToken);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public TenantUser save(@AuthenticationPrincipal TenantToken tenantToken, @RequestBody @Valid TenantUser entity) {
        dao.save(tenantToken, entity);
        return entity;
    }

    @RequestMapping(value = "/{tenantTokenId}", method = RequestMethod.PATCH)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@AuthenticationPrincipal TenantToken tenantToken, @PathVariable("tenantTokenId") String tenantTokenID,
                       @RequestBody @Valid TenantUser entity) {
        // path sanity check
        Preconditions.checkArgument(tenantTokenID.equals(entity.getUserId()),"tenantTokenId is not consistent with path");

        dao.update(tenantToken, entity);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void saveOrUpdate(@AuthenticationPrincipal TenantToken tenantToken, @RequestBody @Valid TenantUser entity) {
        dao.saveOrUpate(tenantToken, entity);
    }

    @RequestMapping(value = "/{tenantTokenId}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@AuthenticationPrincipal TenantToken tenantToken, @PathVariable("tenantTokenId") String tenantTokenId) {
        dao.delete(tenantToken,tenantTokenId);
    }

}

@@ -1048,21 +1048,21 @@
 g tenant
-Token
+ user
 s within
@@ -1188,27 +1188,20 @@
 p/admin/
-tenantToken
+user
 s%22, prod
@@ -1818,37 +1818,32 @@
 alue = %22/%7Btenant
-Token
 Id%7D%22, method = R
@@ -1991,37 +1991,32 @@
 Variable(%22tenant
-Token
 Id%22) String tena
@@ -2203,21 +2203,16 @@
 ,%22tenant
-Token
 Id is no
@@ -2587,21 +2587,16 @@
 /%7Btenant
-Token
 Id%7D%22, me
@@ -2761,21 +2761,16 @@
 (%22tenant
-Token
 Id%22) Str
@@ -2779,21 +2779,16 @@
 g tenant
-Token
 Id) %7B%0A  
@@ -2822,21 +2822,16 @@
 n,tenant
-Token
 Id);%0A   

fix URL rewritted by refactoring error