Пример #1
0
        public async Task <IActionResult> Post(
            [FromBody] Article article,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <ArticleInfo> result = TokenUtils.CheckToken <ArticleInfo>(token, _context);

            if (result != null)
            {
                return(BadRequest(result));
            }

            Session sessionResult = await _context.Sessions
                                    .FirstOrDefaultAsync(s => s.SessionToken == token);

            article.User = await _context.Users
                           .FirstOrDefaultAsync(u => u.UserId == sessionResult.SessionUserId);

            if (article.PublishDate == DateTime.MinValue)
            {
                article.PublishDate = DateTime.Now;
            }

            if (article.ArticleId != 0)
            {
                result = new ModelResult <ArticleInfo>(400, null, "Invalid Article");
                return(BadRequest(result));
            }

            if (article.Title == null ||
                article.Content == null)
            {
                result = new ModelResult <ArticleInfo>(400, null, "Article Need Title or Content");
                return(BadRequest(result));
            }

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

            await NoticeUtils.CreateArticleNotice(article, _context);

            result = new ModelResult <ArticleInfo>(201, new ArticleInfo(article, _context), "Article Created");
            return(Ok(result));
        }