Example #1
0
        // POST: api/GgcmsCategories
        public IHttpActionResult Add(GgcmsArticles info)
        {
            //提交除附加模型外的文件-标题图,内容中的图
            UpFileClass.FileSave(info, info.files.FindAll(x => x.fileType != 3));
            info.CreateTime = DateTime.Now;
            updateArticleNumber(info.Category_Id, 1);
            CacheHelper.RemoveAllCache(CacheTypeNames.Categorys);
            if (info.ModuleInfo != null && info.ModuleInfo.Id > 0)
            {
                info.ExtModelId = info.ModuleInfo.Id;
            }
            info.pagesCount = info.pages.Count + 1;
            var result = Dbctx.GgcmsArticles.Add(info);

            Dbctx.SaveChanges();
            //附件
            using (GgcmsDB db = new GgcmsDB())
            {
                foreach (GgcmsAttachments attach in info.attachments)
                {
                    attach.Articles_Id = info.Id;
                    attach.CreateTime  = DateTime.Now;
                    db.GgcmsAttachments.Add(attach);
                }
                db.SaveChanges();
            }
            //数据模型
            if (info.ModuleInfo != null && info.ModuleInfo.Id > 0)
            {
                foreach (var file in info.files.FindAll(x => x.fileType == 3))
                {
                    foreach (var item in info.ModuleInfo.Columns)
                    {
                        if (item.ColName == file.propertyName)
                        {
                            item.Value = UpFileClass.FileSave(file.filePath.ToString(), item.Value.ToString(), (int)file.fileType);
                        }
                    }
                }
                ExtendModule.SaveData(info.Id, info.ModuleInfo);
            }

            //分页保存
            if (info.pages.Count > 0)
            {
                foreach (var page in info.pages)
                {
                    UpFileClass.FileSave(page, page.files);
                    page.Article_Id = info.Id;
                    Dbctx.GgcmsArticlePages.Add(page);
                }
                Dbctx.SaveChanges();
            }
            ClearCache();
            return(Ok(result));
        }
Example #2
0
        // PUT: api/GgcmsCategories/5
        public IHttpActionResult Edit(GgcmsArticles info)
        {
            if (Dbctx.GgcmsArticles.Where(x => x.Id == info.Id).Count() == 0)
            {
                return(BadRequest("信息不存在"));
            }
            //Dbctx.GgcmsArticles.Attach(info);
            //Dbctx.Entry(info).Property("goods_name").IsModified = true;
            var ent = Dbctx.Entry(info);

            ent.State       = EntityState.Modified;
            info.pagesCount = info.pages.Where(x => x.state != EntityState.Deleted).Count() + 1;
            UpFileClass.FileSave(info, info.files.FindAll(x => x.fileType != 3));
            var old = Dbctx.GgcmsArticles.Find(info.Id);

            if (old.Category_Id != info.Category_Id)
            {
                updateArticleNumber(info.Category_Id, 1);
                updateArticleNumber(old.Category_Id, -1);
                CacheHelper.RemoveAllCache(CacheTypeNames.Categorys);
            }
            var list = Dbctx.GgcmsAttachments.Where(x => x.Articles_Id == info.Id).ToList();

            //附件
            foreach (GgcmsAttachments attach in list)
            {
                var item = info.attachments.Find(x => x.Id == attach.Id);
                if (item == null)
                {
                    Dbctx.GgcmsAttachments.Remove(attach);
                }
                else
                {
                    attach.AttaUrl    = item.AttaUrl;
                    attach.AttaTitle  = item.AttaTitle;
                    attach.Describe   = item.Describe;
                    attach.CreateTime = DateTime.Now;
                    attach.RealName   = item.RealName;
                }
            }
            foreach (GgcmsAttachments attach in info.attachments)
            {
                if (attach.Id == 0)
                {
                    attach.Articles_Id = info.Id;
                    attach.CreateTime  = DateTime.Now;
                    Dbctx.GgcmsAttachments.Add(attach);
                }
            }
            if (info.ModuleInfo != null)
            {
                info.ExtModelId = info.ModuleInfo.Id;
                if (old.ExtModelId > 0 && info.ExtModelId != old.ExtModelId)
                {
                    ExtendModule.Delete(info.Id, old.ExtModelId);
                }
                foreach (var file in info.files.FindAll(x => x.fileType == 3))
                {
                    foreach (var item in info.ModuleInfo.Columns)
                    {
                        if (item.ColName == file.propertyName)
                        {
                            item.Value = UpFileClass.FileSave(file.filePath.ToString(), item.Value.ToString(), (int)file.fileType);
                        }
                    }
                }
                ExtendModule.SaveData(info.Id, info.ModuleInfo);
            }
            else if (old.ExtModelId > 0)
            {
                ExtendModule.Delete(info.Id, old.ExtModelId);
            }
            //分页保存
            if (info.pages.Count > 0)
            {
                foreach (var page in info.pages)
                {
                    UpFileClass.FileSave(page, page.files);
                    if (page.state == EntityState.Added)
                    {
                        page.Article_Id = info.Id;
                        Dbctx.GgcmsArticlePages.Add(page);
                    }
                    else if (page.state == EntityState.Modified)
                    {
                        var pageEnt = Dbctx.Entry(page);
                        pageEnt.State = EntityState.Modified;
                    }
                    else if (page.state == EntityState.Deleted)
                    {
                        var p = Dbctx.GgcmsArticlePages.Find(page.Id);
                        if (p != null)
                        {
                            Dbctx.GgcmsArticlePages.Remove(p);
                        }
                    }
                    //只更新分页信息
                    else if (page.state == EntityState.Unchanged)
                    {
                        Dbctx.GgcmsArticlePages.Attach(page);
                        Dbctx.Entry(info).Property("OrderId").IsModified = true;
                    }
                }
            }
            Dbctx.SaveChanges();
            ClearCache();
            return(Ok(info));
        }