Esempio n. 1
0
 public async Task <MessageModel <int> > Post([FromBody] PostBlogViewModel blog)
 {
     return(new MessageModel <int>
     {
         response = await service.Save(blog)
     });
 }
Esempio n. 2
0
        public async Task <int> Save(PostBlogViewModel blogViewModel)
        {
            //var tags = ;

            try
            {
                baseRepository.Db.Ado.BeginTran();

                var blog = mapper.Map <PostBlogViewModel, BlogArticle>(blogViewModel);

                blog.PublishDate = DateTime.Now;

                var id = blog.Id;
                if (blog.Id == 0)
                {
                    id = await Add(blog);
                }
                else
                {
                    await Edit(blog);

                    await blogTagRepository.Db.Deleteable <BlogTag>().Where(t => t.BlogId == blog.Id).ExecuteCommandAsync();
                }


                var tags = await tagRepository.Query(t => blogViewModel.Tags.Contains(t.Name));

                var blogTags = new List <BlogTag>();
                foreach (var tag in tags)
                {
                    var temp = new BlogTag
                    {
                        BlogId = id,
                        TagId  = tag.Id,
                    };

                    blogTags.Add(temp);

                    //if(blogViewModel.Tags.Contains(tag.Name))
                    //{
                    //    blogViewModel.Tags.Remove(tag.Name);
                    //}

                    var res = blogViewModel.Tags.RemoveWhere(c => tag.Name.Trim() == c.Trim());
                }

                var newTags = new List <Tag>();
                foreach (var tag in blogViewModel.Tags)
                {
                    var newTag = new Tag()
                    {
                        Name = tag.Trim()
                    };
                    newTag.Id = await tagRepository.Add(newTag);

                    newTags.Add(newTag);
                }

                //if (newTags.Count > 0)
                //{
                //    await tagRepository.Add(newTags);

                //}

                foreach (var tag in newTags)
                {
                    var temp = new BlogTag
                    {
                        BlogId = id,
                        TagId  = tag.Id,
                    };

                    blogTags.Add(temp);
                }

                if (blogTags.Count > 0)
                {
                    await blogTagRepository.Add(blogTags);
                }

                //throw new Exception("dd");
                baseRepository.Db.Ado.CommitTran();

                return(id);
            }
            catch (Exception)
            {
                baseRepository.Db.Ado.RollbackTran();
                throw;
            }
        }