public async Task <IActionResult> Update(Guid id, [FromBody] Dictionary <string, string> data) { IActionResult result = null; bool locked = default; try { if (result == null && data == null) { result = BadRequest("Missing patch data"); } if (result == null && (!data.ContainsKey("Locked") || string.IsNullOrEmpty(data["Locked"]))) { result = BadRequest("Missing Locked value"); } if (result == null && Guid.Empty.Equals(id)) { result = BadRequest("Invalid account id"); } if (result == null && !UserCanAccessAccount(id)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null && !bool.TryParse(data["Locked"], out locked)) { result = BadRequest("Invalid locked value. Expecting 'True' or 'False'"); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = await accountFactory.Get(settings, id); if (innerAccount == null) { result = NotFound(); } else { IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.UpdateLocked(settings, innerAccount.AccountId, locked); result = Ok(); } } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Update(Guid id, [FromBody] Account account) { IActionResult result = null; try { if (result == null && account == null) { result = BadRequest("Missing account data"); } if (result == null && string.IsNullOrEmpty(account.Name)) { result = BadRequest("Missing account name"); } if (result == null && Guid.Empty.Equals(id)) { result = BadRequest("Invalid account id"); } if (result == null && !UserCanAccessAccount(id)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = await accountFactory.Get(settings, id); if (innerAccount == null) { result = NotFound(); } else { IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Account, IAccount>(account, innerAccount); IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.Update(settings, innerAccount); result = Ok( mapper.Map <Account>(innerAccount) ); } } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> Create([FromBody] Account account) { IActionResult result = null; try { if (result == null && account == null) { result = BadRequest("Missing account data"); } if (result == null && string.IsNullOrEmpty(account.Name)) { result = BadRequest("Missing account name"); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IUser user = await GetUser(scope.Resolve <IUserFactory>(), settings); IAccountFactory accountFactory = scope.Resolve <IAccountFactory>(); IAccount innerAccount = accountFactory.Create(); IMapper mapper = MapperConfigurationFactory.CreateMapper(); mapper.Map <Account, IAccount>(account, innerAccount); IAccountSaver saver = scope.Resolve <IAccountSaver>(); await saver.Create(settings, user.UserId, innerAccount); result = Ok( mapper.Map <Account>(innerAccount) ); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
public async Task <IActionResult> DeleteUser([FromRoute] Guid?accountId, [FromRoute] Guid?userId) { IActionResult result = null; try { if (result == null && (!accountId.HasValue || Guid.Empty.Equals(accountId.Value))) { result = BadRequest("Missing account id parameter value"); } if (result == null && (!userId.HasValue || Guid.Empty.Equals(userId.Value))) { result = BadRequest("Missing user id parameter value"); } if (result == null && !UserCanAccessAccount(accountId.Value)) { result = StatusCode(StatusCodes.Status401Unauthorized); } if (result == null) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>(); CoreSettings settings = settingsFactory.CreateAccount(_settings.Value); IAccountSaver accountSaver = scope.Resolve <IAccountSaver>(); await accountSaver.RemoveUser(settings, userId.Value, accountId.Value); result = Ok(); } } } catch (Exception ex) { using (ILifetimeScope scope = _container.BeginLifetimeScope()) { await LogException(ex, scope.Resolve <IExceptionService>(), scope.Resolve <SettingsFactory>(), _settings.Value); } result = StatusCode(StatusCodes.Status500InternalServerError); } return(result); }
private async Task AddAccountUser(IUserFactory userFactory, IAccountSaver accountSaver, CoreSettings settings, Guid accountId) { IUser user = await GetUser(userFactory, settings); await accountSaver.AddUser(settings, user.UserId, accountId); }