Update the GetProfileInfo to get data from gdbm
This commit is contained in:
parent
7a12588744
commit
eb9f470ac5
@ -316,7 +316,7 @@ func (h *Handlers) SaveFamilyname(ctx context.Context, sym string, input []byte)
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return res,fmt.Errorf("a family name cannot be less than one character")
|
return res, fmt.Errorf("a family name cannot be less than one character")
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -886,62 +886,55 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
|||||||
// GetProfileInfo retrieves and formats the profile information of a user from a Gdbm backed storage.
|
// GetProfileInfo retrieves and formats the profile information of a user from a Gdbm backed storage.
|
||||||
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
|
if !ok {
|
||||||
|
return res, fmt.Errorf("missing session")
|
||||||
|
}
|
||||||
|
|
||||||
// Define default values
|
// Default value when an entry is not found
|
||||||
defaultValue := "Not provided"
|
defaultValue := "Not Provided"
|
||||||
name := defaultValue
|
|
||||||
familyName := defaultValue
|
|
||||||
yob := defaultValue
|
|
||||||
gender := defaultValue
|
|
||||||
location := defaultValue
|
|
||||||
offerings := defaultValue
|
|
||||||
|
|
||||||
// Fetch data using a map for better organization
|
// Helper function to handle nil byte slices and convert them to string
|
||||||
// dataKeys := map[string]*string{
|
getEntryOrDefault := func(entry []byte, err error) string {
|
||||||
// FirstName: &name,
|
if err != nil || entry == nil {
|
||||||
// FamilyName: &familyName,
|
return defaultValue
|
||||||
// YearOfBirth: &yob,
|
}
|
||||||
// Location: &location,
|
return string(entry)
|
||||||
// Gender: &gender,
|
}
|
||||||
// Offerings: &offerings,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Iterate over keys and fetch values
|
// Retrieve user data as strings with fallback to defaultValue
|
||||||
//iter := h.db.Iterator()
|
firstName := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_FIRST_NAME))
|
||||||
// next := h.db.Iterator()
|
familyName := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_FAMILY_NAME))
|
||||||
// //defer iter.Close() // Ensure the iterator is closed
|
yob := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_YOB))
|
||||||
// for key, err := next(); err == nil; key, err = next() {
|
gender := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_GENDER))
|
||||||
// if valuePointer, ok := dataKeys[string(key)]; ok {
|
location := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_LOCATION))
|
||||||
// // value, fetchErr := h.db.Fetch(key)
|
offerings := getEntryOrDefault(utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_OFFERINGS))
|
||||||
// // if fetchErr == nil {
|
|
||||||
// // *valuePointer = string(value)
|
|
||||||
// // }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Construct the full name
|
// Construct the full name
|
||||||
|
name := defaultValue
|
||||||
if familyName != defaultValue {
|
if familyName != defaultValue {
|
||||||
if name == defaultValue {
|
if firstName == defaultValue {
|
||||||
name = familyName
|
name = familyName
|
||||||
} else {
|
} else {
|
||||||
name = name + " " + familyName
|
name = firstName + " " + familyName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate age from year of birth
|
// Calculate age from year of birth
|
||||||
var age string
|
age := defaultValue
|
||||||
if yob != defaultValue {
|
if yob != defaultValue {
|
||||||
yobInt, err := strconv.Atoi(yob)
|
if yobInt, err := strconv.Atoi(yob); err == nil {
|
||||||
if err != nil {
|
age = strconv.Itoa(utils.CalculateAgeWithYOB(yobInt))
|
||||||
|
} else {
|
||||||
return res, fmt.Errorf("invalid year of birth: %v", err)
|
return res, fmt.Errorf("invalid year of birth: %v", err)
|
||||||
}
|
}
|
||||||
age = strconv.Itoa(utils.CalculateAgeWithYOB(yobInt))
|
|
||||||
} else {
|
|
||||||
age = defaultValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the result
|
// Format the result
|
||||||
formattedData := fmt.Sprintf("Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n", name, gender, age, location, offerings)
|
res.Content = fmt.Sprintf(
|
||||||
res.Content = formattedData
|
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
|
||||||
|
name, gender, age, location, offerings,
|
||||||
|
)
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user