public ActionResult CreateUser([FromBody] JsonElement body) { string json = JsonSerializer.Serialize(body); var userObejct = JsonSerializer.Deserialize <User>(json); var hashedPassword = passwordHash.HashPassword(userObejct.Password, null, false); var newUser = new User() { Email = userObejct.Email, Name = userObejct.Name, Phone = userObejct.Phone, UserRole = "U", Password = hashedPassword, CreateBy = 1, CreateDate = DateTime.Now, Active = 1 }; try { _context.Users.Add(newUser); _context.SaveChanges(); int userId = _context.Users .Where(u => u.Email == userObejct.Email) .Select(u => u.Id) .SingleOrDefault(); var newUserBalance = new UserBalance() { UserId = userId, BalanceAmount = 0, Active = 1, CreateBy = 1, CreateDate = DateTime.Now }; _context.UserBalances.Add(newUserBalance); _context.SaveChanges(); } catch (DbUpdateException ex) { //This either returns a error string, or null if it can’t handle that error if (ex != null) { return(BadRequest(ex.ToString())); //return the error string } throw; //couldn’t handle that error, so rethrow } return(Ok(new { result = "OK" })); }
public ActionResult UpdateUserReguler(int currentId, [FromBody] JsonElement body) { string json = JsonSerializer.Serialize(body); var userObejct = JsonSerializer.Deserialize <User>(json); var hashedPassword = passwordHash.HashPassword(userObejct.Password, null, false); try { var user = _context.Users.First(a => a.Id == currentId); user.Name = userObejct.Name; user.Email = userObejct.Email; user.Phone = userObejct.Phone;; user.Password = hashedPassword; user.UserRole = user.UserRole; user.UpdateBy = currentId; user.UpdateDate = DateTime.Now; _context.SaveChanges(); } catch (DbUpdateException ex) { //This either returns a error string, or null if it can’t handle that error if (ex != null) { return(BadRequest(ex.ToString())); //return the error string } throw; //couldn’t handle that error, so rethrow } return(Ok(new { result = "OK" })); }
public async Task <User> AddAsync(string username, string password) { string id = Guid.NewGuid().ToString(); using (QueryFactory db = _queryFactory.Invoke()) { await db.Query("users").InsertAsync(new { Id = id, Username = username, HashedPassword = _passwordHash.HashPassword(password) }); } return(await GetByIdAsync(id)); }