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 SecurityMasterController : ControllerBase
{
private readonly IApplicationService service;
private readonly ISecurityMasterService securityMasterService;

public SecurityMasterController(IApplicationService service, ISecurityMasterService securityMasterService)
{
this.service = service;
this.securityMasterService= securityMasterService;
}

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

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

                            [AllowAnonymous]
                            [HttpGet]
                            public dynamic FindAll()
                    {
                    try
                    {

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

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

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

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

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

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

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

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

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

                            [PermissionOperate(name: "SecurityMaster", operate: "Delete")]
                            [HttpPost]
                            public dynamic Delete(SecurityMasterEntityKey key){
                    try
                    {            
                securityMasterService.Delete(key);

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

                            [PermissionOperate(name: "SecurityMaster", operate: "Delete")]
                            [HttpPost]
                            public dynamic Deletes(SecurityMasterEntity[] datas){
                    try
                    {            
                securityMasterService.Deletes(datas);

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

                    
}
}