Esempio n. 1
0
 private Category_Tags SetClient_Tag(Category_Tags idata, bool is_new = false)
 {
     if (is_new)
     {
         idata.created_by = Client.CurrentUser.id;
         idata.created    = DateTime.Now;
     }
     else
     {
         idata.updated    = DateTime.Now;
         idata.updated_by = Client.CurrentUser.id;
     }
     return(idata);
 }
Esempio n. 2
0
        public async Task <Category> CreateAsync(Category idata)
        {
            idata = SetClient(idata, true);
            if (idata.Tags != null)
            {
                idata.Category_Tags = new List <Category_Tags>();
                foreach (var tag in idata.Tags)
                {
                    var new_tag = new Category_Tags()
                    {
                        tag_id = tag.ref_id
                    };
                    new_tag = SetClient_Tag(new_tag, true);
                    idata.Category_Tags.Add(new_tag);
                }
            }
            await this.dbSet.AddAsync(idata);

            await storageContext.SaveChangesAsync();

            return(idata);
        }
Esempio n. 3
0
        public async Task <Category> UpdateAsync(Category idata)
        {
            if (idata.id > 0)
            {
                var data = await this.dbSet.AsNoTracking().FirstOrDefaultAsync(x => x.id == idata.id);

                if (data == null)
                {
                    throw new Exception($"Categoty id:{idata.id} is not found");
                }
                idata            = SetClient(idata);
                data.name        = idata.name;
                data.description = idata.description;

                var tags_context = this.storageContext.Set <Category_Tags>();
                var tags         = await tags_context.Where(x => x.category_id == idata.id).AsNoTracking().ToListAsync();

                if (idata.Tags != null)
                {
                    if (data.Category_Tags == null)
                    {
                        data.Category_Tags = new List <Category_Tags>();
                    }
                    foreach (var tag in idata.Tags)
                    {
                        var tag_indb = tags.FirstOrDefault(x => x.id == tag.id);
                        if (tag_indb == null)
                        {
                            var new_tag = new Category_Tags()
                            {
                                category_id = idata.id,
                                tag_id      = tag.ref_id,
                                Category    = null,
                                Tags        = null
                            };
                            new_tag = SetClient_Tag(new_tag, true);
                            await tags_context.AddAsync(new_tag);
                        }
                        else
                        {
                            tag_indb = SetClient_Tag(tag_indb);
                            tags_context.Update(tag_indb);
                        }
                    }
                    var ids         = idata.Tags.Select(x => x.id).ToArray();
                    var remove_tags = tags.Where(x => !ids.Contains(x.id)).ToList();
                    if (remove_tags.Count() > 0)
                    {
                        tags_context.RemoveRange(remove_tags);
                    }
                }
                else if (tags.Count() > 0)
                {
                    tags_context.RemoveRange(tags);
                }
                idata              = null;
                tags               = null;
                data.Tags          = null;
                data.Category_Tags = null;
                this.dbSet.Update(data);
                await storageContext.SaveChangesAsync();

                return(data);
            }
            return(null);
        }