Ejemplo n.º 1
0
        public async Task <JsonResult> SubmitPost(PostViewModel model)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return(Json(new { Message = "You must be signed in to do that" }));
            }
            try
            {
                if (ModelState.IsValid)
                {
                    var user = await _userManager.GetUserAsync(User);

                    Post post = new Post()
                    {
                        Message  = model.Message,
                        User     = user,
                        Author   = user.UserName,
                        PostDate = DateTime.Now
                    };
                    _context.Posts.Add(post);
                    await _context.SaveChangesAsync();

                    Response.StatusCode = (int)HttpStatusCode.Created;
                    return(Json(new { Message = "Success!" }));
                }
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = "An error occurred when submitting your bark." }));
            }
            catch (Exception e)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = e.Message }));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> ChangeBio(ChangeBioViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            try
            {
                user.Bio = model.Bio;
                _context.Users.Update(user);
                await _context.SaveChangesAsync();

                TempData["Status"] = "Your bio has been changed.";
                return(RedirectToAction(nameof(ChangeBio)));
            }
            catch (Exception) {
                TempData["Status"] = "An error occurred when updating your bio.";
                return(View(model));
            }
        }