public async Task <IActionResult> Detail(int id) { LessonDetailViewModel viewModel = await lessonService.GetLessonAsync(id); ViewData["Title"] = viewModel.Title; return(View(viewModel)); }
public async Task <LessonDetailViewModel> CreateLessonAsync(LessonCreateInputModel inputModel) { LessonDetailViewModel viewModel = await lessonService.CreateLessonAsync(inputModel); memoryCache.Remove($"Course{viewModel.CourseId}"); return(viewModel); }
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 <LessonDetailViewModel> EditLessonAsync(LessonEditInputModel inputModel) { LessonDetailViewModel viewModel = await lessonService.EditLessonAsync(inputModel); memoryCache.Remove($"Course{viewModel.CourseId}"); memoryCache.Remove($"Lesson{viewModel.Id}"); return(viewModel); }
public async Task <LessonDetailViewModel> CreateLessonAsync(LessonCreateInputModel inputModel) { int lessonId = await db.QueryScalarAsync <int>($@"INSERT INTO Lessons (Title, CourseId, Duration) VALUES ({inputModel.Title}, {inputModel.CourseId}, '00:00:00'); SELECT last_insert_rowid();"); LessonDetailViewModel lesson = await GetLessonAsync(lessonId); return(lesson); }
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 <IActionResult> Create(LessonCreateInputModel inputModel) { if (ModelState.IsValid) { LessonDetailViewModel lesson = await lessonService.CreateLessonAsync(inputModel); TempData["ConfirmationMessage"] = "Ok! La lezione è stata creata, aggiungi anche gli altri dati"; return(RedirectToAction(nameof(Edit), new { id = lesson.Id })); } ViewData["Title"] = "Nuova lezione"; return(View(inputModel)); }
public async Task <ActionResult> CreateDetail(LessonDetailViewModel viewModel) { if (ModelState.IsValid) { var lessonDetail = Mapper.Map <LessonDetail>(viewModel); db.LessonDetail.Add(lessonDetail); await db.SaveChangesAsync(); return(Json("Success")); } return(null); }
public ActionResult Detail(int id, string subview = "") { var lesson = LessonRepository.Instance._getByID(id); var teacher = lesson.Data.LessonAccess.FirstOrDefault(a => a.User.UserTypeID == 2)?.User; ViewBag.SubView = subview; var data = new LessonDetailViewModel() { Lesson = lesson.Data, Teacher = teacher }; return(View(data)); }
public async Task <LessonDetailViewModel> GetLessonAsync(int id) { IQueryable <LessonDetailViewModel> queryLinq = dbContext.Lessons .AsNoTracking() .Where(lesson => lesson.Id == id) .Select(lesson => LessonDetailViewModel.FromEntity(lesson)); //Usando metodi statici come FromEntity, la query potrebbe essere inefficiente. Mantenere il mapping nella lambda oppure usare un extension method personalizzato LessonDetailViewModel viewModel = await queryLinq.FirstOrDefaultAsync(); if (viewModel == null) { logger.LogWarning("Lesson {id} not found", id); throw new LessonNotFoundException(id); } return(viewModel); }
public async Task <LessonDetailViewModel> GetLessonAsync(int id) { FormattableString query = $@"SELECT Id, CourseId, Title, Description, Duration FROM Lessons WHERE ID={id}"; DataSet dataSet = await db.QueryAsync(query); //Course var lessonTable = dataSet.Tables[0]; if (lessonTable.Rows.Count != 1) { logger.LogWarning("Lesson {id} not found", id); throw new LessonNotFoundException(id); } var lessonRow = lessonTable.Rows[0]; var lessonDetailViewModel = LessonDetailViewModel.FromDataRow(lessonRow); return(lessonDetailViewModel); }
public async Task <IActionResult> Edit(LessonEditInputModel inputModel) { if (ModelState.IsValid) { try { LessonDetailViewModel viewModel = await lessonService.EditLessonAsync(inputModel); TempData["ConfirmationMessage"] = "I dati sono stati salvati con successo"; return(RedirectToAction(nameof(Detail), new { id = viewModel.Id })); } catch (OptimisticConcurrencyException) { ModelState.AddModelError("", "Spiacenti, il salvataggio non è andato a buon fine perché nel frattempo un altro utente ha aggiornato la lezione. Ti preghiamo di aggiornare la pagina e ripetere le modifiche."); } } ViewData["Title"] = "Modifica lezione"; return(View(inputModel)); }
public async Task <LessonDetailViewModel> EditLessonAsync(LessonEditInputModel inputModel) { int affectedRows = await db.CommandAsync($"UPDATE Lessons SET Title={inputModel.Title}, Description={inputModel.Description}, Duration={inputModel.Duration:HH':'mm':'ss}, [Order]={inputModel.Order} WHERE Id={inputModel.Id} AND RowVersion={inputModel.RowVersion}"); if (affectedRows == 0) { bool lessonExists = await db.QueryScalarAsync <bool>($"SELECT COUNT(*) FROM Lessons WHERE Id={inputModel.Id}"); if (lessonExists) { throw new OptimisticConcurrencyException(); } else { throw new LessonNotFoundException(inputModel.Id); } } LessonDetailViewModel lesson = await GetLessonAsync(inputModel.Id); return(lesson); }
public EditLessonPage(LessonDetailViewModel viewModel) { InitializeComponent(); Lesson = viewModel.Lesson; BindingContext = vm = new EditLessonViewModel(Lesson); }
public LessonDetailPage() { BindingContext = viewModel = new LessonDetailViewModel(); InitializeComponent(); }
public LessonDetailPage(Lesson lesson) { InitializeComponent(); lessonValue = lesson; BindingContext = viewModel = new LessonDetailViewModel(lesson); }
public LessonDetailPage(LessonDetailViewModel viewModel) { InitializeComponent(); BindingContext = this.viewModel = viewModel; }