Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User struct + CRUD operations #7

Merged
merged 8 commits into from
Feb 10, 2024
Merged

User struct + CRUD operations #7

merged 8 commits into from
Feb 10, 2024

Conversation

sneha-afk
Copy link
Contributor

@sneha-afk sneha-afk commented Feb 10, 2024

Changelog

  • A User struct has been added to the crud package. Its usage is similar to the preexisting Tasks struct.
  • IMPORTANT! Username and Picture are not fields that are stored to our DB but are supplied by Auth0 upon login. I kept these for future use once we have more user customization + giving something to the frontend to display
  • I have added the CRUD operations to UserTable that read/write from the User struct.
  • I have added unit tests for these operations

Usage

Assume that if all went well once a user logs in, there is a key called user_profile in the Gin session cookies held within the Gin context. Here is an example of retrieving the corresponding User of the logged in user.

  • session := sessions.Default(c): get the cookie store associated with a Gin context c
  • Retrieve the user_profile key and typecast it properly into a crud.User type. Use the second return var ok to make sure it typecasted ok.
session := sessions.Default(c)
userProfile, ok := session.Get("user_profile").(crud.User)
if !ok {
        c.String(http.StatusInternalServerError, "naur :(")
        return
}

Current example in getAllUserTasks()

// Returns a list of all tasks of the current user
func getAllUserTasks(c *gin.Context) {
	// Retrieve the user_id through the struct stored in the session
	session := sessions.Default(c)
	userProfile, ok := session.Get("user_profile").(crud.User)
	if !ok {
		c.String(http.StatusInternalServerError, "Couldn't retreive user's id to display tasks.")
		return
	}
	uid := userProfile.UserID

@sneha-afk sneha-afk self-assigned this Feb 10, 2024
@sneha-afk sneha-afk merged commit 2a4ef92 into main Feb 10, 2024
@sneha-afk sneha-afk deleted the userdata branch February 12, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant