Пример #1
0
        public IHttpActionResult PostAnonymoustMessage([FromUri] string channelName, [FromBody] ChannelMessageBindingModel model)
        {
            if (string.IsNullOrEmpty(channelName) || model == null || !this.ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            var dbChannel = this.Data.Channels.FirstOrDefault(c => c.Name == channelName);

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

            if (this.User.Identity.IsAuthenticated)
            {
                var userId  = this.User.Identity.GetUserId();
                var dbUser  = this.Data.Users.First(u => u.Id == userId);
                var message = new ChannelMessage()
                {
                    DateSent = DateTime.Now,
                    Sender   = dbUser,
                    Text     = model.Text
                };
                dbChannel.ChannelMessages.Add(message);
                this.Data.SaveChanges();

                var viewModel = new ChannelMessageCreateWithUserViewModel()
                {
                    Id      = message.Id,
                    Sender  = message.Sender.UserName,
                    Message = "Message sent to channel " + dbChannel.Name + "."
                };
                return(this.Ok(viewModel));
            }
            else
            {
                var message = new ChannelMessage()
                {
                    DateSent = DateTime.Now,
                    Sender   = null,
                    Text     = model.Text
                };
                dbChannel.ChannelMessages.Add(message);
                this.Data.SaveChanges();

                var viewModel = new ChannelMessageCreateViewModel()
                {
                    Id      = message.Id,
                    Message = "Anonymous message sent to channel " + dbChannel.Name + "."
                };
                return(this.Ok(viewModel));
            }
        }
        public IHttpActionResult PostPersonalMessage([FromBody] PersonalMessageBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

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

            var recipient = this.Data.Users.FirstOrDefault(u => u.UserName == model.Recipient);

            if (recipient == null)
            {
                return(this.BadRequest());
            }

            if (this.User.Identity.IsAuthenticated)
            {
                var userId  = this.User.Identity.GetUserId();
                var sender  = this.Data.Users.First(u => u.Id == userId);
                var message = new UserMessage()
                {
                    Sender    = sender,
                    Recipient = recipient,
                    Text      = model.Text,
                    DateSent  = DateTime.Now
                };
                this.Data.UserMessages.Add(message);
                this.Data.SaveChanges();

                var viewModel = new ChannelMessageCreateWithUserViewModel()
                {
                    Id      = message.Id,
                    Sender  = sender.UserName,
                    Message = "Message sent successfully to user " + recipient.UserName + "."
                };
                return(this.Ok(viewModel));
            }
            else
            {
                var message = new UserMessage()
                {
                    Sender    = null,
                    Recipient = recipient,
                    Text      = model.Text,
                    DateSent  = DateTime.Now
                };
                this.Data.UserMessages.Add(message);
                this.Data.SaveChanges();

                var viewModel = new ChannelMessageCreateViewModel()
                {
                    Id      = message.Id,
                    Message = "Anonymous message sent successfully to user " + recipient.UserName + "."
                };
                return(this.Ok(viewModel));
            }
        }