public IHttpActionResult GetAllUsersFromChat(int userId, string chat, int?page = null, int pageSize = 10)
        {
            Logger.log.Debug("at ChatController.GetAllUsersFromChat");
            IQueryable <UserInfoDTO> users = null;

            if (!this.CheckUserIdAcces(userId))
            {
                Logger.log.Error("at ChatController.GetAllUsersFromChat - User request denied");
                return(new Forbidden("User cannot have acces to other chats!"));
            }

            int chatId = this.CreateChatIdFromProxy(chat);

            using (var client = SoapProvider.GetChatServiceClient())
            {
                if (!this.IfUserHasAccesToChat(client, chatId, userId))
                {
                    Logger.log.Error("at ChatController.GetChatById - User request denied");
                    return(new Forbidden("User cannot have acces to other chat!"));
                }

                users = client.GetAllUsersFromChat(chatId).AsQueryable();
            }

            if (users == null)
            {
                Logger.log.Error("at ChatController.GetAllUsersFromChat - Error at database");
                return(InternalServerError());
            }

            var data = this.ApplyPaging(users, page.Value, pageSize, "GetAllUsersFromChat");

            return(Ok(data));
        }
        public IHttpActionResult AddChat(int userId, ChatDTO chat)
        {
            Logger.log.Debug("at ChatController.AddChat");
            int createdChatId;

            if (!this.CheckUserIdAcces(userId))
            {
                Logger.log.Error("at ChatController.AddChat - User request denied");
                return(new Forbidden("User cannot add chat to other people!"));
            }

            using (var client = SoapProvider.GetChatServiceClient())
            {
                createdChatId = client.AddChat(chat);
                if (createdChatId == -1)
                {
                    Logger.log.Error("at ChatController.AddChat - Error at database");
                    return(InternalServerError());
                }

                if (!client.AddUser(createdChatId, userId))
                {
                    Logger.log.Error("at ChatController.AddChat - Error at database");
                    return(InternalServerError());
                }
            }

            return(Ok(createdChatId));
        }
        public IHttpActionResult GetChatById(int userId, string chat)
        {
            Logger.log.Debug("at ChatController.GetChatById");
            ChatDTO chatDTO = null;

            if (!this.CheckUserIdAcces(userId))
            {
                Logger.log.Error("at ChatController.GetChatById - User request denied");
                return(new Forbidden("User cannot have acces to other chat!"));
            }

            int chatId = this.CreateChatIdFromProxy(chat);

            using (var client = SoapProvider.GetChatServiceClient())
            {
                if (!this.IfUserHasAccesToChat(client, chatId, userId))
                {
                    Logger.log.Error("at ChatController.GetChatById - User request denied");
                    return(new Forbidden("User cannot have acces to other chat!"));
                }

                chatDTO = client.GetChatById(chatId);
            }

            if (chatDTO == null)
            {
                Logger.log.Error("at ChatController.GetChatById - Error at database");
                return(InternalServerError());
            }

            return(Ok(chatDTO));
        }
        public IHttpActionResult AddMessage(int userId, string chat, MessageDTO message)
        {
            Logger.log.Debug("at ChatController.AddMessage");
            int createdMessageId;

            if (!this.CheckUserIdAcces(userId))
            {
                Logger.log.Error("at ChatController.AddMessage - User request denied");
                return(new Forbidden("User cannot send message from other accounts!"));
            }

            if (userId != message.UserId)
            {
                Logger.log.Error("at ChatController.AddMessage - User request denied");
                return(new Forbidden("User cannot send message from other accounts!"));
            }

            int chatId = this.CreateChatIdFromProxy(chat);

            using (var client = SoapProvider.GetChatServiceClient())
            {
                if (!this.IfUserHasAccesToChat(client, chatId, userId))
                {
                    Logger.log.Error("at ChatController.GetChatById - User request denied");
                    return(new Forbidden("User cannot have acces to other chat!"));
                }

                createdMessageId = client.AddMessage(chatId, message);
                if (createdMessageId == -1)
                {
                    Logger.log.Error("at ChatController.AddMessage - Error at database");
                    return(InternalServerError());
                }

                if (chat == "bot")
                {
                    using (var botClient = SoapProvider.GetBotServiceClient())
                    {
                        var messageToBot = new BotServiceReference.MessageDTO()
                        {
                            NewContent = message.NewContent,
                            UserId     = message.UserId
                        };
                        botClient.SendMessageToBot(messageToBot, chatId);
                    }
                }
            }

            return(Ok(createdMessageId));
        }
        public IHttpActionResult AddUserToChat(int userId, string chat, int newUserId)
        {
            Logger.log.Debug("at ChatController.AddUserToChat");

            if (userId == newUserId)
            {
                Logger.log.Error("at ChatController.AddUserToChat - User cannot add himself to chat");
                return(BadRequest("User cannot add himself to chat"));
            }

            if (!this.CheckUserIdAcces(userId))
            {
                Logger.log.Error("at ChatController.AddUserToChat - User request denied");
                return(new Forbidden("User has no acces!"));
            }

            //if (userdId == SoapProvider.GetBotServiceClient())

            int chatId = 0;

            if (!int.TryParse(chat, out chatId))
            {
                return(BadRequest("Bad chat id, or cannot add user to chat with a bot"));
            }

            using (var client = SoapProvider.GetChatServiceClient())
            {
                if (!this.IfUserHasAccesToChat(client, chatId, userId))
                {
                    Logger.log.Error("at ChatController.GetChatById - User request denied");
                    return(new Forbidden("User cannot have acces to other chat!"));
                }

                if (!client.AddUser(chatId, newUserId))
                {
                    Logger.log.Error("at ChatController.AddUserToChat - Error at database");
                    return(InternalServerError());
                }
            }

            return(Ok());
        }