public async Task <CourseDetailViewModel> CreateCourseAsync(CourseCreateInputModel inputModel) { string title = inputModel.Title; string author; string authorId; try { author = httpContextAccessor.HttpContext.User.FindFirst("FullName").Value; authorId = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; } catch (NullReferenceException) { throw new UserUnknownException(); } Course course = new(title, author, authorId); dbContext.Add(course); try { await dbContext.SaveChangesAsync(); } catch (DbUpdateException exc) when((exc.InnerException as SqliteException)?.SqliteErrorCode == 19) { throw new CourseTitleUnavailableException(title, exc); } return(CourseDetailViewModel.FromEntity(course)); }
public async Task <LessonDetailViewModel> CreateLessonAsync(LessonCreateInputModel inputModel) { var lesson = new Lesson(inputModel.Title, inputModel.CourseId); dbContext.Add(lesson); await dbContext.SaveChangesAsync(); return(LessonDetailViewModel.FromEntity(lesson)); }
public async Task <CourseDetailViewModel> CreateCourseAsync(CourseCreateInputModel inputModel) { string title = inputModel.Title; string author = "Mario Rossi"; var course = new Course(title, author); dbContext.Add(course); await dbContext.SaveChangesAsync(); CourseDetailViewModel viewModel = CourseDetailViewModel.FromEntity(course); return(viewModel); }
public async Task <CourseDetailViewModel> CreateCourseAsync(CourseCreateInputModel inputModel) { string title = inputModel.Title; string author = "Mario Rossi"; var course = new Course(title, author); dbContext.Add(course); try { await dbContext.SaveChangesAsync(); } catch (DbUpdateException exc) when((exc.InnerException as SqliteException)?.SqliteErrorCode == 19) { throw new CourseTitleUnavailableException(title, exc); } return(CourseDetailViewModel.FromEntity(course)); }
public async Task <CourseDetailViewModel> CreateCourseAsync(CourseCreateInputModel inputModel) { //TODO var course = new Course(inputModel.Title, "Mario Rossi"); dbContext.Add(course); await dbContext.SaveChangesAsync(); return(CourseDetailViewModel.FromEntity(course)); }
//-----------------------------------------Inserimento corsi---------------------------- public async Task <CourseDetailViewModel> CreateCourseAsync(CourseCreateInputModel inputModel) { string title = inputModel.Title; string author = "Mario Verdi"; var course = new Course(title, author); //nuova istanza di course dbContext.Add(course); //query di insert try { await dbContext.SaveChangesAsync(); //persiste la modifica in modo definito } //delle eccezioni del db, catturo solamnte la sqlitexception con codice 19 (i corsi sono unique) catch (DbUpdateException ex) when((ex.InnerException as SqliteException)?.SqliteErrorCode == 19) { //eccezione personalizzata: creazione del corso fallita perché il titolo non era disponibile throw new CourseTitleUnavailableException(title, ex); } //restituisco un istanza di CourseDetailVieModel tramite FromEntity return(CourseDetailViewModel.FromEntity(course)); }