public async Task <IActionResult> Register(UserSingUpViewModel user) { if (ModelState.IsValid) { if (db.Users.Any(o => o.Login == user.Login.ToLower())) { ModelState.AddModelError("", "This login is taken already please use other one"); } else if (db.Users.Any(o => o.Email == user.Email.ToLower())) { ModelState.AddModelError("", "This EMail is taken already please use other one"); } else { var u = new User { Login = user.Login.ToLower(), Email = user.Email.ToLower(), PasswordHash = passwordHasher.HashPassword(null, user.Password) }; db.Users.Add(u); await db.SaveChangesAsync(); await Authenticate(u); return(RedirectToAction("Products", "Home")); } } return(View(user)); }
public async Task <IActionResult> Post([FromBody] Lamp lamp) { if (lamp is null) { return(BadRequest()); } //Here should be lamp check logic and that's why it should be in Entities types, //not in Validation attributes or MVC controllers... db.Lamps.Add(lamp); await db.SaveChangesAsync(); return(Ok(lamp)); }
[ValidateAntiForgeryToken] //To prevent handling Post requests from the foreign apps public async Task <IActionResult> Create(LampViewModel lamp) { if (!ModelState.IsValid) { return(View(lamp)); //There are some validation errors, reshow form } //Here could be used AutoMapper or something like this... db.Lamps.Add(new Lamp { //lamp.Id is ignored here Cost = lamp.Cost, ImageRef = lamp.ImageRef, LampType = lamp.LampType, Manufacturer = lamp.Manufacturer }); await db.SaveChangesAsync(); //DB/ORM exceptions probably should being handled in a special way... return(RedirectToAction("Products")); }