Example #1
0
        public ActionResult Retweet([FromBody] JwtWtihObject <Guid> jwtWithTweetId)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithTweetId.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter/retweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            Guid tweetId;

            try
            {
                tweetId = jwtWithTweetId.WithJWTObject;
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter/retweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }


            var tweet = _db.Tweets
                        .FirstOrDefault(p => p.Id == tweetId);

            if (tweet == null)
            {
                return(NotFound("There is no tweet with such Id"));
            }

            var retweet = new Tweet(tweet);

            retweet.UserIdRetweetedFrom = retweet.UserId;
            retweet.UserId = user.Id;
            _db.Tweets.Add(retweet);
            _db.SaveChanges();


            _logger.LogInformation($"[POST]api/twitter/retweet;" +
                                   $"User [{user.Login}] retweeted tweet [{tweet.Id}]. So, tweet [{retweet.Id}] is created");
            return(Ok(retweet));
        }
Example #2
0
        public ActionResult AddTweet([FromBody] JwtWtihObject <Tweet> jwtWithTweet)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithTweet.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            var tweetJSON = jwtWithTweet.WithJWTObject;

            if (tweetJSON == null)
            {
                return(BadRequest("No tweet object in request"));
            }
            tweetJSON.UserId = token.PAYLOAD.Sub;

            Tweet tweet;

            try
            {
                tweet = new Tweet(tweetJSON);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter;" + ex.Message);
                return(BadRequest(ex.Message));
            }

            _db.Tweets.Add(tweet);
            _db.SaveChanges();


            _logger.LogInformation($"[POST]api/twitter;" +
                                   $"User [{user.Login}] added tweet [{tweet.Id}]");
            return(Ok(tweet));
        }
        public ActionResult Subscribe([FromBody] JwtWtihObject <string> subInfo)
        {
            JWT token;

            try
            {
                token = new JWT(subInfo.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/auth/subscribe" + ex.Message);
                return(BadRequest(ex.Message));
            }

            var userWhoSubscribe = _db.Users
                                   .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (userWhoSubscribe == null)
            {
                return(NotFound("No user that matches this JWT"));
            }

            var userToSubscribe = _db.Users
                                  .FirstOrDefault(p => p.Login == subInfo.WithJWTObject);

            if (userToSubscribe == null)
            {
                return(NotFound("No user that matches you entered login"));
            }
            if (userToSubscribe == userWhoSubscribe)
            {
                return(BadRequest("You can't subscribe on yourself"));
            }

            var subSub = new SubscriberSubscription
            {
                WhoSubscribeId    = userWhoSubscribe.Id,
                OnWhomSubscribeId = userToSubscribe.Id
            };

            _db.SubscriberSubscriptions.Add(subSub);
            _db.SaveChanges();


            _logger.LogInformation($"[POST]api/auth/subscribe;" +
                                   $"User [{userWhoSubscribe.Login}] subscribed to [{userToSubscribe.Id}]");
            return(Ok(subSub));
        }
        public ActionResult Unsubscribe([FromBody] JwtWtihObject <string> subInfo)
        {
            JWT token;

            try
            {
                token = new JWT(subInfo.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/auth/unsubscribe" + ex.Message);
                return(BadRequest(ex.Message));
            }

            var userWhoSubscribe = _db.Users.FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (userWhoSubscribe == null)
            {
                return(NotFound("No user that matches this JWT"));
            }

            var userToSubscribe = _db.Users.FirstOrDefault(p => p.Login == subInfo.WithJWTObject);

            if (userToSubscribe == null)
            {
                return(NotFound("No user in database that matches you entered login"));
            }

            var subSub = _db.SubscriberSubscriptions
                         .FirstOrDefault(p => p.WhoSubscribeId == userWhoSubscribe.Id &&
                                         p.OnWhomSubscribeId == userToSubscribe.Id);

            if (subSub == null)
            {
                string ex = "You are not subscribed to this user";
                _logger.LogError($"[DELETE]api/auth/unsubscribe" + ex);
                return(BadRequest(ex));
            }

            _db.SubscriberSubscriptions.Remove(subSub);
            _db.SaveChanges();


            _logger.LogInformation($"[DELETE]api/auth/unsubscribe;" +
                                   $"User [{userWhoSubscribe.Login}] unsubscribed from [{userToSubscribe.Id}]");
            return(Ok(subSub));
        }
Example #5
0
        public ActionResult RateTweet([FromBody] JwtWtihObject <TweetIdWithObject <RateState> > jwtWithInfo)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithInfo.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[PUT]api/twitter/rate-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            Guid tweetId;

            try
            {
                tweetId = jwtWithInfo.WithJWTObject.TweetId;
            }
            catch (Exception ex)
            {
                _logger.LogError($"[PUT]api/twitter/rate-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var tweet = _db.Tweets
                        .FirstOrDefault(p => p.Id == tweetId);

            if (tweet == null)
            {
                return(NotFound("User doesn't have tweet with such Id"));
            }


            int stateFromJSON = (int)jwtWithInfo.WithJWTObject.WithTweetObject;

            if (stateFromJSON != -1 && stateFromJSON != 0 && stateFromJSON != 1)
            {
                string ex = "There is no such rate state";
                _logger.LogError($"[PUT]api/twitter/rate-tweet;" + ex);
                return(BadRequest(ex));
            }


            _db.Entry(tweet).Collection(p => p.TweetRateStates).Load();
            var stateFromDb = tweet.TweetRateStates
                              .FirstOrDefault(p => p.UserId == user.Id);

            if (stateFromDb != null)
            {
                if ((int)stateFromDb.RateState == stateFromJSON)
                {
                    string ex = "You have already rated this tweet the same way";
                    _logger.LogError($"[PUT]api/twitter/rate-tweet;" + ex);
                    return(BadRequest(ex));
                }



                if (stateFromDb.RateState == RateState.Dislike)
                {
                    tweet.Dislikes--;
                }
                else if (stateFromDb.RateState == RateState.Like)
                {
                    tweet.Likes--;
                }



                stateFromDb.RateState = (RateState)stateFromJSON;
            }
            else
            {
                var newState = new UserRateState
                {
                    RateState = (RateState)stateFromJSON,
                    TweetId   = tweet.Id,
                    UserId    = user.Id
                };
                _db.UserRateStates.Add(newState);
            }

            if ((RateState)stateFromJSON == RateState.Dislike)
            {
                tweet.Dislikes++;
            }
            else if ((RateState)stateFromJSON == RateState.Like)
            {
                tweet.Likes++;
            }

            _db.Tweets.Update(tweet);
            _db.SaveChanges();


            _logger.LogInformation($"[PUT]api/twitter/rate-tweet;" +
                                   $"User [{user.Login}] rated tweet [{tweet.Id}]");
            return(Ok());
        }
Example #6
0
        public ActionResult DeleteCommentToTweet([FromBody] JwtWtihObject <Guid> jwtWithCommentId)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithCommentId.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/twitter/comment-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            Guid commentId;

            try
            {
                commentId = jwtWithCommentId.WithJWTObject;
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/twitter/comment-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }


            Comment comment;

            if (user.IsAdmin)
            {
                comment = _db.Comments
                          .FirstOrDefault(p => p.Id == commentId);
                if (comment == null)
                {
                    return(NotFound("There is no comment with such Id"));
                }
            }
            else
            {
                _db.Entry(user).Collection(p => p.TweetComments).Load();
                comment = user.TweetComments
                          .FirstOrDefault(p => p.Id == commentId);
                if (comment == null)
                {
                    return(NotFound("You haven't comment with such Id"));
                }
            }

            _db.Comments.Remove(comment);
            _db.SaveChanges();


            _logger.LogInformation($"[DELETE]api/twitter/comment-tweet;" +
                                   $"User [{user.Login}] deleted comment [{comment.Id}]");
            return(Ok(comment));
        }
Example #7
0
        public ActionResult WriteCommentToTweet([FromBody] JwtWtihObject <TweetIdWithObject <string> > jwtWithComment)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithComment.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter/comment-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            Guid tweetId;

            try
            {
                tweetId = jwtWithComment.WithJWTObject.TweetId;
            }
            catch (Exception ex)
            {
                _logger.LogError($"[POST]api/twitter/comment-tweet;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var tweet = _db.Tweets
                        .FirstOrDefault(p => p.Id == tweetId);

            if (tweet == null)
            {
                return(NotFound("User doesn't have tweet with such Id"));
            }

            var commentCont = jwtWithComment.WithJWTObject.WithTweetObject;

            if (commentCont == null || commentCont.Length == 0)
            {
                string ex = "Comment must include at least one symbol";
                _logger.LogError($"[POST]api/twitter/comment-tweet;" + ex);
                return(BadRequest(ex));
            }

            var comment = new Comment
            {
                Id      = new Guid(),
                Content = commentCont,
                TweetId = tweet.Id,
                UserId  = user.Id
            };

            _db.Comments.Add(comment);
            _db.SaveChanges();


            _logger.LogInformation($"[POST]api/twitter/comment-tweet;" +
                                   $"User [{user.Login}] commented tweet [{tweet.Id}]. So, comment [{comment.Id}] is created");
            return(Ok(comment));
        }
Example #8
0
        public ActionResult DeleteTweet([FromBody] JwtWtihObject <Guid> jwtWithTweetId)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithTweetId.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/twitter;" + ex.Message);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }



            Guid tweetId;

            try
            {
                tweetId = jwtWithTweetId.WithJWTObject;
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/twitter;" + ex.Message);
                return(BadRequest(ex.Message));
            }


            Tweet tweet;

            if (user.IsAdmin)
            {
                tweet = _db.Tweets
                        .FirstOrDefault(p => p.Id == tweetId);
                if (tweet == null)
                {
                    return(NotFound("There is no tweet with such Id"));
                }
            }
            else
            {
                _db.Entry(user).Collection(p => p.Tweets).Load();
                tweet = user.Tweets
                        .FirstOrDefault(p => p.Id == tweetId);
                if (tweet == null)
                {
                    return(NotFound("You haven't tweet with such Id"));
                }
            }

            _db.Entry(tweet).Collection(p => p.TweetComments).Load();
            _db.Entry(tweet).Collection(p => p.TweetRateStates).Load();

            _db.Comments.RemoveRange(tweet.TweetComments); //чістка вручную, т.к. в контексте DeleteBehaviour.Restrict (а по другому і нельзя)
            _db.UserRateStates.RemoveRange(tweet.TweetRateStates);
            _db.Tweets.Remove(tweet);
            _db.SaveChanges();



            _logger.LogInformation($"[DELETE]api/twitter;" +
                                   $"User [{user.Login}] deleted tweet [{tweet.Id}]");
            return(Ok(tweet));
        }
        public ActionResult DeleteUserAdmin([FromBody] JwtWtihObject <string> jwtWithUserLogin)
        {
            JWT token;

            try
            {
                token = new JWT(jwtWithUserLogin.JWT);
            }
            catch (Exception ex)
            {
                _logger.LogError($"[DELETE]api/auth/admin-delete" + ex);
                return(BadRequest(ex.Message));
            }
            var user = _db.Users
                       .FirstOrDefault(p => p.Id == token.PAYLOAD.Sub);

            if (user == null)
            {
                return(NotFound("Your jwt doesn't match any user!"));
            }
            if (!user.IsAdmin)
            {
                string ex = "This method is only for administrators";
                _logger.LogError($"[DELETE]api/auth/admin-delete" + ex);
                return(StatusCode(403, ex));
            }



            var userToDelete = _db.Users
                               .Include(p => p.Subscribers)
                               .Include(p => p.Subscriptions)
                               .Include(p => p.Tweets)
                               .Include(p => p.TweetRateStates)
                               .FirstOrDefault(p => p.Login == jwtWithUserLogin.WithJWTObject);

            if (userToDelete == null)
            {
                return(NotFound("There is no user with such Id"));
            }
            if (userToDelete == user)
            {
                string ex = "You can't delete this account with this method";
                _logger.LogError($"[DELETE]api/auth/admin-delete" + ex);
                return(StatusCode(403, ex));
            }

            #region  учнаяЧистка(т.к. в контексте DeleteBehaviour.Restrict, а по другому і нельзя)
            _db.SubscriberSubscriptions.RemoveRange(userToDelete.Subscriptions);
            _db.SubscriberSubscriptions.RemoveRange(userToDelete.Subscribers);
            _db.UserRateStates.RemoveRange(userToDelete.TweetRateStates);
            foreach (var tweet in userToDelete.Tweets)
            {
                var tweetComments = _db.Comments
                                    .Where(p => p.TweetId == tweet.Id).ToList();
                _db.Comments.RemoveRange(tweetComments);

                var tweetRates = _db.UserRateStates
                                 .Where(p => p.TweetId == tweet.Id).ToList();
                _db.UserRateStates.RemoveRange(tweetRates);
            }
            #endregion
            _db.Users.Remove(userToDelete);
            _db.SaveChanges();

            _logger.LogInformation($"[DELETE]api/auth/admin-delete;" +
                                   $"User [{userToDelete.Login}] was deleted by admin [{user.Login}]");
            return(Ok());
        }