public async Task <BlogViewModel> UpdateBlogWithConstraintAsync(BlogViewModel blogVM) { // begin transaction using (var transaction = this.UnitOfWork.CreateTransac()) { try { // after binding tag from db // Update blog and binding blog tag var updateBlogResult = await UpdateAsync(blogVM.Id, blogVM); // Remove all list blog tag var listCurrentBlogTagVM = await blogTagService.GetActive(x => x.BlogId == blogVM.Id).ToListAsync(); foreach (var currentBTVM in listCurrentBlogTagVM) { await blogTagService.DeleteByObjAsync(currentBTVM); } // finding tag title and make sure create new tag if it does exist List <TagViewModel> listTagVM = new List <TagViewModel>(); foreach (var title in blogVM.Tags) { var blogTagVM = new BlogTagViewModel() { BlogId = updateBlogResult.Id, Active = true }; var existTag = await tagService.FirstOrDefaultActiveAsync(x => x.Title.Equals(title, StringComparison.OrdinalIgnoreCase)); if (existTag == null) { // if title does not exist, create new tag with this title and add to list var result = await tagService.CreateAsync(new TagViewModel() { Title = title, Active = true }); blogTagVM.TagId = result.Id; await blogTagService.CreateAsync(blogTagVM); } else { // if title does exist tag add to list tag VM blogTagVM.TagId = existTag.Id; await blogTagService.CreateAsync(blogTagVM); } } transaction.Commit(); return(updateBlogResult); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }
public JsonResult Delete(BlogTagViewModel data) { new BlogTags().Delete(data.Tag); return(Json(true)); }
public BlogTag(BlogTagViewModel blogTagVm) { BlogId = blogTagVm.BlogId; TagId = blogTagVm.TagId; }