コード例 #1
0
        public async Task <IActionResult> EditPost(int id, CreateHistoryVM category)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Xaiş olunur düzgün doldurun.");
                return(View(category));
            }
            AferogluHistory newCategory = await _context.AferogluHistory.FindAsync(id);

            if (newCategory == null)
            {
                return(View("Error"));
            }

            if (category.Photo != null)
            {
                string computerPhoto = Path.Combine(_env.WebRootPath, "images", newCategory.PhotoUrl);

                if (System.IO.File.Exists(computerPhoto))
                {
                    System.IO.File.Delete(computerPhoto);
                }

                string fileName = await category.Photo.SaveAsync(_env.WebRootPath);

                newCategory.PhotoUrl = fileName;
            }

            AferogluHistoryLang azBlogLangFromDb = await _context.AferogluHistoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "az" &&
                                                                                                           x.AferogluHistoryId == newCategory.Id);

            AferogluHistoryLang ruBlogLangFromDb = await _context.AferogluHistoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "ru" &&
                                                                                                           x.AferogluHistoryId == newCategory.Id);

            AferogluHistoryLang enBlogLangFromDb = await _context.AferogluHistoryLangs.FirstOrDefaultAsync(x => x.Lang.Code.ToLower() == "en" &&
                                                                                                           x.AferogluHistoryId == newCategory.Id);

            newCategory.Date             = category.Date;
            azBlogLangFromDb.Header      = category.HeaderAZ;
            enBlogLangFromDb.Header      = category.HeaderEN;
            ruBlogLangFromDb.Header      = category.HeaderRU;
            azBlogLangFromDb.Description = category.DescriptionAZ;
            azBlogLangFromDb.Description = category.DescriptionEN;
            azBlogLangFromDb.Description = category.DescriptionRU;

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public async Task <IActionResult> Details(int?id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (id == null)
            {
                return(View("Error"));
            }

            AferogluHistoryLang category = await _context.AferogluHistoryLangs.FirstOrDefaultAsync(p => p.AferogluHistory.Id == id && p.Lang.Code.ToLower() == "az");

            if (category == null)
            {
                return(View("Error"));
            }
            return(View(category));
        }
コード例 #3
0
        public async Task <IActionResult> Create(CreateHistoryVM category)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                return(View(category));
            }

            if (category.Photo == null)
            {
                return(View(category));
            }

            string fileName = await category.Photo.SaveAsync(_env.WebRootPath);

            Lang azLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "az");

            Lang ruLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "ru");

            Lang enLang = await _context.Langs.FirstOrDefaultAsync(x => x.Code.ToLower() == "en");

            AferogluHistory newCategory = new AferogluHistory()
            {
                Date     = category.Date,
                PhotoUrl = fileName
            };

            await _context.AferogluHistory.AddAsync(newCategory);

            await _context.SaveChangesAsync();

            AferogluHistoryLang historyAZ = new AferogluHistoryLang
            {
                Header            = category.HeaderAZ,
                Description       = category.DescriptionAZ,
                LangId            = azLang.Id,
                AferogluHistoryId = newCategory.Id
            };
            AferogluHistoryLang historyEN = new AferogluHistoryLang
            {
                Header            = category.HeaderEN,
                Description       = category.DescriptionEN,
                LangId            = enLang.Id,
                AferogluHistoryId = newCategory.Id
            };
            AferogluHistoryLang historyRU = new AferogluHistoryLang
            {
                Header            = category.HeaderRU,
                Description       = category.DescriptionRU,
                LangId            = ruLang.Id,
                AferogluHistoryId = newCategory.Id
            };

            _context.AferogluHistoryLangs.AddRange(historyAZ, historyEN, historyRU);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }