Files
BuYuHouDuan/AdminSrc/AdminApi/GameConfigs/BotConfigController.cs
T

76 lines
2.4 KiB
C#

using Common;
using CommonDomain;
using CommonInfrastructure;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using MongoRepository;
namespace AdminApi.GameConfigs
{
[ApiController, Route("adminapi/[controller]")]
public class BotConfigController : BaseController
{
private readonly IMongoBaseRepository<BotProfileEntity> _botRepo;
public BotConfigController(IMongoBaseRepository<BotProfileEntity> botRepo)
{
_botRepo = botRepo;
}
/// <summary>
/// 获取所有机器人配置
/// </summary>
[HttpGet("GetAll")]
public async Task<BaseResponse<List<BotProfileEntity>>> GetAll()
{
var list = await _botRepo.FilterByAsync(_ => true);
return this.ToResult(list.ToList());
}
/// <summary>
/// 添加机器人配置
/// </summary>
[HttpPost("Add")]
public async Task<BaseResponse<string>> Add(BotProfileEntity entity)
{
if (string.IsNullOrWhiteSpace(entity.Name))
return this.ToFailResult<string>("名称不能为空");
await _botRepo.InsertOneAsync(entity);
return this.ToResult("添加成功");
}
/// <summary>
/// 更新机器人配置
/// </summary>
[HttpPost("Update")]
public async Task<BaseResponse<string>> Update(BotProfileEntity entity)
{
if (string.IsNullOrWhiteSpace(entity.Id))
return this.ToFailResult<string>("缺少Id");
if (string.IsNullOrWhiteSpace(entity.Name))
return this.ToFailResult<string>("名称不能为空");
var update = Builders<BotProfileEntity>.Update
.Set(x => x.Name, entity.Name)
.Set(x => x.Avatar, entity.Avatar ?? string.Empty);
await _botRepo.UpdateByIdAsync(entity.Id, update);
return this.ToResult("更新成功");
}
/// <summary>
/// 删除机器人配置
/// </summary>
[HttpPost("Delete")]
public async Task<BaseResponse<string>> Delete([FromQuery] string id)
{
if (string.IsNullOrWhiteSpace(id))
return this.ToFailResult<string>("缺少Id");
await _botRepo.DeleteByIdAsync(id);
return this.ToResult("删除成功");
}
}
}