/// <summary> /// Create Guest /// </summary> public void Create(Guest guest) { // Remove unwanted formatting before storing the Guest guest.RemoveFormattingFromPhoneNumbers(); var sql = new StringBuilder(); sql.Append("INSERT INTO Customer.Guest ( "); sql.Append(" BusinessId, "); sql.Append(" TitleId, "); sql.Append(" Forename, "); sql.Append(" Surname, "); sql.Append(" AddressLine1, "); sql.Append(" AddressLine2, "); sql.Append(" AddressLine3, "); sql.Append(" City, "); sql.Append(" StateProvinceId, "); sql.Append(" PostCode, "); sql.Append(" CountryId, "); sql.Append(" Email, "); sql.Append(" SourceOfEnquiryId, "); sql.Append(" IsProviderAgreementAccepted, "); sql.Append(" IsEviivoAgreementAccepted, "); sql.Append(" IsOtherAgreementAccepted, "); sql.Append(" DefaultCultureCode, "); sql.Append(" Notes, "); sql.Append(" UpdatedByUserId "); sql.Append(") VALUES ("); sql.Append(" @BusinessId, "); sql.Append(" @TitleId, "); sql.Append(" @Forename, "); sql.Append(" @Surname, "); sql.Append(" @AddressLine1, "); sql.Append(" @AddressLine2, "); sql.Append(" @AddressLine3, "); sql.Append(" @City, "); sql.Append(" @StateProvinceId, "); sql.Append(" @PostCode, "); sql.Append(" @CountryId, "); sql.Append(" @Email, "); sql.Append(" @SourceOfEnquiryId, "); sql.Append(" @IsProviderAgreementAccepted, "); sql.Append(" @IsEviivoAgreementAccepted, "); sql.Append(" @IsOtherAgreementAccepted, "); sql.Append(" @DefaultCultureCode, "); sql.Append(" @Notes, "); sql.Append(" @UpdatedByUserId ); "); sql.Append("SET @Id = SCOPE_IDENTITY(); "); // Create the record in the database var parameters = new List<SqlParameter> { DbHelper.CreateParameter(GuestMapper.Parameters.BusinessId, guest.BusinessId), DbHelper.CreateParameter(GuestMapper.Parameters.TitleId, guest.TitleId), DbHelper.CreateParameter(GuestMapper.Parameters.Forename, guest.Forename), DbHelper.CreateParameter(GuestMapper.Parameters.Surname, guest.Surname), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine1, guest.AddressLine1), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine2, guest.AddressLine2), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine3, guest.AddressLine3), DbHelper.CreateParameter(GuestMapper.Parameters.City, guest.City), DbHelper.CreateParameter(GuestMapper.Parameters.StateProvinceId, guest.StateProvinceId), DbHelper.CreateParameter(GuestMapper.Parameters.PostCode, guest.PostCode), DbHelper.CreateParameter(GuestMapper.Parameters.CountryId, guest.CountryId), DbHelper.CreateParameter(GuestMapper.Parameters.Email, guest.Email), DbHelper.CreateParameter(GuestMapper.Parameters.SourceOfEnquiryId, guest.SourceOfEnquiryId), DbHelper.CreateParameter(GuestMapper.Parameters.IsProviderAgreementAccepted, guest.IsProviderAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.IsEviivoAgreementAccepted, guest.IsEviivoAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.IsOtherAgreementAccepted, guest.IsOtherAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.DefaultCultureCode, guest.DefaultCultureCode), DbHelper.CreateParameter(GuestMapper.Parameters.Notes, guest.Notes) }; // Add auditing parameters AuditFieldsHelper.PopulateAuditFields(parameters); SqlParameter outputKey; parameters.Add(outputKey = DbHelper.CreateParameterOut<int>(GuestMapper.Parameters.Id, SqlDbType.Int)); DbHelper.ExecuteNonQueryCommand(sql.ToString(), parameters: parameters); // Make sure the record was created if (outputKey.Value == DBNull.Value) { throw new PrimaryKeyNotSetException(ErrorFactory.CreateAndLogError(Errors.SRVEX30022, "GuestDao.Create", additionalDescriptionParameters: (new object[] { guest.BusinessId }))); } // Set the Guest Id so creation can be validated, and further updates to the object can be done correctly guest.Id = DbHelper.ParameterValue<int>(outputKey); // Create Guest Phones foreach (var guestPhone in guest.GuestPhones) { guestPhoneDao.Create(guestPhone, guest.Id.Value); } }
/// <summary> /// Modify Guest for the mobile api /// </summary> /// <param name="guest">Guest object to update</param> public void Modify(Guest guest) { // Remove unwanted formatting before storing the Guest guest.RemoveFormattingFromPhoneNumbers(); var sbSqlStatement = new StringBuilder(); sbSqlStatement.Append("UPDATE Customer.Guest "); sbSqlStatement.Append("SET "); sbSqlStatement.Append(" TitleId = @TitleId, "); sbSqlStatement.Append(" Forename = @Forename, "); sbSqlStatement.Append(" Surname = @Surname, "); sbSqlStatement.Append(" AddressLine1 = @AddressLine1, "); sbSqlStatement.Append(" AddressLine2 = @AddressLine2, "); sbSqlStatement.Append(" AddressLine3 = @AddressLine3, "); sbSqlStatement.Append(" City = @City, "); sbSqlStatement.Append(" StateProvinceId = @StateProvinceId, "); sbSqlStatement.Append(" PostCode = @PostCode, "); sbSqlStatement.Append(" CountryId = @CountryId, "); sbSqlStatement.Append(" Email = @Email, "); sbSqlStatement.Append(" SourceOfEnquiryId = @SourceOfEnquiryId, "); sbSqlStatement.Append(" IsProviderAgreementAccepted = @IsProviderAgreementAccepted, "); sbSqlStatement.Append(" IsEviivoAgreementAccepted = @IsEviivoAgreementAccepted, "); sbSqlStatement.Append(" IsOtherAgreementAccepted = @IsOtherAgreementAccepted, "); sbSqlStatement.Append(" Notes = @Notes, "); sbSqlStatement.Append(" UpdatedByUserId = @UpdatedByUserId "); sbSqlStatement.Append("WHERE "); sbSqlStatement.Append(" Id = @Id AND "); sbSqlStatement.Append(" BusinessId = @BusinessId; "); // Update the Guest var parameters = new List<SqlParameter> { DbHelper.CreateParameter(GuestMapper.Parameters.Id, guest.Id), DbHelper.CreateParameter(GuestMapper.Parameters.BusinessId, guest.BusinessId), DbHelper.CreateParameter(GuestMapper.Parameters.TitleId, guest.TitleId), DbHelper.CreateParameter(GuestMapper.Parameters.Forename, guest.Forename), DbHelper.CreateParameter(GuestMapper.Parameters.Surname, guest.Surname), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine1, guest.AddressLine1), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine2, guest.AddressLine2), DbHelper.CreateParameter(GuestMapper.Parameters.AddressLine3, guest.AddressLine3), DbHelper.CreateParameter(GuestMapper.Parameters.City, guest.City), DbHelper.CreateParameter(GuestMapper.Parameters.StateProvinceId, guest.StateProvinceId), DbHelper.CreateParameter(GuestMapper.Parameters.PostCode, guest.PostCode), DbHelper.CreateParameter(GuestMapper.Parameters.CountryId, guest.CountryId), DbHelper.CreateParameter(GuestMapper.Parameters.Email, guest.Email), DbHelper.CreateParameter(GuestMapper.Parameters.SourceOfEnquiryId, guest.SourceOfEnquiryId), DbHelper.CreateParameter(GuestMapper.Parameters.IsProviderAgreementAccepted, guest.IsProviderAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.IsEviivoAgreementAccepted, guest.IsEviivoAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.IsOtherAgreementAccepted, guest.IsOtherAgreementAccepted), DbHelper.CreateParameter(GuestMapper.Parameters.Notes, guest.Notes), DbHelper.CreateParameter(GuestPhoneMapper.Parameters.GuestId, guest.Id), }; // Add auditing parameters AuditFieldsHelper.PopulateAuditFields(parameters); int rowsAffected = DbHelper.ExecuteNonQueryCommand(sbSqlStatement.ToString(), parameters: parameters); // Check if the update was successful and return if (rowsAffected == 0) { throw new ExpectedResultException(ErrorFactory.CreateAndLogError(Errors.SRVEX30027, "GuestDao.Modify", additionalDescriptionParameters: (new object[] { guest.GetType().Name, guest.Id }), arguments: new object[] { guest.GetType().Name, guest.Id })); } // Modify Phones : Remove and Insert Phones guestPhoneDao.RemoveAllByGuestId(guest.Id.Value); foreach (var guestPhone in guest.GuestPhones) { guestPhoneDao.Create(guestPhone, guest.Id.Value); } }