Esempio n. 1
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)));
        }
Esempio n. 2
0
        public IHttpActionResult GetChatParticipants([FromUri] Guid chatId)
        {
            s_log.LogInfo($"{System.Reflection.MethodBase.GetCurrentMethod()}({chatId}) is called");

            if (chatId == Guid.Empty)
            {
                ModelState.AddModelError($"{nameof(chatId)}", "Incoming data is null");
                s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({chatId})", new ArgumentNullException(nameof(chatId), "Incoming data is null"));
                return(BadRequest(ModelState));
            }

            var userInfos = _userDataProvider.GetAllChatParticipantsByChatId(chatId);

            if (userInfos == null)
            {
                s_log.LogError($"{System.Reflection.MethodBase.GetCurrentMethod()}({chatId})", new Exception("Failed to get all chat participants"));
                return(NotFound());
            }

            return(Ok(userInfos.Select(x => new CTokenDto(x.Id))));
        }