You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.0 KiB
63 lines
2.0 KiB
using Build_God_Api.DB;
|
|
using SqlSugar;
|
|
|
|
namespace Build_God_Api.Services
|
|
{
|
|
public interface ILevelService
|
|
{
|
|
public Task<bool> Add(Level item);
|
|
|
|
public Task<bool> Delete(int id);
|
|
|
|
public Task<bool> Update(Level item);
|
|
|
|
public Task<List<Level>> GetAll();
|
|
|
|
public Task<bool> ExistsByNameAsync(string name);
|
|
}
|
|
|
|
public class LevelService(ISqlSugarClient db, ICurrentUserService currentUserService) : ILevelService
|
|
{
|
|
private readonly ISqlSugarClient db = db;
|
|
private readonly ICurrentUserService currentUserService = currentUserService;
|
|
|
|
public async Task<bool> Add(Level item)
|
|
{
|
|
var bo = await this.ExistsByNameAsync(item.Name);
|
|
if (bo)
|
|
{
|
|
throw new Exception($"已经添加过名为{item.Name}的境界");
|
|
}
|
|
item.CreatedOn = DateTime.UtcNow;
|
|
item.CreatedBy = currentUserService.UserId;
|
|
await db.Insertable(item).ExecuteCommandAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> Delete(int id)
|
|
{
|
|
var item = await db.Queryable<Level>().FirstAsync(x => x.Id == id) ?? throw new Exception("没有找到对应境界");
|
|
await db.Deleteable(item).ExecuteCommandAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> ExistsByNameAsync(string name)
|
|
{
|
|
return await db.Queryable<Level>().AnyAsync(x => x.Name == name);
|
|
}
|
|
|
|
public async Task<List<Level>> GetAll()
|
|
{
|
|
return await db.Queryable<Level>().OrderBy(x => x.LevelId).ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> Update(Level item)
|
|
{
|
|
var level = await db.Queryable<Level>().FirstAsync(x => x.Id == item.Id) ?? throw new Exception("没有找到对应境界");
|
|
item.UpdatedOn = DateTime.UtcNow;
|
|
item.UpdatedBy = currentUserService.UserId;
|
|
await db.Updateable(item).ExecuteCommandAsync();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|