using Apimanager_backend.Config; using Apimanager_backend.Data; using Apimanager_backend.Dtos; using Apimanager_backend.Exceptions; using Apimanager_backend.Models; using AutoMapper; using Microsoft.AspNetCore.Connections.Features; using Microsoft.EntityFrameworkCore; using System.ComponentModel; namespace Apimanager_backend.Services { public class UserService : IUserService { private readonly ApiContext apiContext; private readonly IMapper mapper; public UserService(ApiContext apiContext,IMapper automapper) { this.apiContext = apiContext; this.mapper = automapper; } public async Task GetUserAsync(int userId) { User? user = await apiContext.Users.SingleOrDefaultAsync(x => x.Id == userId); //未找到用户 if (user == null) { throw new BaseException(2004, "User not found"); } return mapper.Map(user); } public async Task IsEmailExist(string email) { return await apiContext.Users.AnyAsync(x => x.Email == email); } public async Task IsUsernameExist(string username) { return await apiContext.Users.AnyAsync(x => x.Username == username); } public Task ResetPasswordAsync(string email, string token, string newPassword) { throw new NotImplementedException(); } public Task SendResetPasswordEmailAsync(string email) { throw new NotImplementedException(); } public async Task UpdateUserAsync(UpdateUserDto dto) { var user = await apiContext.Users.FirstOrDefaultAsync(x => x.Id == dto.userId); if (user == null) { throw new BaseException(2004, "用户不存在"); } user.PassHash = dto.password == null ? user.PassHash : dto.password; apiContext.Users.Update(user); await apiContext.SaveChangesAsync(); return mapper.Map(user); } } }