示例#1
0
        public async Task <ActionResult <Articles> > PostArticles(ArticlesModel articlesModel)
        {
            var articles = new Articles()
            {
                ArticleId        = articlesModel.ArticleId,
                Name             = articlesModel.Name,
                HeroImage        = articlesModel.HeroImage,
                ShortDescription = articlesModel.ShortDescription,
                Description      = articlesModel.Description,
                PublicationDate  = articlesModel.PublicationDate,
                Category         = articlesModel.CategoryId
            };

            _context.Articles.Add(articles);
            await _context.SaveChangesAsync();

            for (int i = 0; i < articlesModel.Tags.Length; i++)
            {
                var articleTag = new ArticlesTags()
                {
                    ArticleId = articles.ArticleId,
                    TagId     = Convert.ToInt32(articlesModel.Tags[i])
                };

                _context.ArticlesTags.Add(articleTag);
            }


            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetArticles", new { id = articles.ArticleId }, articles));
        }
示例#2
0
        public async Task <IActionResult> PutArticles(int id, ArticlesModel articlesModel)
        {
            if (id != articlesModel.ArticleId)
            {
                return(BadRequest());
            }

            var articles = new Articles()
            {
                ArticleId        = articlesModel.ArticleId,
                Name             = articlesModel.Name,
                HeroImage        = articlesModel.HeroImage,
                ShortDescription = articlesModel.ShortDescription,
                Description      = articlesModel.Description,
                PublicationDate  = articlesModel.PublicationDate,
                Category         = articlesModel.CategoryId
            };

            _context.Entry(articles).State = EntityState.Modified;

            var articleTags = _context.ArticlesTags.Where(s => s.ArticleId == articlesModel.ArticleId);

            _context.ArticlesTags.RemoveRange(articleTags);

            for (int i = 0; i < articlesModel.Tags.Length; i++)
            {
                var articleTag = new ArticlesTags()
                {
                    ArticleId = articles.ArticleId,
                    TagId     = _context.Tags.Where(name => name.TagName == articlesModel.Tags[i]).Select(s => s.TagId).First()
                };

                _context.ArticlesTags.Add(articleTag);
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ArticlesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }