157 lines
5.5 KiB
C#
157 lines
5.5 KiB
C#
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
|
|
#region 通过Api路径获取api信息
|
|
public async Task<ApiInfoDto> GetApiInfoByEndpointAsync(string endpoint)
|
|
{
|
|
var apiInfo = await context.Apis.FirstOrDefaultAsync(x => x.Endpoint == endpoint);
|
|
if (apiInfo == null)
|
|
{
|
|
throw new BaseException(3002, "API不存在");
|
|
}
|
|
return mapper.Map<ApiInfoDto>(apiInfo);
|
|
}
|
|
#endregion
|
|
#region 获取用于用户展示的api列表
|
|
public async Task<List<ApiInfoDto>> GetUserApisAsync(int pageIndex, int pageSize, bool desc)
|
|
{
|
|
IQueryable<Api> query = context.Apis.Include(x => x.ApiRequestExamples).AsQueryable();
|
|
//倒序
|
|
if (desc)
|
|
{
|
|
query.OrderDescending();
|
|
}
|
|
//分页
|
|
query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
|
return mapper.Map<List<ApiInfoDto>>(await query.ToListAsync());
|
|
}
|
|
#endregion
|
|
#region 获取API对应的套餐
|
|
public async Task<List<Apipackage>> GetApipackageAsync(int[] apiId)
|
|
{
|
|
if(apiId.Count() == 0)
|
|
{
|
|
throw new BaseException(1004,"id不能为空");
|
|
}
|
|
var packageItems = await context.apiPackageItems.Include(x => x.ApiPackage).Where(x => apiId.Contains(x.ApiId)).ToListAsync();
|
|
return packageItems.Select(x => x.ApiPackage).ToList();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|