public IActionResult Post([FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingProduct = _context.Products. Where(b => (b.name == product.name) || (b.skuId == product.skuId)). FirstOrDefault(); if (existingProduct != null) { ModelState.AddModelError("name", "Product name or skuId is already taken"); return(BadRequest(ModelState)); } if (!product.imageExists()) { ModelState.AddModelError("name", "Image supplied does not exist"); return(BadRequest(ModelState)); } _context.Products.Add(product); _context.SaveChanges(); return(Ok(product)); }
public IActionResult Post([FromBody] RegistrationRequest registrationRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingUser = _context.Users. Where(b => b.email == registrationRequest.email). FirstOrDefault(); if (existingUser != null) { ModelState.AddModelError("email", "Email address is already taken"); return(BadRequest(ModelState)); } var user = new Models.User(); user.name = registrationRequest.name; user.email = registrationRequest.email; user.role = Models.User.RoleUser; user.createdAt = user.updatedAt = DateTime.Now; user.updatePassword(registrationRequest.password); _context.Users.Add(user); _context.SaveChanges(); return(Ok(user)); }
public IActionResult Put([FromBody] PasswordResetRequest passwordResetRequest) { if (String.IsNullOrEmpty(passwordResetRequest.key)) { ModelState.AddModelError("key", "Key is required for password reset"); return(BadRequest(ModelState)); } if (String.IsNullOrEmpty(passwordResetRequest.password) || String.IsNullOrEmpty(passwordResetRequest.passwordConfirmation)) { ModelState.AddModelError("password", "Password is required"); ModelState.AddModelError("passwordConfirmation", "Password confirmation is required"); return(BadRequest(ModelState)); } if (passwordResetRequest.password != passwordResetRequest.passwordConfirmation) { ModelState.AddModelError("password", "Password must match password confirmation"); return(BadRequest(ModelState)); } var resetRequest = _context.PasswordResetRequests. Where(b => b.key == passwordResetRequest.key).FirstOrDefault(); if (resetRequest == null) { ModelState.AddModelError("key", "Key not found in system"); return(BadRequest(ModelState)); } var existingUser = _context.Users. Where(b => b.email == resetRequest.email). FirstOrDefault(); existingUser.updatePassword(passwordResetRequest.password); _context.Users.Update(existingUser); _context.SaveChanges(); return(Ok("Password updated successfully for userId: " + existingUser.ID.ToString())); }
public IActionResult New(int id, string name, string ip, string redirectOK = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = User.Claims.Where(u => u.Type == "id").Select(u => u.Value).FirstOrDefault(); if (string.IsNullOrEmpty(userId)) { throw new UnauthorizedAccessException(); } var entry = _context.Servers.SingleOrDefault(m => m.ID == id && m.UserId == Convert.ToInt32(userId)); if (entry != null) { entry.Name = name; entry.Ip = ip; } else { entry = new Server() { UserId = Convert.ToInt32(userId), ID = id, Name = name, Ip = ip }; _context.Servers.Add(entry); } _context.SaveChanges(); if (!string.IsNullOrEmpty(redirectOK)) { return(Redirect(redirectOK)); } return(Ok(entry)); }
public IActionResult New([FromForm] UserContentRequest user, string redirectOK = null) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_context.Users.Count(u => u.Email == user.email) > 0) { // Don't allow same email account throw new Exception(); } // TODO: We must copy this user to our backup server at 172.22.22.1 var id = _context.Users.Count() == 0 ? 1 : _context.Users.Max(u => u.ID) + 1; var entry = new User { ID = id, Name = user.name, Email = user.email, Role = user.role }; entry.UpdatePassword(user.password); _context.Users.Add(entry); _context.SaveChanges(); if (!string.IsNullOrEmpty(redirectOK)) { return(Redirect(redirectOK)); } return(Ok(entry)); }
public IActionResult Put(int id, [FromBody] Models.UserUpdateRequest user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingUser = _context.Users.SingleOrDefault(m => m.ID == id); if (existingUser == null) { return(NotFound()); } existingUser.name = user.name; existingUser.email = user.email; existingUser.role = user.role; existingUser.updatePassword(user.password); _context.Users.Update(existingUser); _context.SaveChanges(); return(Ok(existingUser)); }