Esempio n. 1
0
        public async Task <bool> AddAsync(CategoryVM entity)
        {
            var list_level = entity.LevelID2.DistinctBy(x => x.id);

            entity.Code = entity.Code.ToUpper();

            try
            {
                //if (entity.NameVI == null)
                //{
                //    entity.NameVI = entity.NameEn;
                //}
                var cat = new Category
                {
                    Name         = entity.NameEn,
                    Code         = entity.Code,
                    LevelID      = 0,
                    Cate_Kind_ID = entity.Cate_Kind_ID,
                    UserID       = entity.UserID,
                    CreateTime   = DateTime.Now
                };
                _dbContext.Categories.Add(cat);
                await _dbContext.SaveChangesAsync();

                var listCategoryLangs = new List <CategoryLang> {
                    new CategoryLang {
                        Name = entity.NameVI, LanguageID = "vi", CategoryID = cat.ID
                    },
                    new CategoryLang {
                        Name = entity.NameTW, LanguageID = "zh-TW", CategoryID = cat.ID
                    },
                    new CategoryLang {
                        Name = entity.NameEn, LanguageID = "en", CategoryID = cat.ID
                    },
                };
                _dbContext.CategoryLangs.AddRange(listCategoryLangs);
                await _dbContext.SaveChangesAsync();

                foreach (var item in list_level)
                {
                    var cat_level = new Models.EF.CategoryLevel
                    {
                        Cate_ID  = cat.ID,
                        Level_ID = item.id.ToInt()
                    };
                    _dbContext.CategoryLevels.Add(cat_level);
                    await _dbContext.SaveChangesAsync();
                }
                return(true);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                return(false);
            }
        }
Esempio n. 2
0
        public async Task <bool> UpdateAsync(CategoryVM entity)
        {
            var list_level = entity.LevelID2.DistinctBy(x => x.id);

            entity.Code = entity.Code.ToUpper();
            try
            {
                var iteam = await _dbContext.Categories.FirstOrDefaultAsync(x => x.ID == entity.ID);

                var item_level = _dbContext.CategoryLevels.Where(x => x.Cate_ID == entity.ID).ToList();
                if (item_level != null)
                {
                    _dbContext.CategoryLevels.RemoveRange(item_level);
                    await _dbContext.SaveChangesAsync();
                }
                iteam.Name         = entity.NameEn;
                iteam.Cate_Kind_ID = entity.Cate_Kind_ID;
                iteam.Code         = entity.Code;
                foreach (var item in list_level)
                {
                    var cat_level = new Models.EF.CategoryLevel
                    {
                        Cate_ID  = entity.ID,
                        Level_ID = item.id.ToInt()
                    };
                    _dbContext.CategoryLevels.Add(cat_level);
                    await _dbContext.SaveChangesAsync();
                }

                var item_occategories = _dbContext.OCCategories.Where(x => x.CategoryID == entity.ID).ToList();
                var item_cate_level   = _dbContext.CategoryLevels.Where(x => x.Cate_ID == entity.ID).ToList();

                foreach (var item2 in item_occategories)
                {
                    var level_oc = _dbContext.Levels.FirstOrDefault(x => x.ID == item2.OCID).LevelNumber;
                    foreach (var item3 in item_cate_level)
                    {
                        if (level_oc != item3.Level_ID)
                        {
                            item2.Status = false;
                            _dbContext.OCCategories.Update(item2);
                            await _dbContext.SaveChangesAsync();
                        }
                    }
                }
                iteam.LevelID = 0;
                await _dbContext.SaveChangesAsync();

                var listLang = _dbContext.CategoryLangs.Where(x => x.CategoryID == iteam.ID).ToList();
                if (listLang == null || listLang.Count == 0)
                {
                    var listCategoryLangs = new List <CategoryLang> {
                        new CategoryLang {
                            Name = entity.NameVI, LanguageID = "vi", CategoryID = iteam.ID
                        },
                        new CategoryLang {
                            Name = entity.NameTW, LanguageID = "zh-TW", CategoryID = iteam.ID
                        },
                        new CategoryLang {
                            Name = entity.NameEn, LanguageID = "en", CategoryID = iteam.ID
                        },
                    };
                    _dbContext.CategoryLangs.AddRange(listCategoryLangs);
                }
                else
                {
                    listLang.ForEach(x =>
                    {
                        switch (x.LanguageID)
                        {
                        case "zh-TW":
                            x.Name = entity.NameTW;
                            break;

                        case "en":
                            x.Name = entity.NameEn;
                            break;

                        case "vi":
                            x.Name = entity.NameVI;
                            break;

                        default:
                            break;
                        }
                    });
                }
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                //logging
                return(false);
            }
        }