示例#1
0
        public async Task <IHttpActionResult> Add(MessageCreateVM messageCreateVM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByIdAsync(User.Identity.GetUserId());

            var message = new Message
            {
                Body       = messageCreateVM.Body,
                CreatedUtc = DateTime.UtcNow,
                UserId     = user.Id,
                User       = user
            };

            _messagesManager.Create(message);

            var viewModel = new MessageFullVM
            {
                Id         = message.Id,
                Body       = message.Body,
                CreatedUtc = message.CreatedUtc,
                User       = new UserVM
                {
                    Id       = message.User.Id,
                    UserName = message.User.UserName
                }
            };

            return(Ok(viewModel));
        }
示例#2
0
        public ActionResult Create(MessageCreateVM viewModel)
        {
            if (ModelState.IsValid)
            {
                var message = new Message
                {
                    Body   = viewModel.Body,
                    UserId = User.Identity.GetUserId()
                };

                _messagesManager.Create(message);
                return(RedirectToAction("List"));
            }

            return(View(viewModel));
        }