public IActionResult Create([FromBody] CreateUserRequest person) { var user = new User { UserName = person.username, Email = person.email, PasswordHash = PasswordHasher.HashPassword(person.password) }; if (TryValidateModel(user)) { Db.Users.Add(user); Db.SaveChanges(); return(Ok(user.ToViewModel())); } else { return(BadRequest(ModelState)); } }
public IActionResult Create([FromBody] MicropostRequest request) { var email = User.Claims.First(c => c.Type == ClaimTypes.Email).Value; var user = Db.Users.Single(u => u.Email == email); Db.Entry(user).Collection(u => u.Microposts).Load(); var post = new Micropost { Content = request.Content }; TryValidateModel(post); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } user.Microposts.Add(post); Db.SaveChanges(); return(Ok(post)); }