using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.Policy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using CloudBuilder.Core.Service;
using CloudBuilder.Security.Service;
            using CloudBuilder.Core.DatabaseAccessor.Entity;
            using Microsoft.AspNetCore.Http;
            using CloudBuilder.Core.Policy;
            using CloudBuilder.Core.DependencyInjection.Request;
            using CloudBuilder.Security.Data;
            using CloudBuilder.Core.Authorization;
            using CloudBuilder.Security.Entity;
            using CloudBuilder.Security.SearchCriteria;
            using CloudBuilder.Security;
            using System;
            
namespace CloudBuilder.Web.Entry.Controllers.Security
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class SecurityFunctionController : ControllerBase
{
private readonly IApplicationService service;
private readonly ISecurityFunctionService securityFunctionService;

public SecurityFunctionController(IApplicationService service, ISecurityFunctionService securityFunctionService)
{
this.service = service;
this.securityFunctionService= securityFunctionService;
}

        [PermissionOperate(name: "SecurityFunction", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByKey(string key)       
                    {
                    try
                    {            

                    var result = securityFunctionService.FindByKey(key);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Find")]
                            [HttpPost]
                            public dynamic FindBySearchCriteria(SecurityFunctionSearchCriteria criteria)       
                    {
                    try
                    {            

                    var result = securityFunctionService.FindBySearchCriteria(criteria);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Print")]
                            [HttpPost]
                            public dynamic PrintBySearchCriteria(SecurityFunctionSearchCriteria criteria)       
                    {
                    try
                    {            

                    var result = securityFunctionService.PrintBySearchCriteria(criteria);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Insert")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Insert(SecurityFunctionData data)       
                    {
                    try
                    {            

                    var result = securityFunctionService.Insert(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Update(SecurityFunctionData data)       
                    {
                    try
                    {            

                    var result = securityFunctionService.Update(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Delete(string key){
                    try
                    {            
                securityFunctionService.Delete(key);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                            [PermissionOperate(name: "SecurityFunction", operate: "Delete")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Deletes(string[] keys){
                    try
                    {            
                securityFunctionService.Deletes(keys);

                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                    
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic FindByFunctionName(string functionName)       
                    {
                    try
                    {            

                    var result = securityFunctionService.FindByFunctionName(functionName);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic FindMenus()
                    {
                    try
                    {

                    var result = securityFunctionService.FindMenus();
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                    
}
}