ql_apimanager_backend/Apimanager_backend/Services/ApiService.cs

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
}
}