public ResponseModel <UserModel> Post([FromBody] UserModel value) { var response = new ResponseModel <UserModel>(); try { if (value == null || string.IsNullOrEmpty(value?.Email)) { response.Status = 200; response.Description = "Wrong data..."; return(response); } if (UserRepo.ExistUserByEmail(value.Email)) { response.Status = 200; response.Description = "This user already exist"; return(response); } var pass = value.Password; value.Password = CryptoClass.CreateHash(pass); var user = new User { FirstName = value.FirstName, LastName = value.LastName, Password = value.Password, Email = value.Email, IsActive = value.IsActive }; UserRepo.Insert(user); value.Id = user.Id; value.Password = null; response.Status = 200; response.Description = "OK"; response.Data = value; } catch (Exception ex) { response.Status = 500; response.Description = $"Critical error: {ex.Message}"; response.Data = null; } return(response); }
private static void UsersRecords(DataBaseContext context) { if (context.Users.Any()) { return; } var users = new User { Id = 1, Email = "*****@*****.**", FirstName = "test", LastName = "test", Password = CryptoClass.CreateHash("test"), Date = DateTime.Now, IsActive = true, IsDelete = false }; context.Users.Add(users); context.SaveChanges(); }
public ResponseModel <bool> EditUser([FromBody] UserModel userModel) { var response = new ResponseModel <bool>(); try { if (userModel == null) { response.Status = 200; response.Description = "Wrong data..."; response.Data = false; return(response); } var user = UserRepo.GetById(userModel?.Id ?? 0); if (user == null) { throw new Exception("User not found"); } if (!user.Password.Equals(CryptoClass.CreateHash(userModel.OldPassword))) { throw new Exception("User's password is incorrect"); } if (!string.IsNullOrEmpty(userModel.Password)) { user.Password = CryptoClass.CreateHash(userModel.Password); } user.IsDelete = userModel.IsDelete; user.IsActive = userModel.IsActive; if (userModel.Email != null) { user.Email = userModel.Email; } if (userModel.FirstName != null) { user.FirstName = userModel.FirstName; } if (userModel.LastName != null) { user.LastName = userModel.LastName; } if (UserRepo.Update(user)) { response.Status = 200; response.Description = "OK"; response.Data = true; return(response); } response.Status = 500; response.Description = "Error"; response.Data = false; return(response); } catch (Exception ex) { response.Status = 500; response.Description = $"Critical error: {ex.Message}"; response.Data = false; return(response); } }
private string GenerateToken(GroupInvitations hash) { return(CryptoClass.CreateHash(GetTokenHash(hash))); }