public BoardMeetingsModel(ApiClient apiClient, IMapper mapper, IAzureSASTokenUrl azureSASTokenUrl, IAzureBlob azureBlob, IFileValidate fileValidate, ILogger <BoardMeetingsModel> logger) { _apiClient = apiClient; _mapper = mapper; _azureSASTokenUrl = azureSASTokenUrl; _azureBlob = azureBlob; _fileValidate = fileValidate; _logger = logger; //need to initialize an empty object because by default the property is null //within the view, when you try and access the objects underlying properties, razor will throw an error //because the property has not been initialized MeetingToCreateEdit = new BoardMeetingManipulationDto(); FileUploaded = new RequiredFileDto(); }
public async Task <IActionResult> OnPostEditMeetingAsync(CancellationToken cancellationToken, BoardMeetingManipulationDto MeetingToCreateEdit) { if (!ModelState.IsValid) { //set the pagemodel property to be the values submitted by the user //not doing this will cause all model state errors to be lost this.MeetingToCreateEdit = MeetingToCreateEdit; return(Partial("_CreateEditBoardMeetingPartial", this)); } var baseAPIUrl = $"api/boardmeetings/{Id}"; try { var meetingToUpdate = _mapper.Map <BoardMeetingUpdateDto>(MeetingToCreateEdit); //get the response with authorization as the API endpoint requires an authenticated user var response = await(await _apiClient.WithAuthorization()).PutAsJsonAsync <BoardMeetingUpdateDto>(baseAPIUrl, meetingToUpdate, cancellationToken); //return the same page with an error message if the user is trying to call the API too many times if (response.StatusCode == HttpStatusCode.TooManyRequests) { TempData["TooManyRequests"] = "Too many requests. Please slow down with your requests"; return(Partial("_CreateEditBoardMeetingPartial", this)); } //ensure success status code else throw an exception response.EnsureSuccessStatusCode(); } catch (HttpRequestException ex) { _logger.LogError($"An error occured accessing the API. Url: {HttpContext.Request.GetDisplayUrl()}" + $" Error Message: {ex.Message}"); //either the API is not running or an error occured on the server TempData["Error"] = "An error occured while processing your request. Please try again later."; return(Partial("_CreateEditBoardMeetingPartial", this)); } return(Partial("_CreateEditBoardMeetingPartial", this)); }