Exemplo n.º 1
0
        public async Task <int> CreateCommentAsync(Comment newComment)
        {
            await base._dBContext.ExecuteTransactionAsync(new Dictionary <string, object>()
            {
                {
                    $@"INSERT INTO Comment (ShoutId, UserId, Content)
                       VALUES ({ newComment.ShoutId.ToString() }, { newComment.UserId.ToString() }, @Content)
                    ",
                    null
                },
                {
                    $@"UPDATE Shout
                       SET CommentCount = CommentCount + 1
                       WHERE Id = { newComment.ShoutId }
                    ",
                    null
                }
            }, false);

            int commentId = await base._dBContext.DbConnection.ExecuteScalarAsync <int>(
                DapperHelperQueries.SelectSessionLastInserted("Comment", "id"),
                new { Content = newComment.Content }
                );

            _ = base._dBContext.DbConnection.DisposeAsync();
            return(commentId);
        }
Exemplo n.º 2
0
        public async Task <int> CreateReactionAsync(EntityType entityType, int entityId, int userId, short reactionTypeId)
        {
            string reactionTableName = entityType == EntityType.Shout ? TableNames.ShoutReaction : TableNames.CommentReaction;
            string entityTableName   = entityType == EntityType.Shout ? TableNames.Shout : TableNames.Comment;

            string reactionType = reactionTypeId == ReactionTypeId.Like ?
                                  ReactionTypeColumnNames.Like :
                                  ReactionTypeColumnNames.Dislike;

            await base._dBContext.ExecuteTransactionAsync(new Dictionary <string, object>()
            {
                {
                    $@"INSERT INTO {reactionTableName} ({entityTableName}Id, UserId, ReactionTypeId)
                       VALUES ( {entityId.ToString()}, {userId.ToString()}, {reactionTypeId.ToString()} )",
                    null
                },
                {
                    $@"UPDATE {entityTableName}
                       SET {reactionType} = {reactionType} + 1
                       WHERE Id = {entityId}",
                    null
                }
            }, false);

            // Reuse "shoutId" variable to alocate less memory.
            entityId = await base._dBContext.DbConnection.ExecuteScalarAsync <int>(DapperHelperQueries.SelectSessionLastInserted(reactionTableName, "id"));

            // Do not await.
            _ = base._dBContext.DbConnection.DisposeAsync();
            return(entityId);
        }