Ejemplo n.º 1
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="theData">保存的数据</param>
        public ActionResult SaveData(ArticleDto theData)
        {
            if (!string.IsNullOrEmpty(theData.Cover))
            {
                theData.Cover = theData.Cover.Substring(theData.Cover.IndexOf("/Upload"));
            }
            Article article = _mapper.Map <ArticleDto, Article>(theData);//进行Dto对象与模型之间的映射

            if (theData.Id == 0)
            {
                article.CreateTime = DateTime.Now;
                _articleBusiness.Insert(article);
            }
            else
            {
                _articleBusiness.Update(article);
                _attachmentBusiness.Delete(m => m.TargetId == article.Id);
            }
            List <string> attachments = new List <string>();

            if (!string.IsNullOrEmpty(theData.Attachments))
            {
                attachments = theData.Attachments.Split(',').CastToList <string>();
            }
            List <Attachment> attachmentList = new List <Attachment>();

            attachments.ForEach(m =>
            {
                var data          = m.Split('&');
                Attachment attach = new Attachment();
                attach.Directory  = data[0];
                attach.SavePath   = data[1].Substring(data[1].IndexOf("/Upload"));
                attach.Thumb      = data[2].Substring(data[1].IndexOf("/Upload"));
                attach.FileExt    = Path.GetExtension(data[1]);
                attach.Name       = data[3];
                attach.Type       = 1;
                attach.TargetId   = article.Id;
                attach.CreateTime = DateTime.Now;
                attach.Store      = 0;
                attachmentList.Add(attach);
            });
            if (attachmentList.Count > 0)
            {
                _attachmentBusiness.BulkInsert(attachmentList);
            }

            return(Success());
        }