public async Task <IActionResult> Get(int id, [FromQuery(Name = "token")] string token)
        {
            ModelResult <ArticleInfo> result;
            var articleResult = await _context.Articles
                                .FirstOrDefaultAsync(a => a.ArticleId == id);

            if (articleResult == null)
            {
                result = new ModelResult <ArticleInfo>(404, null, "Article Not Exists");
                return(BadRequest(result));
            }

            if (token != null && TokenUtils.CheckToken <ArticleInfo>(token, _context) == null)
            {
                // Token有效,点击量+1
                articleResult.ViewNumber           += 1;
                _context.Entry(articleResult).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }

            articleResult.User = await _context.Users
                                 .FirstOrDefaultAsync(u => u.UserId == articleResult.UserId);

            articleResult.ArticleTags = await _context.ArticleTags
                                        .Where(at => at.ArticleId == articleResult.ArticleId).ToListAsync();

            ArticleInfo articleInfo = new ArticleInfo(articleResult, _context);

            articleInfo.CommentCount = _context.Comments
                                       .Count(c => c.ArticleId == articleInfo.ArticleId);
            result = new ModelResult <ArticleInfo>(200, articleInfo, "Article Exists");
            return(Ok(result));
        }
Exemple #2
0
        public async Task <IActionResult> Put(
            int id,
            [FromBody] Tag tag,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <Tag> result = TokenUtils.CheckToken <Tag>(token, _context);

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

            Tag tagResult = await _context.Tags.FirstOrDefaultAsync(t => t.TagId == id);

            if (tagResult == null)
            {
                result = new ModelResult <Tag>(405, null, "Tag Not Found");
                return(BadRequest(result));
            }

            if (id != tag.TagId)
            {
                result = new ModelResult <Tag>(405, null, "Cannot Modify TagId");
                return(BadRequest(result));
            }

            _context.Entry(tag).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            result = new ModelResult <Tag>(200, null, "Tag Modified");
            return(Ok(result));
        }
        public async Task <IActionResult> Put(
            int id,
            [FromBody] User user,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <User> result = TokenUtils.CheckToken <User>(token, _context);

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

            User userResult = await _context.Users
                              .FirstOrDefaultAsync(u => u.UserId == id);

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

            if (userResult == null || userResult.UserId != sessionResult.SessionUserId)
            {
                result = new ModelResult <User>(405, null, "User Not Exists or Token not suit");
                return(BadRequest(result));
            }

            if (id != user.UserId)
            {
                result = new ModelResult <User>(405, null, "Cannot Modify UserId");
                return(BadRequest(result));
            }

            userResult.Email        = user.Email;
            userResult.Nickname     = user.Nickname;
            userResult.PasswordHash = user.PasswordHash;
            userResult.AvatarUrl    = user.AvatarUrl;
            userResult.Intro        = user.Intro;

            _context.Entry(userResult).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            result = new ModelResult <User>(200, null, "User Modified");
            return(Ok(result));
        }
Exemple #4
0
        public async Task <IActionResult> Put([FromQuery(Name = "token")] string token)
        {
            ModelResult <Session> result;
            Session sessionResult = await _context.Sessions.FirstOrDefaultAsync(s => s.SessionToken == token);

            if (sessionResult == null)
            {
                result = new ModelResult <Session>(404, null, "Token Not Exists");
                return(BadRequest(result));
            }
            else
            {
                sessionResult.ExpiresTime           = DateTime.Now.AddDays(1);
                _context.Entry(sessionResult).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                result = new ModelResult <Session>(200, sessionResult, null);
                return(Ok(result));
            }
        }