public async Task <IActionResult> Edit(ApiScopeInputModel apiScopeInputModel, string button) { if (button == ControllerConstants.CANCEL) { return(RedirectToAction(nameof(Index))); } try { if (ModelState.IsValid) { await ApiScopeService.UpsertApiScopeAsync(apiScopeInputModel); return(RedirectToAction(nameof(Index))); } } catch (AlreadyExistsException ex) { ModelState.Merge(ex.ModelStateDictionary); } catch (Exception ex) { ModelState.AddModelError(ControllerConstants.ERROR, ex.Message); } return(View(apiScopeInputModel)); }
public void GivenItHasAnEmail_WhenICallInserUser_AndTheEmailAlreadyExists_ThenShouldThrowAlreadyExistsException() { // Given var apiScopeInputModel = new ApiScopeInputModel() { Name = "Name", DisplayName = "DisplayName" }; ApiScopeDataAccessMock .Setup(dataAccess => dataAccess.InsertAsync(It.IsAny <ApiScopeData>())) .Throws(MongoWriteException); // When Assert.Throws(typeof(AlreadyExistsException), () => ApiScopeService.UpsertApiScopeAsync(apiScopeInputModel).GetAwaiter().GetResult()); }
public IActionResult Edit(int id, ApiScopeInputModel input) { if (!ModelState.IsValid) { return(View(input)); } var apiScope = _apiScopeService.GetApiScope(id); _mapper.Map(input, apiScope); _apiScopeService.SaveChanges(); _logger.LogInformation("{user} edited ApiScope {apiScope}", User.Identity.Name, id); return(RedirectToAction("Index")); }
public IActionResult Add(ApiScopeInputModel input) { if (!ModelState.IsValid) { return(View(input)); } var apiScope = _mapper.Map <ApiScope>(input); _apiScopeService.AddApiScope(apiScope); _apiScopeService.SaveChanges(); _logger.LogInformation("{user} created apiScope {apiScope}", User.Identity.Name, apiScope.Name); return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(string id) { var apiScopeInputModel = new ApiScopeInputModel(); if (!string.IsNullOrWhiteSpace(id)) { var scope = await ApiScopeService.GetApiScopeById(id); apiScopeInputModel.Id = scope.Id; apiScopeInputModel.Name = scope.Name; apiScopeInputModel.DisplayName = scope.DisplayName; apiScopeInputModel.Description = scope.Description; } return(View(apiScopeInputModel)); }
public void GivenItHasANewApiScope_WhenItCallsInsertApiScopes_ThenShouldInsert() { // Given var apiScopeInputModel = new ApiScopeInputModel() { Name = "API1", DisplayName = "API 1" }; ApiScopeDataAccessMock .Setup(apiScopeDataAccess => apiScopeDataAccess.InsertAsync(It.IsAny <ApiScopeData>())) .Returns(Task.CompletedTask); // When ApiScopeService.UpsertApiScopeAsync(apiScopeInputModel).GetAwaiter().GetResult(); // Then ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.InsertAsync(It.IsAny <ApiScopeData>()), Times.Once); ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.ReplaceAsync(It.IsAny <ApiScopeData>(), ExpressionItsAny), Times.Never); }
public async Task UpsertApiScopeAsync(ApiScopeInputModel apiScopeInputModel) { try { var apiScopeData = new ApiScopeData(apiScopeInputModel.Id, apiScopeInputModel.Name, apiScopeInputModel.DisplayName, apiScopeInputModel.Description); if (string.IsNullOrEmpty(apiScopeData.Id)) { await ApiScopeDataAccess.InsertAsync(apiScopeData); } else { await ApiScopeDataAccess.ReplaceAsync(apiScopeData, data => data.Id == apiScopeData.Id); } } catch (MongoWriteException ex) { ex.ThrowIfDuplicateKey(nameof(apiScopeInputModel.Name), $"O nome '{apiScopeInputModel.Name} já existe."); throw ex; } }
public void GivenItHasApiScope_WhenItCallsUpsertApiScopes_ThenShouldCallReplace() { // Given var apiScopeInputModel = new ApiScopeInputModel() { Id = "1", Name = "API1", DisplayName = "API 1" }; ApiScopeDataAccessMock .Setup(apiScopeDataAccess => apiScopeDataAccess.ReplaceAsync(It.IsAny <ApiScopeData>(), It.IsAny <Expression <Func <ApiScopeData, bool> > >())) .ReturnsAsync(true); // When ApiScopeService.UpsertApiScopeAsync(apiScopeInputModel).GetAwaiter().GetResult(); // Then ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.ReplaceAsync(It.IsAny <ApiScopeData>(), It.IsAny <Expression <Func <ApiScopeData, bool> > >()), Times.Once); ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.InsertAsync(It.IsAny <ApiScopeData>()), Times.Never); }