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);
        }
Example #2
0
        public bool Save(Pot entity)
        {
            Check.IsNotNull(entity, "Pot should be provided");

            var saved = false;

            entity.ModificationDate = TimeProvider.Now();
            _logger.Info("Start saving pot");

            try
            {
                // id
                var id = GetNewId();

                if (id <= 0)
                {
                    _logger.Info("Unable to retrieve new id to save a pot");
                    return(false);
                }
                using (var con = new DatabaseConnection(DatabaseType.PostgreSql, GetConnectionString()))
                {
                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = InsertQuery;
                        cmd.AddIntParameter(":pIDT", id);
                        cmd.AddStringParameter(":pORG", entity.Organizer);
                        cmd.AddStringParameter(":pPOTMOD", ModelEnumConverter.PotModeToString(entity.Mode));
                        cmd.AddDoubleParameter(":pCURMNT", entity.CurrentAmount);
                        cmd.AddDoubleParameter(":pTGTMNT", entity.TargetAmount);
                        cmd.AddIntParameter(":pTRPIDT", entity.TripId);
                        cmd.AddStringParameter(":pNAM", entity.Name);
                        cmd.AddDateParameter(":pSTRDAT", entity.StartDate);
                        cmd.AddDateParameter(":pENDDAT", entity.EndDate);
                        cmd.AddDateParameter(":pVALDAT", entity.ValidityDate);
                        cmd.AddStringParameter(":pDSC", entity.Description);
                        cmd.AddStringParameter(":pINDCANCEL", ConverterHelper.BoolToYesNoString(entity.IsCancelled));
                        cmd.AddStringParameter(":pCANCELRSN", entity.CancellationReason);
                        cmd.AddDateParameter(":pCANCELDAT", entity.CancellationDate);
                        cmd.AddDateTimeParameter(":pDATEFT", entity.ModificationDate);
                        saved = cmd.ExecuteNonQuery() > 0;
                        _logger.Info("End saving pot " + (saved ? "Success" : "Error"));
                        if (saved)
                        {
                            entity.Id = id;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error while trying to save pot with query : " + InsertQuery, ex);
                throw new ImportExportException("Error occured during database access " + ex.Message, ex);
            }

            return(saved);
        }
Example #3
0
        public bool Update(Pot entity)
        {
            var updated = false;

            Check.IsNotNull(entity, "Pot must provided for update");
            entity.ModificationDate = TimeProvider.Now();
            _logger.Info("Start updating pot");
            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(":pORG", entity.Organizer);
                        cmd.AddStringParameter(":pPOTMOD", ModelEnumConverter.PotModeToString(entity.Mode));
                        cmd.AddDoubleParameter(":pCURMNT", entity.CurrentAmount);
                        cmd.AddDoubleParameter(":pTGTMNT", entity.TargetAmount);
                        cmd.AddStringParameter(":pNAM", entity.Name);
                        cmd.AddDateParameter(":pSTRDAT", entity.StartDate);
                        cmd.AddDateParameter(":pENDDAT", entity.EndDate);
                        cmd.AddDateParameter(":pVALDAT", entity.ValidityDate);
                        cmd.AddStringParameter(":pDSC", entity.Description);
                        cmd.AddStringParameter(":pINDCANCEL", ConverterHelper.BoolToYesNoString(entity.IsCancelled));
                        cmd.AddStringParameter(":pCANCELRSN", entity.CancellationReason);
                        cmd.AddDateParameter(":pCANCELDAT", entity.CancellationDate);
                        cmd.AddDateTimeParameter(":pDATEFT", entity.ModificationDate);
                        updated = cmd.ExecuteNonQuery() > 0;
                        _logger.Info("End updating pot : " + (updated ? "Success" : "Error"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error while trying to update pot with query : " + UpdateQuery, ex);
                throw new ImportExportException("Error occured during database access " + ex.Message, ex);
            }

            return(updated);
        }
        protected override User CreateValueFromReader(IDatabaseReader reader)
        {
            var user = new User
                       (
                reader.GetInt("IDT"),
                reader.GetString("MEL"),
                reader.GetString("PWD"),
                reader.GetString("PSD"),
                reader.GetInt("AGE"),
                reader.GetString("DSC"),
                ModelEnumConverter.RoleFromString(reader.GetString("RLE")),
                reader.GetDate("CREDAT"),
                reader.GetString("PHNNBR"),
                ModelEnumConverter.UserTypeFromString(reader.GetString("TYP")),
                reader.GetDouble("USRNOT")
                       );

            user.ModificationDate = reader.GetDate("DATEFT");
            return(user);
        }
        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);
        }
Example #6
0
        protected override Pot CreateValueFromReader(IDatabaseReader reader)
        {
            var pot = new  Pot
                      (
                reader.GetInt("IDT"),
                reader.GetInt("TRPIDT"),
                reader.GetString("ORG"),
                ModelEnumConverter.PotModeFromString(reader.GetString("POTMOD")),
                reader.GetDouble("CURMNT"),
                reader.GetDouble("TGTMNT"),
                reader.GetString("NAM"),
                reader.GetDate("STRDAT"),
                reader.GetDate("ENDDAT"),
                reader.GetDate("VALDAT"),
                reader.GetString("DSC"),
                ConverterHelper.YesNoStringToBool(reader.GetString("INDCANCEL")),
                reader.GetString("CANCELRSN"),
                reader.GetNullableDate("CANCELDAT")
                      );

            pot.ModificationDate = reader.GetDate("DATEFT");
            return(pot);
        }
 public void UserTypeFromString_ShouldReturnCorrectEnumValue(string type, UserType expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.UserTypeFromString(type));
 }
 public void PotModeToString_ShouldReturnCorrectStringValue(PotMode mode, string expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.PotModeToString(mode));
 }
 public void PotModeFromString_ShouldReturnCorrectEnumValue(string mode, PotMode expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.PotModeFromString(mode));
 }
 public void RoleEnumToString_ShouldReturnCorrectStringValue(Role role, string expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.RoleToString(role));
 }
 public void RoleEnumFromString_ShouldReturnCorrectEnumValue(string role, Role expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.RoleFromString(role));
 }
 public void UserTypeToString_ShouldReturnCorrectStringValue(UserType type, string expected)
 {
     Assert.AreEqual(expected, ModelEnumConverter.UserTypeToString(type));
 }