using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using OpenAuth.App;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 字典分类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysDataItemController : ControllerBase
    {
        SysDataItemApp dataItemApp;
        public SysDataItemController(SysDataItemApp sysDataItemApp)
        {
            dataItemApp = sysDataItemApp;
        }

        /// <summary>
        /// 获取字典分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<Response<List<SysDataItem>>> Load()
        {
            var list = await dataItemApp.Load(string.Empty, false);

            return new Response<List<SysDataItem>>
            {
                Result = list,
                Message = "success"
            };
        }


        /// <summary>
        /// 获取分类实体数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]

        public async Task<Response<SysDataItem>> Get(string code)
        {
            var model = await dataItemApp.GetDataItem(code);

            return new Response<SysDataItem>
            {
                Result = model,
                Message = "success"
            };
        }


        /// <summary>
        /// 新增分类数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]

        public async Task<Response<bool>> Add(SysDataItem entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await dataItemApp.SaveClassifyEntity(entity);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 更新分类数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Update(SysDataItem entity)
        {
            var result = new Response<bool>();
            try
            {
                result = await dataItemApp.SaveClassifyEntity(entity);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除分类数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Delete(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await dataItemApp.DeleteClassify(id);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;          
        }
    }


}
