public async Task <int> Add(ViewAddCategory entity) { Category category = entity.Category; List <EavAttributeValue> lsAttributeValue = entity.EavAttributeValue; DaoEAVAttribute daoEAVAttribute = new DaoEAVAttribute(context); category.CategoryId = this.GenerateCategoryID(); category.Code = string.IsNullOrEmpty(category.Code) ? category.CategoryId : category.Code; category.ParentId = string.IsNullOrEmpty(category.ParentId) ? "0" : category.ParentId; bool checkCode = this.CheckUniqueCategoryCode(category.Code, category.CategoryId); bool checkID = this.CheckUniqueCategoryID(category.CategoryId); if (checkCode && checkID) { category.CreatedAt = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); category.UpdatedAt = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); context.Category.Add(category); foreach (EavAttributeValue item in lsAttributeValue) { var eavGroup = context.EavAttribute.Where(p => p.Guid.Equals(item.EavId)).Select(p => p.AttributeGroup).FirstOrDefault(); item.Guid = Guid.NewGuid().ToString().ToUpper(); item.CategoryId = category.CategoryId; item.AttributeGroup = eavGroup; context.EavAttributeValue.Add(item); } } return(await context.SaveChangesAsync()); }
public async Task <int> Update(ViewAddCategory entity) { Category category = entity.Category; List <EavAttributeValue> lsAttributeValue = entity.EavAttributeValue; DaoEAVAttribute daoEAVAttribute = new DaoEAVAttribute(context); category.Code = string.IsNullOrEmpty(category.Code) ? category.CategoryId : category.Code; bool checkCode = this.CheckUniqueCategoryCode(category.Code, category.CategoryId); Category categoryUpdate = context.Category.Where(p => p.CategoryId.Equals(category.CategoryId)).FirstOrDefault(); category.CopyPropertiesTo <Category>(categoryUpdate); if (checkCode) { categoryUpdate.UpdatedAt = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"); context.Category.Update(categoryUpdate); foreach (EavAttributeValue item in lsAttributeValue) { if (string.IsNullOrEmpty(item.Guid)) { item.Guid = Guid.NewGuid().ToString().ToUpper(); item.CategoryId = category.CategoryId; item.AttributeGroup = category.CategoryType; context.EavAttributeValue.Add(item); } else { var entityDB = context.EavAttributeValue.Where(p => p.Guid.Equals(item.Guid)).FirstOrDefault(); if (entityDB != null) { item.CopyPropertiesTo <EavAttributeValue>(entityDB); entityDB.AttributeGroup = category.CategoryType; context.EavAttributeValue.Update(entityDB); } } } } return(await context.SaveChangesAsync()); }