Address Query Documentation
This code provides functions for querying and manipulating address data in the database. It uses the PocketBase DAO for database interactions and includes functions for retrieving, inserting, and updating address records.
Functions
GetUserHomeAddress
func GetUserHomeAddress(dao *daos.Dao, userProfileID string) (*entity.AddressTable, error)
Retrieves the home address of a user based on their user profile ID.
dao: The PocketBase DAO.userProfileID: The ID of the user profile.- Returns: A pointer to an
AddressTablestruct containing the address data, or an error if the operation fails.
InsertAddress
func InsertAddress(dao *daos.Dao, req *entity.AddressTable) (*models.Record, error)
Inserts a new address record into the database.
dao: The PocketBase DAO.req: A pointer to anAddressTablestruct containing the address data to be inserted.- Returns: A pointer to a
models.Recordcontaining the inserted record, or an error if the operation fails.
UpdateAddress
func UpdateAddress(dao *daos.Dao, req *entity.AddressTable) (*models.Record, error)
Updates an existing address record in the database.
dao: The PocketBase DAO.req: A pointer to anAddressTablestruct containing the updated address data.- Returns: A pointer to a
models.Recordcontaining the updated record, or an error if the operation fails.
Example Usage
Initializing the DAO
Before using the functions, you need to initialize the PocketBase DAO.
package main
import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
}
Retrieving a User's Home Address
package main
import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
// Retrieve the home address of a user
userProfileID := "user123"
address, err := queries.GetUserHomeAddress(dao, userProfileID)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("User Home Address:", address)
}
Inserting a New Address
package main
import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
// Create a new address
newAddress := &entity.AddressTable{
Label: "Home",
Address: "123 Main St",
CityID: "city123",
ProvinceID: "province123",
CountryID: "country123",
PostalCode: "12345",
Latitude: 37.7749,
Longitude: -122.4194,
DistanceTolerance: 100.0,
OwnerUserProfileID: "user123",
CreatedByID: "admin123",
}
// Insert the new address
record, err := queries.InsertAddress(dao, newAddress)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Inserted Address Record:", record)
}
Updating an Existing Address
package main
import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
// Retrieve the address to update
addressID := "address123"
address, err := queries.GetUserHomeAddress(dao, addressID)
if err != nil {
fmt.Println("Error:", err)
return
}
// Update the address fields
address.Address = "456 Elm St"
address.PostalCode = "67890"
// Update the address in the database
updatedRecord, err := queries.UpdateAddress(dao, address)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Updated Address Record:", updatedRecord)
}
This documentation provides an overview of the address query functions, their parameters, and example usage. By following the examples, you can retrieve, insert, and update address records in the database.