namespace Tiobon.Core.Controllers
{
///
/// 接口管理
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name), ApiExplorerSettings(GroupName = Grouping.GroupName_Other)]
public class ModuleController : BaseApiController
{
readonly IModuleServices _moduleServices;
readonly IUser _user;
public ModuleController(IModuleServices moduleServices, IUser user)
{
_moduleServices = moduleServices;
_user = user;
}
///
/// 获取全部接口api
///
///
///
///
// GET: api/User
[HttpGet]
public async Task>> Get(int page = 1, string key = "")
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
int intPageSize = 50;
Expression> whereExpression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key));
PageModel data = new PageModel();
if (page == -1)
{
var modules = await _moduleServices.Query(whereExpression, " Id desc ");
data.data = modules;
}
else
{
data = await _moduleServices.QueryPage(whereExpression, page, intPageSize, " Id desc ");
}
return Success(data, "获取成功");
}
// GET: api/User/5
[HttpGet("{id}")]
public string Get(string id)
{
return "value";
}
///
/// 添加一条接口信息
///
///
///
// POST: api/User
[HttpPost]
public async Task> Post([FromBody] Modules module)
{
module.CreateId = _user.ID;
module.CreateBy = _user.Name;
var id = (await _moduleServices.Add(module));
return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed();
}
///
/// 更新接口信息
///
///
///
// PUT: api/User/5
[HttpPut]
public async Task> Put([FromBody] Modules module)
{
//var data = new ServiceResult();
//if (module != null && module.Id > 0)
//{
//data.success = await _moduleServices.Update(module);
//if (data.success)
//{
// data.msg = "更新成功";
// data.response = module?.Id.ObjToString();
//}
// }
//return data;
if (module == null || module.Id <= 0)
return Failed("缺少参数");
return await _moduleServices.Update(module) ? Success(module?.Id.ObjToString(), "更新成功") : Failed();
}
///
/// 删除一条接口
///
///
///
// DELETE: api/ApiWithActions/5
[HttpDelete]
public async Task> Delete(long id)
{
if (id <= 0)
return Failed("缺少参数");
var userDetail = await _moduleServices.QueryById(id);
if (userDetail == null)
return Failed("信息不存在");
userDetail.IsDeleted = true;
return await _moduleServices.Update(userDetail) ? Success(userDetail?.Id.ObjToString(), "删除成功") : Failed("删除失败");
//var data = new ServiceResult();
//if (id > 0)
//{
// var userDetail = await _moduleServices.QueryById(id);
// userDetail.IsDeleted = true;
// data.success = await _moduleServices.Update(userDetail);
// if (data.success)
// {
// data.msg = "删除成功";
// data.response = userDetail?.Id.ObjToString();
// }
//}
//return data;
}
///
/// 导入多条接口信息
///
///
///
// POST: api/User
[HttpPost]
public async Task> BatchPost([FromBody] List modules)
{
string ids = string.Empty;
int sucCount = 0;
for (int i = 0; i < modules.Count; i++)
{
var module = modules[i];
if (module != null)
{
module.CreateId = _user.ID;
module.CreateBy = _user.Name;
ids += (await _moduleServices.Add(module));
sucCount++;
}
}
return ids.IsNotEmptyOrNull() ? Success(ids, $"{sucCount}条数据添加成功") : Failed();
}
}
}