Esempio n. 1
0
        public void Like(Guid commentId, Guid userId)
        {
            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                // Retrive the user or throw an exception.
                var comment = _dbContext.Comments.FromSqlRaw("SELECT * FROM `Comments` WHERE `Id` = {0}", commentId).Single();

                // If there already exist a like record, return directly.
                if (null != _likeRepository.GetByCommentIdAndUserId(commentId, userId))
                {
                    return;
                }

                // Try to insert like record.
                _likeRepository.Insert(new Like
                {
                    UserId    = userId,
                    CommentId = commentId
                });

                // Increment the count of likes.
                comment.CountOfLikes++;

                // Execute related update sql.
                _dbContext.SaveChanges();

                // Commit changes.
                transaction.Commit();
            }
        }
Esempio n. 2
0
        public void SaveChanges()
        {
            var entries = _context.ChangeTracker
                          .Entries()
                          .Where(entry => entry.Entity is EntityBase);

            entries
            .Where(entry => entry.State == EntityState.Added)
            .ToList()
            .ForEach(entry => ((EntityBase)entry.Entity).CreatedAt = DateTime.Now);

            entries
            .ToList()
            .ForEach(entry => ((EntityBase)entry.Entity).UpdatedAt = DateTime.Now);

            _context.SaveChanges();
        }