public IHttpActionResult PostMessagesToChannel(string channelName, ChannelMessagesBindingModel m)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (string.IsNullOrEmpty(channelName))
                return BadRequest();

            if (!Data.Channels.Any(c => c.Name == channelName))
                return NotFound();

            var channelId = Data.Channels.First(c => c.Name == channelName).Id;

            var message = new ChannelMessage
            {
                Text = m.Text,
                DateSent = DateTime.Now,
                ChannelId = channelId
            };

            Data.ChannelMessages.Add(message);

            Data.SaveChanges();

            return Ok(new
            {
                message.Id,
                Message = "Anonymous message sent to channel " + channelName + "."
            });
        }
        public IHttpActionResult PostAnonymusMessage([FromUri] string channel, [FromBody] AnonymousMessageBindingModel message)
        {
            if (channel == null)
            {
                return BadRequest();
            }
            Channel channelSearch = this.Data.Channels.FirstOrDefault(c => c.Name == channel);

            if (channelSearch == null)
            {
                return NotFound();
            }
            var currentUserId = User.Identity.GetUserId();
            ChannelMessage newMessage = new ChannelMessage()
            {
                Text = message.Text,
                ChannelId = channelSearch.Id,
                DateSend = DateTime.Now,
                SenderId = null

            };
            this.Data.ChannelMessages.Add(newMessage);
            this.Data.SaveChanges();
            var viewMessage = new AnonymousChannelMessageViewModel()
            {
                Id = newMessage.Id,
                Text = newMessage.Text
            };
            if (currentUserId != null)
            {
                return this.Ok(new
                {
                    Id=viewMessage.Id,
                    Sender=User.Identity.GetUserName(),
                    Message="Message sent to channel " + newMessage.Channel.Name
                });
            }
            return Ok(viewMessage);
        }
        public IHttpActionResult SendAnonymousMessage(string channelName, ChannelMessageBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var existingChannel = this.Data.Channels.All()
                .FirstOrDefault(c => c.Name.Equals(channelName));
            if (existingChannel == null)
            {
                return this.NotFound();
            }

            var newMessage = new ChannelMessage
            {
                Text = model.Text,
                DateSent = DateTime.Now,
                Channel = existingChannel,
                ChannelId = existingChannel.Id
            };

            this.Data.ChannelMessages.Add(newMessage);
            existingChannel.Messages.Add(newMessage);

            this.Data.Channels.Update(existingChannel);
            this.Data.SaveChanges();

            var responseMessage = string.Format("Anonymous message sent to channel {0}.", existingChannel.Name);

            return this.Ok(new
            {
                newMessage.Id,
                Message = responseMessage
            });
        }
        public IHttpActionResult PostChannelMessages(string channelName, ChannelMessagesBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Name is required!Cannot be empty.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var channel = this.Data.Channels
                .FirstOrDefault(ch => ch.Name == channelName);

            if (channel == null)
            {
                return this.NotFound();
            }

            var message = new ChannelMessage
            {
                Text = model.Text,
                DateSent = DateTime.Now,
                ChannelId = channel.Id
            };

            if (this.User.Identity.IsAuthenticated)
            {
                message.SenderId = this.User.Identity.GetUserId();

            }

            this.Data.ChannelMessages.Add(message);
            this.Data.SaveChanges();
            
            if (this.User.Identity.IsAuthenticated)
            {
                return this.Ok(new
                {
                    Id = message.Id,
                    Sender = this.User.Identity.GetUserName(),
                    Message = "Message sent to channel " + channel.Name
                });

            }

            return this.Ok(new
            {
                Id = message.Id,
                Message = "Anonymous message sent to channel " + channel.Name
            });
        }
        public IHttpActionResult SendChannelMessage(
            string channelName, ChannelMessageBindingModel channelMessageData)
        {
            if (channelMessageData == null)
            {
                return BadRequest("Missing message data.");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var channel = db.Channels.All().FirstOrDefault(c => c.Name == channelName);
            if (channel == null)
            {
                return this.NotFound();
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser = this.db.Users.Find(currentUserId);

            var message = new ChannelMessage()
            {
                Text = channelMessageData.Text,
                Channel = channel,
                DateSent = DateTime.Now,
                SenderUser = currentUser
            };
            db.ChannelMessages.Add(message);
            db.SaveChanges();

            if (message.SenderUser == null)
            {
                return this.Ok(
                    new
                    {
                        message.Id,
                        Message = "Anonymous message sent successfully to channel " + channelName + "."
                    }
                );
            }

            return this.Ok(
                new
                {
                    message.Id,
                    Sender = message.SenderUser.UserName,
                    Message = "Message sent successfully to channel " + channelName + "."
                }
            );
        }