User Query Documentation
This code provides functions for querying and manipulating user data in the database. It uses the PocketBase DAO for database interactions and includes functions for retrieving, inserting, and updating user records.
Functions
GetUserProfileByParams
func GetUserProfileByParams(dao *daos.Dao, params map[string]interface{}) (*entity.UserProfileTable, error)
Retrieves a user profile by the given parameters.
dao: The PocketBase DAO.params: A map of parameters to filter the user profiles.- Returns: A pointer to a
UserProfileTablestruct containing the user profile data, or an error if the operation fails.
GetUsersByPins
func GetUsersByPins(dao *daos.Dao, companyID string, pins []string) ([]*entity.UserProfileTable, error)
Retrieves user profiles by their PINs for a specific company.
dao: The PocketBase DAO.companyID: The ID of the company.pins: A slice of user PINs.- Returns: A slice of pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUsersByPinsMapPins
func GetUsersByPinsMapPins(dao *daos.Dao, companyID string, pins []string) (map[string]*entity.UserProfileTable, error)
Retrieves user profiles by their PINs for a specific company and returns them as a map.
dao: The PocketBase DAO.companyID: The ID of the company.pins: A slice of user PINs.- Returns: A map where the keys are user PINs and the values are pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUsersByExp
func GetUsersByExp(dao *daos.Dao, exp []dbx.Expression) ([]*entity.UserProfileTable, error)
Retrieves user profiles by a list of expressions.
dao: The PocketBase DAO.exp: A slice ofdbx.Expressionto filter the user profiles.- Returns: A slice of pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUsersByExpMapUserProfileID
func GetUsersByExpMapUserProfileID(dao *daos.Dao, exp []dbx.Expression) (map[string]*entity.UserProfileTable, error)
Retrieves user profiles by a list of expressions and returns them as a map.
dao: The PocketBase DAO.exp: A slice ofdbx.Expressionto filter the user profiles.- Returns: A map where the keys are user profile IDs and the values are pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetViewUserProfileByParams
func GetViewUserProfileByParams(dao *daos.Dao, params map[string]interface{}) (*entity.UserProfileTable, error)
Retrieves a user profile using a view table based on the provided parameters.
dao: The PocketBase DAO.params: A map of parameters to filter the user profiles.- Returns: A pointer to a
UserProfileTablestruct containing the user profile data, or an error if the operation fails.
GetViewUserProfileByID
func GetViewUserProfileByID(dao *daos.Dao, ID string) (*entity.UserProfileTable, error)
Retrieves a user profile by its ID using a view table.
dao: The PocketBase DAO.ID: The ID of the user profile.- Returns: A pointer to a
UserProfileTablestruct containing the user profile data, or an error if the operation fails.
GetListViewUserProfileByParams
func GetListViewUserProfileByParams(dao *daos.Dao, params map[string]interface{}) ([]*entity.UserProfileTable, error)
Retrieves a list of user profiles using a view table based on the provided parameters.
dao: The PocketBase DAO.params: A map of parameters to filter the user profiles.- Returns: A slice of pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUserProfileByDepartmentID
func GetUserProfileByDepartmentID(dao *daos.Dao, departmentID string) ([]*entity.UserProfileTable, error)
Retrieves user profiles by department ID.
dao: The PocketBase DAO.departmentID: The ID of the department.- Returns: A slice of pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUserProfileByCompanyID
func GetUserProfileByCompanyID(dao *daos.Dao, companyID string) ([]*entity.UserProfileTable, error)
Retrieves user profiles by company ID.
dao: The PocketBase DAO.companyID: The ID of the company.- Returns: A slice of pointers to
UserProfileTablestructs containing the user profile data, or an error if the operation fails.
GetUserEmployment
func GetUserEmployment(dao *daos.Dao, userProfileID string) (*entity.UserEmploymentTable, error)
Retrieves the employment details of a user by their user profile ID.
dao: The PocketBase DAO.userProfileID: The ID of the user profile.- Returns: A pointer to a
UserEmploymentTablestruct containing the employment data, or an error if the operation fails.
InsertUserProfile
func InsertUserProfile(app core.App, dao *daos.Dao, req *entity.UserProfileTable) (*models.Record, error)
Inserts a new user profile into the database.
app: The PocketBase app instance.dao: The PocketBase DAO.req: A pointer to aUserProfileTablestruct containing the user profile data to be inserted.- Returns: A pointer to a
models.Recordcontaining the inserted record, or an error if the operation fails.
UpdateUserProfileGovID
func UpdateUserProfileGovID(app core.App, dao *daos.Dao, userProfile entity.UserProfileTable, govIDFileBase64 string) (*models.Record, error)
Updates the government ID of a user profile.
app: The PocketBase app instance.dao: The PocketBase DAO.userProfile: The user profile to be updated.govIDFileBase64: The base64-encoded government ID file.- Returns: A pointer to a
models.Recordcontaining the updated record, or an error if the operation fails.
InsertUserAddress
func InsertUserAddress(dao *daos.Dao, userProfileID string, req *entity.AddressTable) (*models.Record, error)
Inserts a new address for a user profile.
dao: The PocketBase DAO.userProfileID: The ID of the user profile.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.
InsertUserPrimaryBankAccount
func InsertUserPrimaryBankAccount(dao *daos.Dao, req *entity.UserBankAccountTable) (*models.Record, error)
Inserts a new primary bank account for a user profile.
dao: The PocketBase DAO.req: A pointer to aUserBankAccountTablestruct containing the bank account data to be inserted.- Returns: A pointer to a
models.Recordcontaining the inserted record, or an error if the operation fails.
UpdateUserProfile
func UpdateUserProfile(app core.App, dao *daos.Dao, req *entity.UserProfileTable) (*models.Record, error)
Updates an existing user profile.
app: The PocketBase app instance.dao: The PocketBase DAO.req: A pointer to aUserProfileTablestruct containing the updated user profile data.- Returns: A pointer to a
models.Recordcontaining the updated record, or an error if the operation fails.
RestoreUserProfile
func RestoreUserProfile(dao *daos.Dao, user *entity.UserProfileTable) (*models.Record, error)
Restores a deleted user profile.
dao: The PocketBase DAO.user: The user profile to be restored.- Returns: A pointer to a
models.Recordcontaining the restored record, or an error if the operation fails.
UpdateUserHomeAddress
func UpdateUserHomeAddress(dao *daos.Dao, userProfileID string, req *entity.AddressTable) (*models.Record, error)
Updates the home address of a user profile.
dao: The PocketBase DAO.userProfileID: The ID of the user profile.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.
UpdateUserPrimaryBankAccount
func UpdateUserPrimaryBankAccount(dao *daos.Dao, req *entity.UserBankAccountTable) (*models.Record, error)
Updates the primary bank account of a user profile.
dao: The PocketBase DAO.req: A pointer to aUserBankAccountTablestruct containing the updated bank account data.- Returns: A pointer to a
models.Recordcontaining the updated record, or an error if the operation fails.
GetUserByIds
func GetUserByIds(dao *daos.Dao, ids []string) ([]models.Record, error)
Retrieves user profiles by their IDs.
dao: The PocketBase DAO.ids: A slice of user profile IDs.- Returns: A slice of
models.Recordcontaining the user profile data, 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 Profile by Parameters
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 a user profile by parameters
params := map[string]interface{}{
"email": "john.doe@example.com",
}
userProfile, err := queries.GetUserProfileByParams(dao, params)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("User Profile:", userProfile)
}
Inserting a New User Profile
package main
import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/core"
"fmt"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
// Create a new user profile
newUserProfile := &entity.UserProfileTable{
CompanyID: "company123",
UserID: "user123",
FullName: "John Doe",
Email: "john.doe@example.com",
MobilePhone: "1234567890",
Gender: entity.MALE,
BirthDate: "1990-01-01",
CustomID: "emp123",
}
// Insert the new user profile
record, err := queries.InsertUserProfile(pb, dao, newUserProfile)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Inserted User Profile Record:", record)
}
Updating a User Profile
package main
import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/core"
"fmt"
)
func main() {
// Initialize PocketBase
pb := pocketbase.New()
// Initialize the DAO
dao := daos.New(pb.DB())
// Retrieve the user profile to update
userProfileID := "user123"
userProfile, err := queries.GetViewUserProfileByID(dao, userProfileID)
if err != nil {
fmt.Println("Error:", err)
return
}
// Update the user profile fields
userProfile.FullName = "John Doe Updated"
userProfile.MobilePhone = "0987654321"
// Update the user profile in the database
updatedRecord, err := queries.UpdateUserProfile(pb, dao, userProfile)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Updated User Profile Record:", updatedRecord)
}
Updating a User's Home 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,
}
// Update the user's home address
userProfileID := "user123"
updatedRecord, err := queries.UpdateUserHomeAddress(dao, userProfileID, newAddress)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Updated Home Address Record:", updatedRecord)
}
This documentation provides an overview of the user query functions, their parameters, and example usage. By following the examples, you can retrieve, insert, and update user records in the database.