public ActionResult Create(PostViewModel post)
        {
            //try
            //{

            var id = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (ModelState.IsValid)
            {
                var postDomainModel = mapper.Map <PostDomainModel>(post);
                var newPostId       = postService.CreatePost(postDomainModel, id);

                if (post.Tag.Content != null)
                {
                    tagService.AddTags(post.Tag.Content, newPostId);
                }

                return(RedirectToAction(nameof(Index)));
            }

            else
            {
                return(View());
            }
        }
Exemple #2
0
        public async Task <IActionResult> PutNews([FromRoute] int id, [FromBody] EditNewsDto editNewsDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var editNews = await context.News.Include(news => news.NewsTags)
                           .ThenInclude(tag => tag.Tag)
                           .Include(news => news.NewsCategories)
                           .ThenInclude(category => category.Category)
                           .SingleOrDefaultAsync(news => news.Id == id);

            if (editNews == null)
            {
                return(NotFound());
            }

            var addedTagsDto = tagService.AddTags(editNewsDto.Tags).Result;

            editNewsDto.Tags = addedTagsDto;

            mapper.Map(editNewsDto, editNews);
            var newsTags = editNews.NewsTags.ToList();

            foreach (var tag in addedTagsDto)
            {
                newsTags.Add(new NewsTag()
                {
                    TagId = tag.Id
                });
            }

            editNews.NewsTags             = newsTags;
            editNews.ModifiedAt           = DateTime.Now;
            context.Entry(editNews).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NewsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(new StatusCodeResult((int)HttpStatusCode.InternalServerError));
                }
            }

            await elasticClient.UpdateAsync <News>(editNews, u => u.Doc(editNews));

            return(NoContent());
        }
        public async Task <IActionResult> Post([FromBody] TagDTO model)
        {
            TagModel tag      = new DTOMapper <TagDTO, TagModel>().Serialize(model);
            var      response = await _tagService.AddTags(tag);

            return(Ok(response));
        }
Exemple #4
0
        public HttpResponseMessage Create(HttpRequestMessage request, ProductModel ProductVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    // Create tag mới
                    var TagsCreate = ProductVm.Tags.Where(x => x.isNew == true).ToList();
                    var ListTags = Mapper.Map <List <TagModel>, List <Tag> >(TagsCreate);
                    var ListTagCreate = _Tag.AddTags(ListTags);
                    /// Tạo product
                    var modelVm = Mapper.Map <ProductModel, Product>(ProductVm);
                    _Product.Create(modelVm);
                    _Product.Save();
                    /// Tag tag
                    List <ProductTag> ListProductTag = new List <ProductTag>();
                    foreach (var Tag in ProductVm.Tags.Where(x => x.isNew == false).ToList())
                    {
                        ProductTag ProductTag = new ProductTag();
                        ProductTag.ProductID = modelVm.ProductID;
                        ProductTag.TagID = Convert.ToInt32(Tag.id);
                        ListProductTag.Add(ProductTag);
                    }
                    foreach (var Tag in ListTagCreate)
                    {
                        ProductTag ProductTag = new ProductTag();
                        ProductTag.ProductID = modelVm.ProductID;
                        ProductTag.TagID = Tag.TagID;
                        ListProductTag.Add(ProductTag);
                    }
                    _ProductTagReporistory.CreateProductTag(ListProductTag);
                    _ProductTagReporistory.Save();

                    var responseData = Mapper.Map <Product, ProductModel>(modelVm);
                    response = request.CreateResponse(HttpStatusCode.Created, responseData);
                }

                return response;
            }));
        }
        public object Post()
        {
            var tags = DoActionForGet <List <string> >(string.Empty, Constant.TagUrl);

            if (tags.Any())
            {
                _tagService.AddTags(tags);
            }
            return(GetDataWithMessage(() => new Tuple <object, string, bool>(_tagService.GetAll(), "Record imported successfully", true)));
        }
        private async Task <ApplicationUser> UpdateUserTags(ApplicationUser user, List <string> tags)
        {
            if (tags != null)
            {
                //user = await _context.Users.Where(u => u.Id == user.Id).Include(x => x.Tags).ThenInclude(ut => ut.Tag).FirstOrDefaultAsync();
                var tagsToRemove = user.Tags.Select(t => t.Name.ToLowerInvariant()).Except(tags).ToList(); // Get list of current tags not in the new tag list
                var tagsToAdd    = tags.Except(user.Tags.Select(t => t.Name.ToLowerInvariant())).ToList(); // Get list of new tags which aren't in the current tag list
                user = await _tagService.RemoveTags(tagsToRemove, user);                                   //can I clear this maybe?

                user = await _tagService.AddTags(tagsToAdd, user);
            }

            return(user);
        }
Exemple #7
0
        public ActionResult Create(TagViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var mappingTag = Mapper.Map <TagViewModel, Tag>(model);
            var tagsSplit  = mappingTag.Value.Split(',');
            var tags       = new List <Tag>();

            foreach (var item in tagsSplit)
            {
                tags.Add(new Tag {
                    Value = item, ArticleId = mappingTag.ArticleId
                });
            }
            _tagService.AddTags(tags);
            Logger.Log.Info($"{User.Identity.Name} добавил теги к статье №{model.ArticleId}");
            return(RedirectToAction("Index", new { id = model.ArticleId }));
        }
Exemple #8
0
        public async Task <PostEntity> CreateAsync(PostEntity postEntity)
        {
            var imagePath = _fileUploadService.UploadFile(postEntity.Image, AppSettingsHelper.GetImageFolder());

            var post = _mapper.Map <Post>(postEntity);

            post.ImagePath = imagePath != null ? $"{AppSettingsHelper.GetImageFolder()}/{imagePath}"
                                : AppSettingsHelper.GetDefaultImagePath();
            post.CreatedAt = DateTimeOffset.Now;
            post.UpdatedAt = DateTimeOffset.Now;

            _postRepository.Add(post);
            await _postRepository.SaveAsync();

            var tags = await _tagService.AddTags(post.Id, postEntity.Tags);

            var postResult = _mapper.Map <PostEntity>(post);

            postResult.Tags = tags.Select(t => $"#{t.Title}");

            return(postResult);
        }