public async void UpdateSucess(string result) { UpdateCourseModel = new UpdateCourseModel(); await App.Current.MainPage.DisplayAlert("HMS", result, "OK"); OnPropertyChanged("UpdateCourseModel"); }
public CoursesControllerTests(EMSFixture _fixture) { fixture = _fixture; updateCourseModel = new UpdateCourseModel(); createCourseModel = new CreatingCourseModel(); mockRepo = new Mock <ICourseService>(); controller = new CoursesController(mockRepo.Object); courseModel = Mapper.Map <UpdateCourseModel, Course>(updateCourseModel); }
public IActionResult UpdateCourse(UpdateCourseModel updateCourseModel, int id) { Course course = new Course { Id = id, Subject = updateCourseModel.Subject, TeacherName = updateCourseModel.TeacherName, Inform = updateCourseModel.Inform, }; //if (!Validator.IsValid(course)) // return NotFound(); _courseRepository.Update(course, id); return(Ok()); }
public async Task <IActionResult> UpdateCourse([FromBody] UpdateCourseModel updateCourseModel, Guid id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var course = await courseService.FindById(id); if (course == null) { return(NotFound()); } var courseModel = Mapper.Map <UpdateCourseModel, Course>(updateCourseModel); await courseService.Update(id, courseModel); return(NoContent()); }
public async void UpdateCourse(UpdateCourseModel updateCourseModel) { ProfileUpdateResponse profileUpdateResponse; ProfileUpdateErrorresponse profileUpdateErrorresponse; try { UserDialogs.Instance.ShowLoading(); var client = new HttpClient(); client.BaseAddress = new Uri(ApplicationURL.BaseURL); string jsn = JsonConvert.SerializeObject(updateCourseModel); var content = new StringContent(jsn, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(ApplicationURL.UpdateCourse, content); if ((int)response.StatusCode == 200) { UserDialogs.Instance.HideLoading(); string resultHostel = await response.Content.ReadAsStringAsync(); profileUpdateResponse = JsonConvert.DeserializeObject <ProfileUpdateResponse>(resultHostel); ieditcourse.UpdateSucess(profileUpdateResponse.message); } else { UserDialogs.Instance.HideLoading(); string resultHostel = await response.Content.ReadAsStringAsync(); profileUpdateErrorresponse = JsonConvert.DeserializeObject <ProfileUpdateErrorresponse>(resultHostel); ieditcourse.servicefailed(profileUpdateErrorresponse.errors[0].message); } } catch (Exception ex) { UserDialogs.Instance.HideLoading(); await App.Current.MainPage.DisplayAlert("HMS", ex.ToString(), "OK"); } }
public async Task <IActionResult> Update([FromForm] UpdateCourseModel cm) { var owner = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); var createCourseModel = new CreateCourseModel { Name = cm.Name, Description = cm.Description, Lessons = cm.Lessons, Exercises = cm.Exercises, Files = (List <IFormFile>)cm.Files }; bool isValid = validateCreateCourseModel(createCourseModel, owner); if (!isValid) { return(Json(new { message = "Course update failed!" })); } var appPath = PlatformServices.Default.Application.ApplicationBasePath; appPath = appPath.Remove(appPath.Length - 24); var updateCourse = db.Courses.FirstOrDefault(c => c.Id == cm.Id); updateCourse.Name = cm.Name; updateCourse.Description = cm.Description; db.Courses.Update(updateCourse); db.SaveChanges(); var existingResourceIds = db.Lessons.Where(l => l.CourseId == updateCourse.Id).Select(l => l.ResourceId).ToList(); foreach (int resourceId in existingResourceIds) { string path = db.Resources.Where(r => r.Id == resourceId).Select(r => r.Path).ToString(); System.IO.File.Delete(@path); } JObject toRemove = JObject.Parse(cm.Removed); foreach (JValue lessonId in toRemove.GetValue("lessonIds")) { int id = int.Parse(lessonId.ToString()); var removeLesson = db.Lessons.FirstOrDefault(a => a.Id == id); var removeResource = db.Resources.FirstOrDefault(r => r.Id == removeLesson.ResourceId); db.Lessons.Remove(removeLesson); db.SaveChanges(); db.Resources.Remove(removeResource); db.SaveChanges(); } foreach (JValue exerciseId in toRemove.GetValue("exerciseIds")) { int id = int.Parse(exerciseId.ToString()); var removeExercise = db.Exercises.First(a => a.Id == id); db.Exercises.Remove(removeExercise); db.SaveChanges(); } foreach (JValue questionId in toRemove.GetValue("questionIds")) { int id = int.Parse(questionId.ToString()); var removeQuestion = db.Questions.First(a => a.Id == id); db.Questions.Remove(removeQuestion); db.SaveChanges(); } foreach (JValue answerId in toRemove.GetValue("answerIds")) { int id = int.Parse(answerId.ToString()); var removeAnswer = db.Answers.First(a => a.Id == id); db.Answers.Remove(removeAnswer); db.SaveChanges(); } var courseId = updateCourse.Id; int index = 0; //used for iterating through files foreach (string lesson in cm.Lessons) { using (var stream = new FileStream("./App_Data/Resources/" + updateCourse.Id + "-" + index + ".pdf", FileMode.Create))//create new file { await cm.Files[index].CopyToAsync(stream); } JToken result; JObject parsedObject = JObject.Parse(lesson); if (parsedObject.TryGetValue("id", out result)) { int lessonId = int.Parse(parsedObject.GetValue("id").ToString()); var updateLesson = db.Lessons.FirstOrDefault(l => l.Id == lessonId); updateLesson.Name = parsedObject.GetValue("name").ToString(); updateLesson.Description = parsedObject.GetValue("description").ToString(); db.Lessons.Update(updateLesson); db.SaveChanges(); var updateResource = db.Resources.FirstOrDefault(r => r.Id == updateLesson.ResourceId); updateResource.Name = cm.Files[index].FileName; updateResource.Path = "./App_Data/Resources/" + updateCourse.Id + "-" + index + ".pdf"; db.Resources.Update(updateResource); db.SaveChanges(); } else { var newResource = new Resource(); { newResource.Path = "./App_Data/Resources/" + updateCourse.Id + "-" + index + ".pdf"; newResource.Name = cm.Files[index].FileName; } db.Resources.Add(newResource); db.SaveChanges(); int resourceId = newResource.Id; var newLesson = new Lesson { Name = parsedObject.GetValue("name").ToString(), Description = parsedObject.GetValue("description").ToString(), CourseId = courseId, ResourceId = resourceId // if its new lesson cm[Files] will be different than null since user will have to provide a file }; db.Lessons.Add(newLesson); db.SaveChanges(); } index++; } if (cm.Exercises != null) { foreach (string exercise in cm.Exercises) { int exerciseId; JToken result; JObject parsedObject = JObject.Parse(exercise); if (parsedObject.TryGetValue("id", out result)) { var updateExercise = db.Exercises.FirstOrDefault(l => l.Id == int.Parse(parsedObject.GetValue("id").ToString())); updateExercise.Name = parsedObject.GetValue("name").ToString(); updateExercise.Description = parsedObject.GetValue("description").ToString(); db.Exercises.Update(updateExercise); db.SaveChanges(); exerciseId = updateExercise.Id; } else { var newExercise = new Exercise { Name = parsedObject.GetValue("name").ToString(), Description = parsedObject.GetValue("description").ToString(), CourseId = updateCourse.Id }; db.Exercises.Add(newExercise); db.SaveChanges(); exerciseId = newExercise.Id; } foreach (JObject question in parsedObject.GetValue("questions")) { int questionId; if (question.TryGetValue("id", out result)) { var updateQuestion = db.Questions.FirstOrDefault(q => q.Id == int.Parse(question.GetValue("id").ToString())); updateQuestion.Sentence = question.GetValue("sentence").ToString(); updateQuestion.Points = float.Parse(question.GetValue("points").ToString()); db.Questions.Update(updateQuestion); db.SaveChanges(); questionId = updateQuestion.Id; } else { var newQuestion = new Question { Sentence = question.GetValue("sentence").ToString(), Points = float.Parse(question.GetValue("points").ToString()), ExerciseId = exerciseId }; db.Questions.Add(newQuestion); db.SaveChanges(); questionId = newQuestion.Id; } foreach (JObject answer in question.GetValue("answers")) { if (answer.TryGetValue("id", out result)) { var updateAnswer = db.Answers.FirstOrDefault(a => a.Id == int.Parse(answer.GetValue("id").ToString())); updateAnswer.Sentence = answer.GetValue("sentence").ToString(); updateAnswer.IsCorrect = Convert.ToBoolean(answer.GetValue("isCorrect").ToString()); db.Answers.Update(updateAnswer); db.SaveChanges(); } else { var newAnswer = new Answer { Sentence = answer.GetValue("sentence").ToString(), IsCorrect = Convert.ToBoolean(answer.GetValue("isCorrect").ToString()), QuestionId = questionId }; db.Answers.Add(newAnswer); db.SaveChanges(); } } } } } return(Json(new { message = "Success!" })); }