public bool Save(User entity) { bool saved = false; Check.IsNotNull(entity, "user shouldn't be null"); _logger.Info("Start saving user"); entity.ModificationDate = TimeProvider.Now(); try { // get next id int id = GetNewId(); if (id < 0) { _logger.Info("Id cannot be generated to save new user : Failure"); return(false); } using (var con = new DatabaseConnection(DatabaseType.PostgreSql, GetConnectionString())) { using (var cmd = con.CreateCommand()) { cmd.CommandType = CommandType.Text; // Password handling string salt; var hashPassword = PasswordHasher.HashPassword(entity.Password, out salt); // reset password entity.Password = string.Empty; // query cmd.CommandText = Insert; // id cmd.AddIntParameter(":pIDT", id); cmd.AddStringParameter(":pMEL", entity.Mail); cmd.AddStringParameter(":pPWD", hashPassword); cmd.AddStringParameter(":pUSRSLT", salt); cmd.AddIntParameter(":pAGE", entity.Age); cmd.AddStringParameter(":pPSD", entity.Pseudo); cmd.AddStringParameter(":pPHNNBR", entity.PhoneNumber); cmd.AddStringParameter(":pTYP", ModelEnumConverter.UserTypeToString(entity.Type)); cmd.AddStringParameter(":pDSC", entity.Description); cmd.AddStringParameter(":pRLE", ModelEnumConverter.RoleToString(entity.Role)); cmd.AddDateParameter(":pCREDAT", entity.CreationDate); cmd.AddDoubleParameter(":pUSRNOT", entity.Note); cmd.AddDateTimeParameter(":pDATEFT", entity.ModificationDate); saved = cmd.ExecuteNonQuery() > 0; _logger.Info("End saving user : "******"Success" : "Failure")); if (saved) { entity.Id = id; } } } } catch (Exception ex) { _logger.Error("Error while saving new user with query : " + Insert, ex); throw new ImportExportException("Error occured during database access " + ex.Message, ex); } return(saved); }
public bool Update(User entity) { Check.IsNotNull(entity, "User shouldn't be null"); bool updated = false; _logger.Info("Start update user"); entity.ModificationDate = TimeProvider.Now(); try { using (var con = new DatabaseConnection(DatabaseType.PostgreSql, GetConnectionString())) { using (var cmd = con.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = UpdateQuery; cmd.AddIntParameter(":pIDT", entity.Id); cmd.AddStringParameter(":pMEL", entity.Mail); cmd.AddIntParameter(":pAGE", entity.Age); cmd.AddStringParameter(":pPSD", entity.Pseudo); cmd.AddStringParameter(":pPHNNBR", entity.PhoneNumber); cmd.AddStringParameter(":pDSC", entity.Description); cmd.AddStringParameter(":pTYP", ModelEnumConverter.UserTypeToString(entity.Type)); cmd.AddStringParameter(":pRLE", ModelEnumConverter.RoleToString(entity.Role)); cmd.AddDateParameter(":pCREDAT", entity.CreationDate); cmd.AddDoubleParameter(":pUSRNOT", entity.Note); cmd.AddDateTimeParameter(":pDATEFT", entity.ModificationDate); updated = cmd.ExecuteNonQuery() > 0; _logger.Info("End update user : "******"Success" : "Failure")); } } } catch (Exception ex) { _logger.Error("Error while updating user with query : " + UpdateQuery, ex); throw new ImportExportException("Error occured during database access " + ex.Message, ex); } return(updated); }
public void RoleEnumToString_ShouldReturnCorrectStringValue(Role role, string expected) { Assert.AreEqual(expected, ModelEnumConverter.RoleToString(role)); }