Beispiel #1
0
        public IHttpActionResult PostReadMessages([FromBody] CReadMessagesDto readMessages)
        {
            if (readMessages == null)
            {
                ModelState.AddModelError($"{nameof(readMessages)}", "Incoming data is null");
                return(BadRequest(ModelState));
            }

            foreach (var messageId in readMessages.ReadMessages)
            {
                var messageInChatInfo = new CMessageInChatInfo(
                    default(Guid),
                    messageId,
                    default(Guid),
                    default(Guid),
                    readMessages.UserId,
                    true
                    );

                var rowsAffected = _messageInChatDataProvider.UpdateReadMessageInChat(messageInChatInfo);

                if (rowsAffected == 0)
                {
                    s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({readMessages})", new Exception("Failed to update message in chat"));
                    return(InternalServerError());
                }
            }

            return(Ok(true));
        }
Beispiel #2
0
        public IHttpActionResult PostMessage([FromBody] CNewMessageDto message)
        {
            if (message == null)
            {
                ModelState.AddModelError($"{nameof(message)}", "Incoming data is null");
                return(BadRequest(ModelState));
            }

            var messageInfoToPost = new CMessageInfo(Guid.Empty, message.DispatchDate, message.MessageText, message.Type, message.ContentUri, message.SenderId, true, String.Empty, default(Int64));

            var messageInfoRetrieved = _messageDataProvider.CreateMessage(messageInfoToPost);

            if (messageInfoRetrieved == null || messageInfoRetrieved.Id == Guid.Empty)
            {
                s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({message})", new Exception("Failed to post message: can't get message info"));
                return(InternalServerError());
            }

            var chatParticipants = _userDataProvider.GetAllChatParticipantsByChatId(message.ChatId);

            if (chatParticipants == null || chatParticipants.Count == 0)
            {
                s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({message})", new Exception("Failed to post message: can't get chat participants"));
                return(InternalServerError());
            }

            CUserInfo senderInfo = null;

            //get all participants
            foreach (var user in chatParticipants)
            {
                //TODO А если сами себе пересылаем сообщение?
                //if (user.Id == message.SenderId)
                //    continue;
                if (user.Id == message.SenderId)
                {
                    senderInfo = user;
                }

                var messageInChatInfo = new CMessageInChatInfo(
                    Guid.Empty,
                    messageInfoRetrieved.Id,
                    message.ChatId,
                    message.SenderId,
                    user.Id,
                    user.Id == message.SenderId
                    );

                var rowsAffected = _messageInChatDataProvider.CreateMessageInChat(messageInChatInfo);

                if (rowsAffected == 0)
                {
                    s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({message})", new Exception("Failed to post message: can't create message in chat"));
                    return(InternalServerError());
                }
            }

            return(Ok(new CMessagePostedDto(messageInfoRetrieved.Id, messageInfoRetrieved.DispatchDate, senderInfo != null ? senderInfo.Login : String.Empty, messageInfoRetrieved.Usn)));
        }
        //        #region Static
        //        public static Int32 CreateMessageInChat(CMessageInChatInfo messageInChat)
        //        {
        //            #region Sql

        //            var sql = @"
        //INSERT INTO messagesInChats (Id, MessageId, ChatId, FromUserId, ToUserId, IsRead)
        //    VALUES (
        //DEFAULT, @MessageId, @ChatId, @FromUserId, @ToUserId, @IsRead
        //)
        //";

        //            #endregion

        //            return CDbQueryExecutor.CreateItemParametrized(sql,
        //                SSqlParameterCreator.Create(
        //                    "@MessageId", messageInChat.MessageId, SqlDbType.UniqueIdentifier, false
        //                    ),
        //                SSqlParameterCreator.Create(
        //                    "@ChatId", messageInChat.ChatId, SqlDbType.UniqueIdentifier, false
        //                    ),
        //                SSqlParameterCreator.Create(
        //                    "@FromUserId", messageInChat.FromUserId, SqlDbType.UniqueIdentifier, false
        //                    ),
        //                SSqlParameterCreator.Create(
        //                    "@ToUserId", messageInChat.ToUserId, SqlDbType.UniqueIdentifier, false
        //                    ),
        //                SSqlParameterCreator.Create(
        //                    "@IsRead", messageInChat.IsRead, SqlDbType.Bit, false
        //                    )
        //            );
        //        }

        //        public static Int32 UpdateReadMessageInChat(CMessageInChatInfo messageInChat)
        //        {
        //            #region Sql

        //            var sql = @"
        //UPDATE messagesInChats
        //SET IsRead = @IsRead
        //    WHERE ToUserId = @ToUserId
        //    AND MessageId = @MessageId
        //";

        //            #endregion

        //            return CDbQueryExecutor.CreateItemParametrized(sql,
        //                SSqlParameterCreator.Create(
        //                    "@MessageId", messageInChat.MessageId, SqlDbType.UniqueIdentifier, false
        //                ),
        //                SSqlParameterCreator.Create(
        //                    "@ToUserId", messageInChat.ToUserId, SqlDbType.UniqueIdentifier, false
        //                ),
        //                SSqlParameterCreator.Create(
        //                    "@IsRead", messageInChat.IsRead, SqlDbType.Bit, false
        //                )
        //            );
        //        }
        //        #endregion

        public Int32 CreateMessageInChat(CMessageInChatInfo messageInChat)
        {
            s_log.LogInfo($@"Data provider's method '{nameof(CreateMessageInChat)}({messageInChat})' is called");

            #region Sql

            var sql = @"
INSERT INTO messagesInChats (Id, MessageId, ChatId, FromUserId, ToUserId, IsRead)
    VALUES (
DEFAULT, @MessageId, @ChatId, @FromUserId, @ToUserId, @IsRead
)
";

            #endregion

            using (IDbConnection connection = new SqlConnection(_dbSettings.DbConnectionString))
            {
                using (CDbTransactionQueryExecutor executor = new CDbTransactionQueryExecutor(connection))
                {
                    try
                    {
                        var result = executor.CreateItem(sql,
                                                         SSqlParameterCreator.Create(
                                                             "@MessageId", messageInChat.MessageId, SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@ChatId", messageInChat.ChatId, SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@FromUserId", messageInChat.FromUserId, SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@ToUserId", messageInChat.ToUserId, SqlDbType.UniqueIdentifier, false
                                                             ),
                                                         SSqlParameterCreator.Create(
                                                             "@IsRead", messageInChat.IsRead, SqlDbType.Bit, false
                                                             )
                                                         );

                        executor.Commit();
                        return(result);
                    }
                    catch (SqlException e)
                    {
                        s_log.LogError($@"{nameof(CreateMessageInChat)}({messageInChat}): Error occured during SQL query execution", e);
                        s_log.LogInfo($@"{nameof(CreateMessageInChat)}({messageInChat}): Operation was rolled back because of error");
                        Console.WriteLine($@"{nameof(CreateMessageInChat)}({messageInChat}): Error occured during SQL query execution");
                        Console.WriteLine("Operation was rolled back because of error");
                        return(0);
                    }
                }
            }
        }