File size: 4,003 Bytes
5fc700d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | using ToolHub.Models;
using System.Security.Cryptography;
using System.Text;
namespace ToolHub.Services;
public class UserService : IUserService
{
private readonly IFreeSql _freeSql;
public UserService(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public async Task<User?> GetUserByIdAsync(int id)
{
return await _freeSql.Select<User>()
.Where(u => u.Id == id && u.IsActive)
.FirstAsync();
}
public async Task<User?> GetUserByEmailAsync(string email)
{
return await _freeSql.Select<User>()
.Where(u => u.Email == email && u.IsActive)
.FirstAsync();
}
public async Task<bool> CreateUserAsync(User user)
{
try
{
user.PasswordHash = HashPassword(user.PasswordHash);
await _freeSql.Insert(user).ExecuteAffrowsAsync();
return true;
}
catch
{
return false;
}
}
public async Task<bool> UpdateUserAsync(User user)
{
try
{
user.UpdatedAt = DateTime.Now;
await _freeSql.Update<User>()
.SetSource(user)
.ExecuteAffrowsAsync();
return true;
}
catch
{
return false;
}
}
public async Task<bool> VerifyPasswordAsync(string email, string password)
{
var user = await GetUserByEmailAsync(email);
if (user == null) return false;
return VerifyPassword(password, user.PasswordHash);
}
public async Task<List<Tool>> GetUserFavoritesAsync(int userId)
{
var favorites = await _freeSql.Select<UserFavorite>()
.Include(uf => uf.Tool.Category)
.Where(uf => uf.UserId == userId)
.OrderByDescending(uf => uf.CreatedAt)
.ToListAsync();
return favorites.Select(uf => uf.Tool).ToList();
}
public async Task<bool> AddFavoriteAsync(int userId, int toolId)
{
try
{
var existing = await _freeSql.Select<UserFavorite>()
.Where(uf => uf.UserId == userId && uf.ToolId == toolId)
.FirstAsync();
if (existing != null) return true;
await _freeSql.Insert(new UserFavorite
{
UserId = userId,
ToolId = toolId
}).ExecuteAffrowsAsync();
// 更新工具收藏数
await _freeSql.Update<Tool>()
.Where(t => t.Id == toolId)
.Set(t => t.FavoriteCount + 1)
.ExecuteAffrowsAsync();
return true;
}
catch
{
return false;
}
}
public async Task<bool> RemoveFavoriteAsync(int userId, int toolId)
{
try
{
await _freeSql.Delete<UserFavorite>()
.Where(uf => uf.UserId == userId && uf.ToolId == toolId)
.ExecuteAffrowsAsync();
// 更新工具收藏数
await _freeSql.Update<Tool>()
.Where(t => t.Id == toolId)
.Set(t => t.FavoriteCount - 1)
.ExecuteAffrowsAsync();
return true;
}
catch
{
return false;
}
}
public async Task<bool> IsFavoriteAsync(int userId, int toolId)
{
var favorite = await _freeSql.Select<UserFavorite>()
.Where(uf => uf.UserId == userId && uf.ToolId == toolId)
.FirstAsync();
return favorite != null;
}
private string HashPassword(string password)
{
using var sha256 = SHA256.Create();
var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password + "ToolHub_Salt"));
return Convert.ToBase64String(hashedBytes);
}
private bool VerifyPassword(string password, string hash)
{
var computedHash = HashPassword(password);
return computedHash == hash;
}
}
|