添加管理员控制器,api控制器功能
This commit is contained in:
@@ -101,12 +101,18 @@ namespace Apimanager_backend.Services
|
||||
{
|
||||
throw new BaseException(2004,"用户不存在");
|
||||
}
|
||||
user.PassHash = dto.Password;
|
||||
user.Balance = dto.Balance;
|
||||
user.PassHash = dto.Password ?? user.PassHash;
|
||||
user.Balance = dto.Balance ?? user.Balance;
|
||||
context.Users.Update(user);
|
||||
await context.SaveChangesAsync();
|
||||
return mapper.Map<UserInfoDto>(user);
|
||||
}
|
||||
#endregion
|
||||
#region 用户总数
|
||||
public async Task<int> UserCountAsync()
|
||||
{
|
||||
return await context.Users.CountAsync();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
using Apimanager_backend.Data;
|
||||
using Apimanager_backend.Dtos;
|
||||
using Apimanager_backend.Exceptions;
|
||||
using Apimanager_backend.Models;
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
public class ApiService:IApiService
|
||||
{
|
||||
private readonly ApiContext context;
|
||||
private readonly ILogger<ApiService> logger;
|
||||
private readonly IMapper mapper;
|
||||
public ApiService(ApiContext context, ILogger<ApiService> logger,IMapper mapper)
|
||||
{
|
||||
this.context = context;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
#region 添加接口
|
||||
public async Task<ApiInfoDto> AddApiAsync(CreateApiInfo dto)
|
||||
{
|
||||
var api = mapper.Map<Api>(dto);
|
||||
context.Apis.Add(api);
|
||||
await context.SaveChangesAsync();
|
||||
return mapper.Map<ApiInfoDto>(api);
|
||||
}
|
||||
#endregion
|
||||
#region 删除api
|
||||
public async Task DeleteApiAsync(int apiId)
|
||||
{
|
||||
var api = await context.Apis.FirstOrDefaultAsync(x => x.Id == apiId);
|
||||
if (api == null)
|
||||
{
|
||||
throw new BaseException(3002, "API不存在");
|
||||
}
|
||||
api.IsDelete = true;
|
||||
context.Apis.Update(api);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 查询api信息
|
||||
public async Task<ApiInfoDto> GetApiInfoAsync(int apiId)
|
||||
{
|
||||
var apiInfo = await context.Apis.FirstOrDefaultAsync(x => x.Id == apiId);
|
||||
if (apiInfo == null)
|
||||
{
|
||||
throw new BaseException(3002,"API不存在");
|
||||
}
|
||||
return mapper.Map<ApiInfoDto>(apiInfo);
|
||||
}
|
||||
#endregion
|
||||
#region 禁用api
|
||||
public async Task OffApiAsync(int apiId)
|
||||
{
|
||||
var api = await context.Apis.FirstOrDefaultAsync(x => x.Id == apiId);
|
||||
if (api == null)
|
||||
{
|
||||
throw new BaseException(3002, "API不存在");
|
||||
}
|
||||
api.IsActive = false;
|
||||
context.Apis.Update(api);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 启用API
|
||||
public async Task OnApiAsync(int apiId)
|
||||
{
|
||||
var api = await context.Apis.FirstOrDefaultAsync(x => x.Id == apiId);
|
||||
if (api == null)
|
||||
{
|
||||
throw new BaseException(3002, "API不存在");
|
||||
}
|
||||
api.IsActive = true;
|
||||
context.Apis.Update(api);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 更新接口信息
|
||||
public async Task<ApiInfoDto> UpdateApiAsync(int apiId, UpdateApiDto dto)
|
||||
{
|
||||
var api = await context.Apis.FirstOrDefaultAsync(x => x.Id == apiId);
|
||||
if(api == null)
|
||||
{
|
||||
throw new BaseException(3002, "API不存在");
|
||||
}
|
||||
api.Name = dto.Name ?? api.Name;
|
||||
api.Description = dto.Description ?? api.Description;
|
||||
api.Endpoint = dto.Endpoint ?? api.Endpoint;
|
||||
api.Method = dto.Method ?? api.Method;
|
||||
api.PackageId = dto.PackageId ?? api.PackageId;
|
||||
context.Apis.Update(api);
|
||||
await context.SaveChangesAsync();
|
||||
return mapper.Map<ApiInfoDto>(api);
|
||||
}
|
||||
#endregion
|
||||
#region 获取API列表
|
||||
public async Task<List<ApiInfoDto>> GetApisAsync(int pageIndex, int pageSize, bool desc)
|
||||
{
|
||||
IQueryable<Api> query = context.Apis.Where(x => true).OrderBy(x => x.Id);
|
||||
//倒序
|
||||
if (desc)
|
||||
{
|
||||
query.OrderDescending();
|
||||
}
|
||||
//分页
|
||||
query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||||
var list = await query.ToListAsync();
|
||||
return mapper.Map<List<ApiInfoDto>>(list);
|
||||
}
|
||||
#endregion
|
||||
#region 获取Api总数
|
||||
public async Task<int> ApiCountAsync()
|
||||
{
|
||||
return await context.Apis.CountAsync();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,11 @@ namespace Apimanager_backend.Services
|
||||
/// 修改用户信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<UserInfoDto> UpdateUserAsync(int userId,AdminUpdateUserDto dto);
|
||||
Task<UserInfoDto> UpdateUserAsync(int userId, AdminUpdateUserDto dto);
|
||||
/// <summary>
|
||||
/// 用户总数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<int> UserCountAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
using Apimanager_backend.Dtos;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
public interface IApiService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取api信息
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ApiInfoDto> GetApiInfoAsync(int apiId);
|
||||
/// <summary>
|
||||
/// 添加api
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ApiInfoDto> AddApiAsync(CreateApiInfo dto);
|
||||
/// <summary>
|
||||
/// 删除api
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <returns></returns>
|
||||
public Task DeleteApiAsync(int apiId);
|
||||
/// <summary>
|
||||
/// 更新api信息
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ApiInfoDto> UpdateApiAsync(int apiId,UpdateApiDto dto);
|
||||
/// <summary>
|
||||
/// 启用
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <returns></returns>
|
||||
public Task OnApiAsync(int apiId);
|
||||
/// <summary>
|
||||
/// 禁用
|
||||
/// </summary>
|
||||
/// <param name="apiId"></param>
|
||||
/// <returns></returns>
|
||||
public Task OffApiAsync(int apiId);
|
||||
/// <summary>
|
||||
/// 获取APi列表
|
||||
/// </summary>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
public Task<List<ApiInfoDto>> GetApisAsync(int pageIndex, int pageSize, bool desc);
|
||||
/// <summary>
|
||||
/// 获取api数量
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<int> ApiCountAsync();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Apimanager_backend.Dtos;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
public interface IPackageService
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加套餐
|
||||
/// </summary>
|
||||
/// <param name="addPackageDto"></param>
|
||||
/// <returns></returns>
|
||||
public Task<PackageInfoDto> AddPackageAsync(AddPackageDto addPackageDto);
|
||||
/// <summary>
|
||||
/// 更新套餐信息
|
||||
/// </summary>
|
||||
/// <param name="packageId"></param>
|
||||
/// <param name="updatePackageDto"></param>
|
||||
/// <returns></returns>
|
||||
public Task<PackageInfoDto> UpdatePackageAsync(int packageId,UpdatePackageDto updatePackageDto);
|
||||
/// <summary>
|
||||
/// 删除套餐
|
||||
/// </summary>
|
||||
/// <param name="packageId"></param>
|
||||
/// <returns></returns>
|
||||
public Task DeletePackageAsync(int packageId);
|
||||
/// <summary>
|
||||
/// 获取套餐列表
|
||||
/// </summary>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
public Task<List<PackageInfoDto>> GetAllPackagesAsync(int pageIndex,int pageSize,bool desc);
|
||||
/// <summary>
|
||||
/// 获取套餐信息
|
||||
/// </summary>
|
||||
/// <param name="packageId"></param>
|
||||
/// <returns></returns>
|
||||
public Task<PackageInfoDto> PackageInfoByIdAsync(int packageId);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user