public async Task <IServiceResult> SignupAsync(SignupBindingModel signupModel) { var username = _randomMaker.NewNumber(); var duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username }); while (duplicated != null) { username = _randomMaker.NewNumber(); duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username }); } using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { var now = DateTime.UtcNow; var account = new AccountAddSchema { Password = _cryptograph.RNG(signupModel.Password), ProviderId = AccountProvider.Clipboardy.ToInt(), Username = username, CreatedAt = now, StatusId = Status.Active.ToInt() }; var accountId = await AddAsync(account); var accountDevice = new AccountDeviceAddSchema { AccountId = accountId, DeviceId = signupModel.DeviceId, DeviceName = signupModel.DeviceName, DeviceType = signupModel.DeviceType, CreatedAt = now, StatusId = Status.Active.ToInt() }; var deviceId = await _accountDeviceService.AddAsync(accountDevice); var accountProfile = new AccountProfileAddSchema { AccountId = accountId, LinkedId = string.IsNullOrWhiteSpace(signupModel.Email) ? signupModel.Phone : signupModel.Email, TypeId = string.IsNullOrWhiteSpace(signupModel.Email) ? AccountProfileType.Phone.ToInt() : AccountProfileType.Email.ToInt(), CreatedAt = now, StatusId = Status.Active.ToInt() }; await _accountProfileService.AddAsync(accountProfile); transaction.Complete(); var token = _jwtHandler.Bearer(new Account(accountId, deviceId, username, now).ToClaimsIdentity()); return(DataTransferer.Ok(token)); } catch (Exception ex) { var errmsg = "Something went wrong."; Log.Error(ex, errmsg); return(DataTransferer.InternalServerError(ex)); } } }
public async Task <IServiceResult> ExternalSignupAsync(ExternalUserBindingModel externalUser) { var username = _randomMaker.NewNumber(); var duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username }); while (duplicated != null) { username = _randomMaker.NewNumber(); duplicated = await FirstAsync(new AccountGetFirstSchema { Username = username }); } using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { var now = DateTime.UtcNow; var account = new AccountAddSchema { ProviderId = externalUser.ProviderId.ToInt(), Username = username, CreatedAt = now, StatusId = Status.Active.ToInt() }; var accountId = await AddAsync(account); var accountDevice = new AccountDeviceAddSchema { AccountId = accountId, DeviceId = externalUser.DeviceId, DeviceName = externalUser.DeviceName, DeviceType = externalUser.DeviceType, CreatedAt = now, StatusId = Status.Active.ToInt() }; var deviceId = await _accountDeviceService.AddAsync(accountDevice); var accountProfile = new AccountProfileAddSchema { AccountId = accountId, TypeId = AccountProfileType.Email.ToInt(), LinkedId = externalUser.Email, CreatedAt = now, StatusId = Status.Active.ToInt() }; await _accountProfileService.AddAsync(accountProfile); transaction.Complete(); var token = _jwtHandler.Bearer(new Account(accountId, deviceId, username, now).ToClaimsIdentity()); return(DataTransferer.Ok(token)); } catch (Exception ex) { var errmsg = "Something went wrong."; Log.Error(ex, errmsg); return(null); } } }
public void Add() { var accountDevice = new AccountDeviceAddSchema { AccountId = 8, DeviceId = Guid.NewGuid().ToString(), DeviceName = "DeviceName", DeviceType = "DeviceType", CreatedAt = DateTime.Now, StatusId = Status.Active.ToInt() }; var accountDeviceId = _accountDeviceService.AddAsync(accountDevice).GetAwaiter().GetResult(); Assert.IsTrue(accountDevice.StatusCode == 200); Assert.IsTrue(accountDeviceId > 0); }
public async Task <int> AddAsync(AccountDeviceAddSchema accountDevice) { var result = await _storedProcedure.ExecuteScalarAsync <AccountDeviceAddSchema, int>(accountDevice); return(result); }