Example #1
0
        public IHttpActionResult CreateChannel(ChannelBindingModel channelData)
        {
            if (channelData == null)
            {
                return(BadRequest("Missing channel data."));
            }

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

            if (db.Channels.All().Any(c => c.Name == channelData.Name))
            {
                return(this.Content(HttpStatusCode.Conflict,
                                    new { Message = "Duplicated channel name: " + channelData.Name }));
            }

            var channel = new Channel()
            {
                Name = channelData.Name
            };

            db.Channels.Add(channel);
            db.SaveChanges();

            return(this.CreatedAtRoute(
                       "DefaultApi",
                       new { controller = "channels", id = channel.Id },
                       new { channel.Id, channel.Name }));
        }
Example #2
0
        public IHttpActionResult SendPersonalMessage(UserMessageBindingModel messageData)
        {
            if (messageData == null)
            {
                return(BadRequest("Missing message data."));
            }

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

            var recipientUser = this.db.Users.All()
                                .FirstOrDefault(u => u.UserName == messageData.Recipient);

            if (recipientUser == null)
            {
                return(BadRequest("Recipient user " + messageData.Recipient + " does not exists."));
            }

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

            var message = new UserMessage()
            {
                Text          = messageData.Text,
                DateSent      = DateTime.Now,
                SenderUser    = currentUser,
                RecipientUser = recipientUser
            };

            db.UserMessages.Add(message);
            db.SaveChanges();

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

            return(this.Ok(
                       new
            {
                message.Id,
                Sender = message.SenderUser.UserName,
                Message = "Message sent successfully to user " + recipientUser.UserName + "."
            }
                       ));
        }
Example #3
0
        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 + "."
            }
                       ));
        }