Example #1
0
        public async Task <ActionResult> Create(SubCategory subCategory, string nameVI, string nameEN, string NameTW)
        {
            try
            {
                subCategory.Name = nameVI + " - " + nameEN + " - " + NameTW;;
                db.SubCategories.Add(subCategory);
                await db.SaveChangesAsync();

                SubCategoryLang vn = new SubCategoryLang();
                vn.Name          = nameVI;
                vn.LanguageID    = "vi";
                vn.SubCategoryID = subCategory.SubCategoryID;
                db.SubCategoryLangs.Add(vn);

                SubCategoryLang en = new SubCategoryLang();
                en.Name          = nameEN;
                en.LanguageID    = "en";
                en.SubCategoryID = subCategory.SubCategoryID;
                db.SubCategoryLangs.Add(en);

                SubCategoryLang tw = new SubCategoryLang();
                tw.Name          = NameTW;
                tw.LanguageID    = "en";
                tw.SubCategoryID = subCategory.SubCategoryID;
                db.SubCategoryLangs.Add(tw);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            catch (Exception)
            {
                return(View(subCategory));
            }
        }
        public bool UpdateSubCategoryTranslation(SubCategoryLang subCategoryTranslation)
        {
            bool result;

            try
            {
                this.subCategoryLangs.Update(subCategoryTranslation);
                this.subCategoryLangs.SaveChanges();
                result = true;
            }
            catch
            {
                result = false;
            }

            return(result);
        }
        public IActionResult AddEditCategoryTranslations(SubCategoryIndexViewModel modelIn)
        {
            if (this.ModelState.IsValid)
            {
                if (modelIn.Id == 0)
                {
                    try
                    {
                        var subCategoryTranslation = new SubCategoryLang
                        {
                            Name          = modelIn.Name,
                            LangText      = modelIn.LangText,
                            SubCategoryId = modelIn.SubCategoryId,
                            Lang          = modelIn.Lang,
                        };
                        this.categoryService.SaveSubCategotyLangAsync(subCategoryTranslation);
                        return(this.RedirectToAction("Index", "Category", new { toastr = "New local category name has been created successfully." }));
                    }
                    catch
                    {
                        return(this.RedirectToAction("Index", "Category", new { toastr = "ERROR: No category translation has been created. Try again." }));
                    }
                }
                else
                {
                    try
                    {
                        var subCategoryTranslation = this.categoryService.GetSubCategoryTranslation((int)modelIn.Id);
                        subCategoryTranslation.LangText = modelIn.LangText;
                        this.categoryService.UpdateSubCategoryTranslation(subCategoryTranslation);
                        return(this.RedirectToAction("Index", "Category", new { toastr = "The categroy translation has been updated successfully" }));
                    }
                    catch
                    {
                        return(this.RedirectToAction("Index", "Category", new { toastr = "ERROR: Category translation hasn't been updated. Try again." }));
                    }
                }
            }

            this.ViewBag.Message = modelIn.Id != 0 ? "Edit Translation" : "New Translation";
            return(this.View("AddEditCategoryTranslation", modelIn));
        }
Example #4
0
        public async Task <ActionResult> Edit(SubCategory subCategory, string nameVI, string nameEN, string nameTW)
        {
            try
            {
                var id      = subCategory.SubCategoryID;
                var itemOri = await db.SubCategories.FindAsync(id);

                var itemVi = await db.SubCategoryLangs.FirstOrDefaultAsync(x => x.SubCategoryID == id && x.LanguageID == "vi");

                var itemEn = await db.SubCategoryLangs.FirstOrDefaultAsync(x => x.SubCategoryID == id && x.LanguageID == "en");

                var itemTw = await db.SubCategoryLangs.FirstOrDefaultAsync(x => x.SubCategoryID == id && x.LanguageID == "tw");

                await db.SaveChangesAsync();

                if (itemVi == null)
                {
                    SubCategoryLang vn = new SubCategoryLang();
                    vn.Name          = nameVI;
                    vn.LanguageID    = "vi";
                    vn.SubCategoryID = subCategory.SubCategoryID;
                    db.SubCategoryLangs.Add(vn);
                }
                else
                {
                    itemVi.Name = nameVI;
                }
                if (itemTw == null)
                {
                    SubCategoryLang tw = new SubCategoryLang();
                    tw.Name          = nameTW;
                    tw.LanguageID    = "tw";
                    tw.SubCategoryID = subCategory.SubCategoryID;
                    db.SubCategoryLangs.Add(tw);
                }
                else
                {
                    itemTw.Name = nameTW;
                }
                if (itemEn == null)
                {
                    SubCategoryLang en = new SubCategoryLang();
                    en.Name          = nameEN;
                    en.LanguageID    = "en";
                    en.SubCategoryID = subCategory.SubCategoryID;
                    db.SubCategoryLangs.Add(en);
                }
                else
                {
                    itemEn.Name = nameEN;
                }
                itemOri.CategoryID = subCategory.CategoryID;
                itemOri.Name       = nameVI + " - " + nameEN + " - " + nameTW;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            catch (Exception)
            {
                return(View(subCategory));
            }
        }
        public async Task SaveSubCategotyLangAsync(SubCategoryLang subCategoryTranslation)
        {
            await this.subCategoryLangs.AddAsync(subCategoryTranslation);

            this.subCategoryLangs.SaveChanges();
        }