using Knoks.Core.Data.Interfaces; using Knoks.Core.Entities; using Knoks.Core.Entities.Args; using Knoks.Framework.DataAccess; using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Knoks.Core.Data.Dao { public class UserDao : IUserDao { private readonly IProcExecutor _executor; private readonly ILogger _logger; public UserDao(ILogger logger, IProcExecutor executor) { _logger = logger; _executor = executor; } public async Task UserNameExists(string userName, long? userId) { var result = await _executor.Go("USR_UserNameExists", new { UserName = userName, UserId = userId }); return (result.ReturnValue ?? 0) == 1; } public async Task UserEmailExists(string email) { var result = await _executor.Go("USR_UserEmailExists", new { Email = email }); return (result.ReturnValue ?? 0) == 1; } public async Task<(User user, Account account)> CreateUser(CreateUserArgs args, string password, decimal freeCreditAmount) { var dbTables = await _executor.Go("USR_Users_RegisterNewUser", args, new { Password = password, NewUserFreeCreditAmount = freeCreditAmount }); return ( dbTables.Тables[2][0].To(), dbTables.Тables[3][0].To() ); } public async Task UpdateUser(UpdateUserArgs args) { return (await _executor.Go("USR_UpdateUser", args)).Тables[0][0].To(); } public async Task UpdateUserSucsess(long args) { return (await _executor.Go("UpdateUserSucsess", args)).Тables[0][0].To(); } public async Task> GetUsers(long? userId = null, byte? userTypeId = null) { _logger.LogDebug("GetUsers : {0}", userId); return (await _executor.Go("REG_GetUsers", new { userId, userTypeId })).Тables[0].Rows.Select(row => row.To()); } public async Task GetUser(long userId) { var dbTables = await _executor.Go("BO_GetUser"); var user = dbTables.Тables[0][0].To(); return user; } public async Task GetUserByEmail(string email) { var dbTables = await _executor.Go("BO_GetUserByEmail", new { Email = email}); var user = dbTables.Тables[0][0].To(); return user; } public async Task GetUserCredential(int apiConsumerId, string email) { _logger.LogDebug($"GetUserCredential: {nameof(apiConsumerId)}='{apiConsumerId}', {nameof(email)}='{email}'"); var dataResult = await _executor.Go("REG_GetUserCredential", new { apiConsumerId, email }); return dataResult.Тables[0].Rows.Count != 1 ? null : new UserCredential { User = dataResult.Тables[0][0].To(), Password = dataResult.OutputParams["password"].ToString() }; } public async Task GetUserInfo(string accountWalletAddress) { _logger.LogDebug($"Get User Info for wallet address: {accountWalletAddress}"); var result = await _executor.Go("API_GetUserInfo", new { accountWalletAddress }); return new UserAccounts { Accounts = result.Тables[0].Rows.Select(r => r.To()), User = result.Тables.Count > 1 ? result.Тables[1][0].To() : null }; } public async Task CheckPasswordResetToken(string token) { var result = await _executor.Go("BO_CheckPasswordResetToken", new { Token = token }); return result.ReturnValue == 1; } public async Task SendPasswordResetLink(string email, string token) { await _executor.Go("BO_SetPasswordResetToken", new { Email = email, Token = token }); } public async Task PasswordReset(string token, string newPassword) { var result = await _executor.Go("BO_PasswordReset", new { Token = token, NewPassword = newPassword }); return result.ReturnValue == 1; } } }