public async Task <ServiceResponse <GetCourseDTO> > UpdateCourse(UpdateCourseDTO updatedCourse) { ServiceResponse <GetCourseDTO> serviceResponse = new ServiceResponse <GetCourseDTO>(); try { // Grab the specific Course from the database asynchronously. Course course = await _context.Courses.FirstOrDefaultAsync(c => c.Id == updatedCourse.Id); course.Name = updatedCourse.Name; course.Summary = updatedCourse.Summary; // Update the specific Course in the database _context.Courses.Update(course); // Save the changes in the database await _context.SaveChangesAsync(); serviceResponse.Message = "Course update successful."; serviceResponse.Data = _mapper.Map <GetCourseDTO>(course); } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; } return(serviceResponse); }
public async Task <IActionResult> UpdateSource(int id, [FromBody] UpdateCourseDTO source) { try { if (source == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest("Invalid model object")); } var sourceEntity = await _repositoryWrapper.CourseRepo.GetDataByIdAsync(id); if (sourceEntity == null) { return(NotFound()); } _mapper.Map(source, sourceEntity); _repositoryWrapper.CourseRepo.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)); } }
public async Task <IActionResult> UpdateCourse(UpdateCourseDTO updatedCourse) { ServiceResponse <GetCourseDTO> response = await _courseService.UpdateCourse(updatedCourse); if (response.Data == null) { return(NotFound(response)); } return(Ok(response)); }
public async Task <CourseDTO> UpdateCourse(UpdateCourseDTO updateCourse) { var course = await _repo.GetByIdAsync(updateCourse.Id); try { if (course != null) { course.ImageUrl = updateCourse.ImageUrl; course.Description = updateCourse.Description; course.CourseName = updateCourse.CourseName; } _repo.Edit(course); return(_mapper.Map <CourseDTO>(course)); } catch (Exception) { return(null); } }