public async Task <IActionResult> UpdateSource(int id, [FromBody] UpdateSubjectDTO source) { try { if (source == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest("Invalid model object")); } var sourceEntity = await _repositoryWrapper.SubjectRepo.GetDataByIdAsync(id); if (sourceEntity == null) { return(NotFound()); } _mapper.Map(source, sourceEntity); _repositoryWrapper.SubjectRepo.UpdateData(sourceEntity); await _repositoryWrapper.SaveAsync(); return(Ok("Update successfully!")); } catch (Exception ex) { //_logger.LogError($"Something went wrong inside CreateSources action: {ex.Message}"); if (ex.InnerException != null) { return(BadRequest(ex.Message + "," + ex.InnerException.Message)); } return(BadRequest(ex.Message)); } }
private async void AddSubject_Click(object sender, RoutedEventArgs e) { object @object; if (this.VM.IsItemSelected) { @object = new UpdateSubjectDTO { SubjectName = SubjectName.Text, Description = Description.Text, SubjectId = this.VM.SubjectId }; } else { @object = new AddSubjectDTO { SubjectName = SubjectName.Text, Description = Description.Text, }; } try { if (this.VM.IsItemSelected) { await AppData.Client.PutAsync(Config.REST_Endpoints_Subjects, @object); } else { await AppData.Client.Post(Config.REST_Endpoints_Subjects, @object); } await AppData.RefreshSubjectAsync(); this.VM.Subjects = AppData.Subjects; this.VM.SubjectName = string.Empty; this.VM.Description = string.Empty; } catch (Exception ex) { string title = $"OH SNAP! Failed to add the following subject : {((Subject)@object).SubjectName}"; string msg = $"Couldn't add subject : {((Subject)@object).ToString()}. \n Exception : {ex.Message}. \n Inner Exception : {ex.InnerException?.Message}."; var dialog = new MessageDialog(msg, title); await dialog.ShowAsync(); // Log error } }
public IActionResult UpdateSubject(int subjectId, [FromBody] UpdateSubjectDTO updateSubjectDTO) { if (updateSubjectDTO == null || subjectId != updateSubjectDTO.Id) { return(BadRequest(ModelState)); } var subjectObj = _mapper.Map <Subject>(updateSubjectDTO); if (!_sRepo.UpdateSubject(subjectObj)) { ModelState.AddModelError("", $"Something went wrong when updating the record {subjectObj.Title}"); return(StatusCode(500, ModelState)); } return(NoContent()); }
public async Task <ActionResult> UpdateSubjectAsync(UpdateSubjectDTO subject) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var s = _mapper.Map <Subject>(subject); try { await this._service.UpdateAsync(s); await this._service.SaveAsync(); } catch (Exception ex) { return(Problem(detail: $"Failed to update subject ({s.SubjectName}). Error: {ex.Message}. InnerException: {ex.InnerException?.Message}")); } return(Ok()); }