Example #1
0
        public async Task <IActionResult> Post(
            [FromBody] Tag tag,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <Tag> result = TokenUtils.CheckToken <Tag>(token, _context);

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

            if (tag.TagName == null || tag.TagId != 0)
            {
                result = new ModelResult <Tag>(400, tag, "Invalid Tag");
                return(BadRequest(result));
            }

            Tag tagResult = await _context.Tags
                            .FirstOrDefaultAsync(t => t.TagName == tag.TagName);

            if (tagResult != null)
            {
                result = new ModelResult <Tag>(409, tagResult, "Tag Exists");
                return(BadRequest(result));
            }

            await _context.Tags.AddAsync(tag);

            await _context.SaveChangesAsync();

            result = new ModelResult <Tag>(201, tag, "Tag Created");
            return(Ok(result));
        }
        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));
        }
        public async Task <IActionResult> Post([FromBody] User user)
        {
            ModelResult <User> result;

            if (user.Email == null ||
                user.Nickname == null ||
                user.PasswordHash == null ||
                user.UserId != 0)
            {
                result = new ModelResult <User>(400, user, "Invalid User Info");
                return(BadRequest(result));
            }

            var userResult = await _context.Users
                             .FirstOrDefaultAsync(u => u.Nickname == user.Nickname ||
                                                  u.Email == user.Email);

            if (userResult != null)
            {
                result = new ModelResult <User>(409, null, "User Exists");
                return(BadRequest(result));
            }

            if (user.SignDate == DateTime.MinValue)
            {
                user.SignDate = DateTime.Now;
            }

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            result = new ModelResult <User>(201, user, "User Created");
            return(Ok(result));
        }
Example #4
0
        public async Task <IActionResult> Post(
            int id,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <UserFollowInfo> result = TokenUtils.CheckToken <UserFollowInfo>(token, _context);

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

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

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

            UserFollow userFollowResult = await _context.UserFollows
                                          .FirstOrDefaultAsync(uf => uf.FollowingId == id &&
                                                               uf.FollowerId == sessionResult.SessionUserId);

            if (userFollowResult != null)
            {
                result = new ModelResult <UserFollowInfo>(400, null, "User Followed");
                return(BadRequest(result));
            }

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

            if (following == null)
            {
                result = new ModelResult <UserFollowInfo>(404, null, "User Not Exists");
                return(BadRequest(result));
            }

            UserFollow userFollow = new UserFollow
            {
                Follower    = follower,
                FollowerId  = follower.UserId,
                Following   = following,
                FollowingId = following.UserId
            };

            _context.Add(userFollow);
            await _context.SaveChangesAsync();

            await NoticeUtils.CreateFollowNotice(userFollow, _context);

            result = new ModelResult <UserFollowInfo>(201, null, "Add Follow Success");
            return(Ok(result));
        }
Example #5
0
        public async Task <IActionResult> Post(User user)
        {
            ModelResult <Session> result;
            User userResult;

            if (user.Email != null)
            {
                userResult = await _context.Users
                             .FirstOrDefaultAsync(u => u.Email == user.Email);
            }
            else if (user.Nickname != null)
            {
                userResult = await _context.Users
                             .FirstOrDefaultAsync(u => u.Nickname == user.Nickname);
            }
            else
            {
                userResult = await _context.Users
                             .FirstOrDefaultAsync(u => u.UserId == user.UserId);
            }

            if (userResult == null || userResult.PasswordHash != user.PasswordHash)
            {
                result = new ModelResult <Session>(409, null, "User not Exists or Password wrong");
                return(BadRequest(result));
            }

            string token;

            for (;;)
            {
                token = TokenUtils.GenerateSessionToken(userResult, _context);
                if (token != null)
                {
                    break;
                }
            }

            Session session = new Session
            {
                SessionToken  = token,
                SessionUserId = userResult.UserId,
                ExpiresTime   = DateTime.Now.AddDays(1)
            };
            await _context.Sessions.AddAsync(session);

            await _context.SaveChangesAsync();

            result = new ModelResult <Session>(201, session, "Session Created");
            return(Ok(result));
        }
        public async Task <IActionResult> Delete(
            [FromQuery(Name = "id")] int id,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <Notice> result = TokenUtils.CheckToken <Notice>(token, _context);

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

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

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

            Notice noticeResult = await _context.Notices.FirstOrDefaultAsync(n => n.UserId == id);

            if (userResult == null)
            {
                result = new ModelResult <Notice>(404, null, "User Not Exists");
                return(BadRequest(result));
            }

            if (userResult.UserId != noticeResult.UserId)
            {
                result = new ModelResult <Notice>(405, null, "Cannnot See Others' Notice");
                return(BadRequest(result));
            }

            _context.Notices.Remove(noticeResult);
            await _context.SaveChangesAsync();

            result = new ModelResult <Notice>(200, noticeResult, "Notice Deleted");
            return(Ok(result));
        }
Example #7
0
        public async Task <IActionResult> Post(IFormFile file, [FromQuery(Name = "token")] string token)
        {
            ModelResult <string> result = TokenUtils.CheckToken <string>(token, _context);

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

            if (file != null)
            {
                if (!Directory.Exists(uploadFolder))
                {
                    Directory.CreateDirectory(uploadFolder);
                }

                string hash      = FileHash(file);
                string extension = Path.GetExtension(file.FileName);
                string filename  = $@"{hash}{extension}";

                string filePath = $@"{uploadFolder}/{filename}";
                using (FileStream fs = System.IO.File.Create(filePath))
                {
                    file.CopyTo(fs);
                    await fs.FlushAsync();
                }

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

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

                int userId = userResult.UserId;

                UploadFile uploadFileResult = await _context.UploadFiles
                                              .FirstOrDefaultAsync(f => f.FileName == $@"{filename}");

                if (uploadFileResult == null)
                {
                    await _context.UploadFiles.AddAsync(new UploadFile
                    {
                        FileName   = $@"{filename}",
                        UploaderId = userId
                    });

                    await _context.SaveChangesAsync();
                }

                result = new ModelResult <string>(200, $@"/file/{filename}", null);
                return(Ok(result));
            }
            else
            {
                result = new ModelResult <string>(400, null, "Empty File");
                return(BadRequest(result));
            }
        }
Example #8
0
        public static async Task CreateCommentNotice(Comment comment, BSDNContext context, int userId, string type)
        {
            if (userId == 0)
            {
                return;
            }
            Notice notice = new Notice(userId, $@"New {type}", $@"/api/article/{comment.ArticleId}");
            await context.Notices.AddAsync(notice);

            await context.SaveChangesAsync();
        }
Example #9
0
        public static async Task CreateFollowNotice(UserFollow userFollow, BSDNContext context)
        {
            User follower = await context.Users.FirstOrDefaultAsync(u => u.UserId == userFollow.FollowerId);

            User following = await context.Users.FirstOrDefaultAsync(u => u.UserId == userFollow.FollowingId);

            Notice notice = new Notice(following.UserId, $@"New Follower|{follower.Nickname}|{follower.UserId}", null);
            await context.AddAsync(notice);

            await context.SaveChangesAsync();
        }
Example #10
0
        public async Task <IActionResult> Delete(
            int articleId,
            [FromQuery(Name = "id")] int tagId,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <ArticleTag> result = TokenUtils.CheckToken <ArticleTag>(token, _context);

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

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

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

            Article articleResult = await _context.Articles
                                    .FirstOrDefaultAsync(a => a.ArticleId == articleId);

            if (tagResult == null || articleResult == null)
            {
                result = new ModelResult <ArticleTag>(404, null, "Article or Tag Not Exists");
                return(BadRequest(result));
            }

            if (sessionResult.SessionUserId != articleResult.UserId)
            {
                result = new ModelResult <ArticleTag>(403, null, "You Cannot Add Tag for Others' Article");
                return(BadRequest(result));
            }

            ArticleTag articleTagResult = await _context.ArticleTags
                                          .FirstOrDefaultAsync(at => at.ArticleId == articleId && at.TagId == tagId);

            if (articleTagResult == null)
            {
                result = new ModelResult <ArticleTag>(405, null, "Article Tag Not Exists");
                return(BadRequest(result));
            }

            _context.ArticleTags.Remove(articleTagResult);
            await _context.SaveChangesAsync();

            result = new ModelResult <ArticleTag>(200, articleTagResult, "Tag Deleted");
            return(Ok(result));
        }
Example #11
0
        public static async Task CreateArticleNotice(Article article, BSDNContext context)
        {
            User userResult = await context.Users.FirstOrDefaultAsync(u => u.UserId == article.UserId);

            List <Notice> notices = await context.UserFollows
                                    .Where(uf => uf.FollowingId == userResult.UserId)
                                    .Select(uf => uf.FollowerId)
                                    .Select(uid => new Notice(uid, $@"New Article|{article.Title}|{article.ArticleId}",
                                                              $@"/api/article/{article.ArticleId}"))
                                    .ToListAsync();

            foreach (var notice in notices)
            {
                await context.Notices.AddAsync(notice);
            }

            await context.SaveChangesAsync();
        }
        public async Task <IActionResult> Post(
            string type,
            int id,
            [FromBody] Comment comment,
            [FromQuery(Name = "token")] string token)
        {
            // 先评论是否有正文
            // 再检查Token是否有效
            // 再检查评论类型
            // 再检查文章/评论是否存在
            int     replyCommentId = 0;
            Comment commentResult  = null;

            ModelResult <CommentInfo> result = TokenUtils.CheckToken <CommentInfo>(token, _context);

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

            if (comment.Content == null || comment.CommentId != 0)
            {
                result = new ModelResult <CommentInfo>(400, new CommentInfo(comment), "Invalid Comment");
                return(BadRequest(result));
            }

            comment.PublishDate = DateTime.Now;

            if (type == "article" || type == "reply")
            {
                Article articleResult;
                if (type == "article")
                {
                    if (comment.ArticleId == 0)
                    {
                        comment.ArticleId = id;
                    }
                    if (id == 0)
                    {
                        id = comment.ArticleId;
                    }

                    articleResult = await _context.Articles
                                    .FirstOrDefaultAsync(a => a.ArticleId == id);
                }
                else // if (type == "reply")
                {
                    commentResult = await _context.Comments
                                    .FirstOrDefaultAsync(c => c.CommentId == id);

                    if (commentResult == null)
                    {
                        result = new ModelResult <CommentInfo>(404, null, "Comment to Reply Not Exists");
                        return(BadRequest(result));
                    }

                    replyCommentId = commentResult.CommentId;
                    articleResult  = await _context.Articles
                                     .FirstOrDefaultAsync(a => a.ArticleId == commentResult.ArticleId);

                    comment.ArticleId = articleResult.ArticleId;
                }

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

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

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

                comment.ArticleId = id;
                comment.Article   = articleResult;

                await _context.AddAsync(comment);

                await _context.SaveChangesAsync();

                result = new ModelResult <CommentInfo>(201, new CommentInfo(comment), "Commented");
            }
            else
            {
                result = new ModelResult <CommentInfo>(405, null, "Undefined Comment Type");
                return(BadRequest(result));
            }

            if (type == "reply" && replyCommentId != 0)
            {
                await _context.AddAsync(new CommentReply
                {
                    CommentId        = comment.CommentId,
                    RepliedCommentId = replyCommentId
                });

                await _context.SaveChangesAsync();

                result.Message = "Replied";
            }

            int noticeUserId = 0;

            if (type == "article")
            {
                noticeUserId = comment.UserId;
            }
            else
            {
                if (commentResult != null)
                {
                    noticeUserId = commentResult.UserId;
                }
            }

            await NoticeUtils.CreateCommentNotice(comment, _context, noticeUserId, type);

            return(Ok(result));
        }