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;
            
namespace CloudBuilder.Web.Entry.Controllers.Security
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class LocaleController : ControllerBase
{
private readonly IApplicationService service;
private readonly ILocaleService localeService;

public LocaleController(IApplicationService service, ILocaleService localeService)
{
this.service = service;
this.localeService= localeService;
}

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

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

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic FindBySearchCriteria(LocaleSearchCriteria criteria)       
                    {
                    try
                    {            

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

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic FindByKey(LocaleEntityKey key)       
                    {
                    try
                    {            

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

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

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

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

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

                            [PermissionOperate(name: "Locale", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic UpdateLocaleData(LocaleData data){
                    try
                    {            
                localeService.UpdateLocaleData(data);

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

                            [PermissionOperate(name: "Locale", operate: "Delete")]
                            [HttpPost]
                            public dynamic Delete(LocaleEntityKey key){
                    try
                    {            
                localeService.Delete(key);

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

                    
}
}