public async Task <LessonDetailViewModel> EditLessonAsync(LessonEditInputModel inputModel) { Lesson lesson = await dbContext.Lessons.FindAsync(inputModel.Id); if (lesson == null) { throw new LessonNotFoundException(inputModel.Id); } lesson.ChangeTitle(inputModel.Title); lesson.ChangeDescription(inputModel.Description); lesson.ChangeDuration(inputModel.Duration); lesson.ChangeOrder(inputModel.Order); dbContext.Entry(lesson).Property(lesson => lesson.RowVersion).OriginalValue = inputModel.RowVersion; try { await dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw new OptimisticConcurrencyException(); } return(LessonDetailViewModel.FromEntity(lesson)); }
public async Task <CourseDetailViewModel> EditCourseAsync(CourseEditInputModel inputModel) { //Recupero dell'entità Course //FindAsync: specializzato con le chiavi primarie, gli fornisco l'id Course course = await dbContext.Courses.FindAsync(inputModel.Id); if (course == null) { throw new CourseNotFoundException(inputModel.Id); } //...e lui ci recupererà il corso (collegato a quell'id) andando a richiamare i metodi presenti in Courses course.ChangeTitle(inputModel.Title); course.ChangePrices(inputModel.FullPrice, inputModel.CurrentPrice); course.ChangeDescription(inputModel.Description); course.ChangeEmail(inputModel.Email); //concorrenza ottimistica (prima di invocare il SaveAsync) //chiediamo il registro dell'entità (Entry(course) e la sua proprietà RowVersion a cui settiamo l'original value) //il valore fornito dall'input model é letto dal db dbContext.Entry(course).Property(course => course.RowVersion).OriginalValue = inputModel.RowVersion; if (inputModel.Image != null) { try{ string imagePath = await imagePersister.SaveCourseImageAsync(inputModel.Id, inputModel.Image); //aggiorno solo l'image path con il nuovo percorso che é stato restituito course.ChangeImagePath(imagePath); } catch (Exception ex) //immagine troppo grande! { throw new CourseImageInvalidException(inputModel.Id, ex); } } //dbContext.Update(course); non necessario perché già l'entità course viene tracciata try { //SaveChangesAsync invia un comando update al database await dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw new OptimisticConcurrencyException(); } catch (DbUpdateException ex) when((ex.InnerException as SqliteException)?.SqliteErrorCode == 19) { throw new CourseTitleUnavailableException(inputModel.Title, ex); } return(CourseDetailViewModel.FromEntity(course)); }
public async Task <CourseDetailViewModel> EditCourseAsync(CourseEditInputModel inputModel) { Course course = await dbContext.Courses.FindAsync(inputModel.Id); if (course == null) { throw new CourseNotFoundException(inputModel.Id); } course.ChangeTitle(inputModel.Title); course.ChangePrices(inputModel.FullPrice, inputModel.CurrentPrice); course.ChangeDescription(inputModel.Description); course.ChangeEmail(inputModel.Email); dbContext.Entry(course).Property(course => course.RowVersion).OriginalValue = inputModel.RowVersion; if (inputModel.Image != null) { try { string imagePath = await imagePersister.SaveCourseImageAsync(inputModel.Id, inputModel.Image); course.ChangeImagePath(imagePath); } catch (Exception exc) { throw new CourseImageInvalidException(inputModel.Id, exc); } } //dbContext.Update(course); try { await dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw new OptimisticConcurrencyException(); } catch (DbUpdateException exc) when((exc.InnerException as SqliteException)?.SqliteErrorCode == 19) { throw new CourseTitleUnavailableException(inputModel.Title, exc); } return(CourseDetailViewModel.FromEntity(course)); }