public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var paragraph = await _context.Paragraph.Include(x => x.TheoryModule).SingleOrDefaultAsync(m => m.Id == id);

            if (paragraph == null)
            {
                return(NotFound());
            }

            ParagraphViewModel model = new ParagraphViewModel
            {
                Name      = paragraph.Name,
                ModuleId  = paragraph.TheoryModule.Id,
                SerialNum = paragraph.SerialNum
            };

            using (StreamReader sr = new StreamReader($"wwwroot/Paragraphs/{paragraph.TheoryModule.Id}/{paragraph.Id}/text.txt", Encoding.GetEncoding(1251)))
            {
                ViewData["Data"] = sr.ReadToEnd();
            }

            if (!String.IsNullOrWhiteSpace(paragraph.PictureFileName))
            {
                ViewData["PicturePath"] = $"Paragraphs/{paragraph.TheoryModule.Id}/{paragraph.Id}/{paragraph.PictureFileName}";
            }
            else
            {
                ViewData["PicturePath"] = "none";
            }

            if (!String.IsNullOrWhiteSpace(paragraph.SoundFileName))
            {
                ViewData["AudioPath"] = $"Paragraphs/{paragraph.TheoryModule.Id}/{paragraph.Id}/{paragraph.SoundFileName}";
            }
            else
            {
                ViewData["AudioPath"] = "none";
            }

            if (!String.IsNullOrWhiteSpace(paragraph.VideoFileName))
            {
                ViewData["VideoPath"] = $"Paragraphs/{paragraph.TheoryModule.Id}/{paragraph.Id}/{paragraph.VideoFileName}";
            }
            else
            {
                ViewData["VideoPath"] = "none";
            }

            ViewData["Pid"] = paragraph.Id;

            return(View(model));
        }
        public static ParagraphViewModel Create(ItemText itemText, string language)
        {
            var viewModel = new ParagraphViewModel();

            if (itemText?[language] is string s)
            {
                viewModel.Text = s;
            }

            return(viewModel);
        }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <ParagraphViewModel> GetByIDAsync(int id)
        {
            var entity = await GetByIdAsync(id);

            var entityViewModel = new ParagraphViewModel();

            if (entity != null)
            {
                entity.ThumbsupCount++;
                Update(entity);
                entityViewModel = _mapper.Map <ParagraphViewModel>(entity);
            }
            return(entityViewModel);
        }
        public async Task <IActionResult> Create(int?moduleId, int?serialNum)
        {
            if (moduleId == null)
            {
                return(RedirectToAction("Create", "TheoryModules"));
            }

            var module = await _context.TheoryModule.SingleOrDefaultAsync(m => m.Id == moduleId);

            if (module == null)
            {
                return(RedirectToAction("Create", "TheoryModules"));
            }

            if (serialNum == null)
            {
                var paragraphs = _context.Paragraph.Where(x => x.TheoryModule.Id == moduleId).OrderBy(x => x.SerialNum);
                if (!paragraphs.Any(x => x.SerialNum == 1))
                {
                    serialNum = 1;
                }
                else if (!paragraphs.Any(x => x.SerialNum == 2))
                {
                    serialNum = 2;
                }
                else if (!paragraphs.Any(x => x.SerialNum == 3))
                {
                    serialNum = 3;
                }
                else
                {
                    return(RedirectToAction("Details", "TheoryModules", new { id = moduleId }));
                }
            }

            ParagraphViewModel model = new ParagraphViewModel
            {
                ModuleId  = (int)moduleId,
                SerialNum = (int)serialNum
            };

            return(View(model));
        }
        /// ------------------------------------------------------------------------------------------------

        /// ------------------------------------------------------------------------------------------------

        #region Public Constructors
        /// ------------------------------------------------------------------------------------------------
        ///
        public ParagraphView(Action refreshlist)
        {
            try
            {
                InitializeComponent();
                Refresh         = refreshlist;
                ParaViewModel   = new ParagraphViewModel();
                CurrentInstance = this;
                TapGestures();
                AddNewCell();
                Lstvw_TypedParagraphs.ItemsSource  = ParaViewModel.SelectedParagraphs;
                ParagraphViewCell.CellTextUpdated += AddNewCell;
                Delete = OnDelete;
            }
            catch (Exception ex)
            {
                LogTracking.LogTrace(ex.ToString());
            }
        }
        public static ParagraphViewModel Create(ItemTextNoted itemTextNoted, string language)
        {
            var viewModel = new ParagraphViewModel
            {
                Style = itemTextNoted.Style
            };

            if (itemTextNoted[language] is string s)
            {
                viewModel.Text = s;
            }

            if (itemTextNoted.Note != null)
            {
                viewModel.Note = Create(itemTextNoted.Note, language);
            }

            return(viewModel);
        }
        public async Task <IActionResult> Edit(int id, ParagraphViewModel model)
        {
            if (!ParagraphExists(id))
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var p = _context.Paragraph.Include(x => x.TheoryModule).SingleOrDefault(x => x.Id == id);

                    p.Name = model.Name;

                    using (StreamWriter sw = new StreamWriter($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/text.txt", false, Encoding.GetEncoding(1251)))
                    {
                        sw.Write(model.Text);
                    }

                    // Проверяем, изменена ли картинка
                    if (model.Picture != null)
                    {
                        // Если да, то удаляем старую
                        if (p.PictureFileName != null && p.PictureFileName != model.Picture.FileName)
                        {
                            System.IO.File.Delete($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{p.PictureFileName}");
                        }

                        // И сохраняем новую
                        using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{model.Picture.FileName}", FileMode.Create))
                        {
                            await model.Picture.CopyToAsync(fs);
                        }
                        p.PictureFileName = model.Picture.FileName;
                    }

                    // Проверяем, изменена ли аудио-дорожка
                    if (model.Audio != null)
                    {
                        // Если да, то удаляем старую
                        if (p.SoundFileName != null && p.SoundFileName != model.Audio.FileName)
                        {
                            System.IO.File.Delete($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{p.SoundFileName}");
                        }

                        // И сохраняем новую
                        using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{model.Audio.FileName}", FileMode.Create))
                        {
                            await model.Audio.CopyToAsync(fs);
                        }
                        p.SoundFileName = model.Audio.FileName;
                    }

                    // Проверяем, изменено ли видео
                    if (model.Video != null)
                    {
                        // Если да, то удаляем старое
                        if (p.VideoFileName != null && p.VideoFileName != model.Video.FileName)
                        {
                            System.IO.File.Delete($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{p.VideoFileName}");
                        }

                        // И сохраняем новое
                        using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{p.TheoryModule.Id}/{p.Id}/{model.Video.FileName}", FileMode.Create))
                        {
                            await model.Video.CopyToAsync(fs);
                        }
                        p.VideoFileName = model.Video.FileName;
                    }

                    _context.Update(p);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <IActionResult> Create(ParagraphViewModel model)
        {
            if (ModelState.IsValid)
            {
                var module = _context.TheoryModule.SingleOrDefault(x => x.Id == model.ModuleId);
                if (module == null)
                {
                    return(RedirectToAction("Create", "TheoryModules"));
                }

                if (_context.Paragraph.Any(x => x.Name == model.Name && x.TheoryModule.Id == model.ModuleId))
                {
                    ViewData["Error"] = "Имя уже занято.";
                    return(View(model));
                }

                Paragraph p = new Paragraph
                {
                    Name         = model.Name,
                    TheoryModule = _context.TheoryModule.SingleOrDefault(x => x.Id == model.ModuleId),
                    SerialNum    = model.SerialNum
                };

                if (model.Picture != null)
                {
                    p.PictureFileName = model.Picture.FileName;
                }

                if (model.Audio != null)
                {
                    p.SoundFileName = model.Audio.FileName;
                }

                if (model.Video != null)
                {
                    p.VideoFileName = model.Video.FileName;
                }


                _context.Add(p);
                await _context.SaveChangesAsync();

                if (!Directory.Exists($"wwwroot/Paragraphs/{module.Id}"))
                {
                    Directory.CreateDirectory($"wwwroot/Paragraphs/{module.Id}");
                }
                if (!Directory.Exists($"wwwroot/Paragraphs/{module.Id}/{p.Id}"))
                {
                    Directory.CreateDirectory($"wwwroot/Paragraphs/{module.Id}/{p.Id}");
                }

                using (StreamWriter sw = new StreamWriter($"wwwroot/Paragraphs/{module.Id}/{p.Id}/text.txt", false, Encoding.GetEncoding(1251)))
                {
                    sw.Write(model.Text);
                }

                if (model.Picture != null)
                {
                    using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{module.Id}/{p.Id}/{model.Picture.FileName}", FileMode.Create))
                    {
                        await model.Picture.CopyToAsync(fs);
                    }
                }

                if (model.Audio != null)
                {
                    using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{module.Id}/{p.Id}/{model.Audio.FileName}", FileMode.Create))
                    {
                        await model.Audio.CopyToAsync(fs);
                    }
                }

                if (model.Video != null)
                {
                    using (FileStream fs = new FileStream($"wwwroot/Paragraphs/{module.Id}/{p.Id}/{model.Video.FileName}", FileMode.Create))
                    {
                        await model.Video.CopyToAsync(fs);
                    }
                }

                if (model.SerialNum < 3)
                {
                    return(RedirectToAction("Create", new { moduleId = model.ModuleId, serialNum = model.SerialNum + 1 }));
                }
                else
                {
                    return(RedirectToAction("Details", "TheoryModules", new { id = model.ModuleId }));
                }
            }
            return(View(model));
        }