Example #1
0
        public async Task <IActionResult> Detail(int id)
        {
            LessonDetailViewModel viewModel = await lessonService.GetLessonAsync(id);

            ViewData["Title"] = viewModel.Title;
            return(View(viewModel));
        }
Example #2
0
        public async Task <LessonDetailViewModel> CreateLessonAsync(LessonCreateInputModel inputModel)
        {
            LessonDetailViewModel viewModel = await lessonService.CreateLessonAsync(inputModel);

            memoryCache.Remove($"Course{viewModel.CourseId}");
            return(viewModel);
        }
Example #3
0
        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));
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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));
        }
Example #7
0
        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));
        }
Example #8
0
        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);
        }
Example #9
0
        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));
        }
Example #10
0
        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);
        }
Example #11
0
        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);
        }
Example #12
0
        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));
        }
Example #13
0
        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);
        }
Example #14
0
 public EditLessonPage(LessonDetailViewModel viewModel)
 {
     InitializeComponent();
     Lesson         = viewModel.Lesson;
     BindingContext = vm = new EditLessonViewModel(Lesson);
 }
Example #15
0
 public LessonDetailPage()
 {
     BindingContext = viewModel = new LessonDetailViewModel();
     InitializeComponent();
 }
Example #16
0
 public LessonDetailPage(Lesson lesson)
 {
     InitializeComponent();
     lessonValue    = lesson;
     BindingContext = viewModel = new LessonDetailViewModel(lesson);
 }
        public LessonDetailPage(LessonDetailViewModel viewModel)
        {
            InitializeComponent();

            BindingContext = this.viewModel = viewModel;
        }