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 SecurityPermissionController : ControllerBase
{
private readonly IApplicationService service;
private readonly ISecurityPermissionService securityPermissionService;

public SecurityPermissionController(IApplicationService service, ISecurityPermissionService securityPermissionService)
{
this.service = service;
this.securityPermissionService= securityPermissionService;
}

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

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

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

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

                            [PermissionOperate(name: "SecurityPermission", operate: "Find")]
                            [HttpPost]
                            public dynamic FindDependentBySearchCriteria(SecurityPermissionSearchCriteria criteria)       
                    {
                    try
                    {            

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

                            [PermissionOperate(name: "SecurityPermission", operate: "Find")]
                            [HttpPost]
                            public dynamic FindDependentsBySearchCriteria(SecurityPermissionSearchCriteria criteria)       
                    {
                    try
                    {            

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

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

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

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

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

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

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

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

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

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

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

                    
}
}