Beispiel #1
0
        public User CreateUser(User user)
        {
            user.Joined = DateTime.Now;
            try
            {
                _context.Add(user);
                _context.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException.Message.Contains("unique constraint")) // hmm
                {
                    var existingUser = _context.Users
                                       .FirstOrDefault(u => u.Credentials.Username == user.Credentials.Username);
                    if (existingUser != null)
                    {
                        existingUser.Deleted = false;
                        _context.Update(existingUser);
                        return(existingUser);
                    }
                }
            }

            return(user);
        }
Beispiel #2
0
        /// <summary>
        /// Add a user to the data storage.
        /// </summary>
        /// <param name="user">The user to add.</param>
        public void Add(User user)
        {
            // Encrypt password before saving to database
            user.Password = SimpleMembershipProvider.EncryptPassword(user.Password);

            db.Users.Add(user);

            db.SaveChanges();
        }
Beispiel #3
0
        public Tweet CreateTweet(User author, string content)
        {
            var tweet = new Tweet {
                Author  = author.Id,
                Posted  = DateTime.Now,
                Content = content,
            };

            _context.Tweets.Add(tweet);
            _context.SaveChanges();
            _processContent(tweet);
            return(tweet);
        }
        private void CreateAncestryForNewReply(Tweet tweetThatWasRepliedTo, Tweet tweetThatIsAReply)
        {
            var ancestors = GetAncestors(tweetThatWasRepliedTo);

            ancestors.Add(tweetThatWasRepliedTo.Id);
            foreach (var ancestorId in ancestors)
            {
                _context.Add(new Ancestry()
                {
                    ChildId    = tweetThatIsAReply.Id,
                    AncestorId = ancestorId,
                });
            }
            _context.SaveChanges();
        }
Beispiel #5
0
        public Tweet DeleteTweet(CredentialsDto credentials, int tweet_id)
        {
            var tweet = _context.Tweets.Where(t => t.Id == tweet_id && !t.Deleted).SingleOrDefault();

            if (tweet == null || tweet.Author.Credentials.Username != credentials.Username)
            {
                throw new NotFoundCustomException("No tweet found", "No tweet found");
            }

            tweet.Deleted = true;
            _context.Tweets.Update(tweet);
            _context.SaveChanges();

            return(tweet);
        }
Beispiel #6
0
        public User CreateUser(User user)
        {
            user.Joined = DateTime.Now;
            try
            {
                _context.Add(user);
                _context.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException.Message.Contains("unique constraint")) // hmm
                {
                    throw new UsernameTakenException();
                }
            }

            return(user);
        }
Beispiel #7
0
        public void UpdateHashtag(string label)
        {
            var hashtag = GetHashtagByLabel(label);

            if (hashtag == null)
            {
                hashtag = new Hashtag {
                    Label     = label,
                    FirstUsed = DateTime.Now,
                    LastUsed  = DateTime.Now
                };

                _context.Hashtags.Add(hashtag);
                _context.SaveChanges();
            }
            else
            {
                hashtag.LastUsed = DateTime.Now;
                _context.Hashtags.Update(hashtag);
                _context.SaveChanges();
            }
        }
Beispiel #8
0
        /// <summary>
        ///     Create a new Hashtag, or update an existing Hashtag
        /// </summary>
        /// <exception cref="InvalidOperationException">Throws if a Tweet was passed that has no Id.</exception>
        public Hashtag UpsertHashtag(string label, Tweet tweet)
        {
            if (tweet.Id == 0)
            {
                throw new InvalidOperationException("An unsaved tweet can not be passed to this method.");
            }

            try
            {
                var tag = GetByLabel(label);
                tag.LastUsed = DateTime.Now;
                //tag.Tweets.Add(tweet);
                _context.Hashtags.Update(tag);
                var tweetHashtag = new TweetHashtag()
                {
                    Tweet   = tweet,
                    Hashtag = tag,
                };
                _context.TweetHashtags.Add(tweetHashtag);
                _context.SaveChanges();
                return(tag);
            }
            catch (HashtagNotFoundException)
            {
                var hashtag = new Hashtag(label);
                _context.Hashtags.Add(hashtag);
                var tweetHashtag = new TweetHashtag()
                {
                    Tweet   = tweet,
                    Hashtag = hashtag,
                };
                _context.TweetHashtags.Add(tweetHashtag);
                _context.SaveChanges();
                return(hashtag);
            }
        }
        public Tweet PostTweet(CreateTweetDto info)
        {
            var user         = _userService.GetByUsername(info.Credentials.Username);
            var tweetContent = info.TweetContent;

            if (user == null || user.Credentials.Password != info.Credentials.Password)
            {
                throw new NotImplementedException();
            }

            var tweet = new Tweet();

            tweet.Author       = user;
            tweet.TweetContent = tweetContent;

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

            return(tweet);
        }
Beispiel #10
0
 /// <summary>
 /// Method to add an item to the database.
 /// </summary>
 /// <param name="item">PostReply</param>
 public void Add(PostReply item)
 {
     db.PostReplies.Add(item);
     db.SaveChanges();
 }
 public void SaveChanges()
 {
     db.SaveChanges();
 }
Beispiel #12
0
 public void SaveChanges()
 {
     _context.SaveChanges();
 }
Beispiel #13
0
 /// <summary>
 /// Method to add an item to the database.
 /// </summary>
 /// <param name="item">Role</param>
 public void Add(Role item)
 {
     db.Roles.Add(item);
     db.SaveChanges();
 }
Beispiel #14
0
 /// <summary>
 /// Method to add an item to the database.
 /// </summary>
 /// <param name="item">Tag</param>
 public void Add(Tag item)
 {
     db.Tags.Add(item);
     db.SaveChanges();
 }
Beispiel #15
0
 /// <summary>
 ///
 /// </summary>
 public void SaveChanges()
 {
     _socialMediaContext.SaveChanges();
 }
Beispiel #16
0
 /// <summary>
 /// Method to add an item to the database.
 /// </summary>
 /// <param name="item">Post</param>
 public void Add(Post item)
 {
     db.Posts.Add(item);
     db.SaveChanges();
 }
Beispiel #17
0
 /// <summary>
 /// Method to add an item to the database.
 /// </summary>
 /// <param name="item">Comment</param>
 public void Add(Comment item)
 {
     db.Comments.Add(item);
     db.SaveChanges();
 }