
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.AI.Service;
            using CloudBuilder.Core.DatabaseAccessor.Entity;
            using CloudBuilder.AI.Entity;
            using System;
            using CloudBuilder.AI.Data;
            using CloudBuilder.AI.SearchCriteria;
            
namespace CloudBuilder.Web.Entry.Controllers.Ai
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class AiChapterController : ControllerBase
{
private readonly IApplicationService service;
private readonly IAiChapterService aiChapterService;

public AiChapterController(IApplicationService service, IAiChapterService aiChapterService)
{
this.service = service;
this.aiChapterService= aiChapterService;
}

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

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

                            [PermissionOperate(name: "AiBook", operate: "Find")]
                            [HttpPost]
                            public dynamic FindByTitle(string title)       
                    {
                    try
                    {            

                    var result = aiChapterService.FindByTitle(title);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

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

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

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

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

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

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

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

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

                    
}
}