user

package
v0.0.0-...-efc3f8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package user contains user related CRUD functionalities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NewUser

type NewUser struct {
	Name            string   `json:"name" validate:"required"`
	Email           string   `json:"email" validate:"required,email"`
	Roles           []string `json:"roles" validate:"required"`
	Password        string   `json:"password" validate:"required"`
	PasswordConfirm string   `json:"password_confirm" validate:"eqfield=Password"`
}

[PS] for CRUD operations [PS] we will only have those fields which we expect from the user. This will prevent the issues like mass assignments NewUser contains information needed to create a new User.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages the set of API's for user access. [PS] store is created because all CRUD operation requires same logging and db details and these should not be hidden in contexts

func NewStore

func NewStore(log *zap.SugaredLogger, db *sqlx.DB) Store

NewStore constructs a user store for api access.

func (Store) Authenticate

func (s Store) Authenticate(ctx context.Context, now time.Time, email, password string) (auth.Claims, error)

Authenticate finds a user by their email and verifies their password. On success it returns a Claims User representing this user. The claims can be used to generate a token for future authentication.

func (Store) Create

func (s Store) Create(ctx context.Context, nu NewUser, now time.Time) (User, error)

Create inserts a new user into the database. [PS] context is required for timeouts on db operations

func (Store) Delete

func (s Store) Delete(ctx context.Context, claims auth.Claims, userID string) error

Delete removes a user from the database.

func (Store) Query

func (s Store) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]User, error)

[PS] Retrieve operation. Method 1 Query retrieves a list of existing users from the database.

func (Store) QueryByEmail

func (s Store) QueryByEmail(ctx context.Context, claims auth.Claims, email string) (User, error)

QueryByEmail gets the specified user from the database by email.

func (Store) QueryByID

func (s Store) QueryByID(ctx context.Context, claims auth.Claims, userID string) (User, error)

[PS] Retrieve operation. Method 2 QueryByID gets the specified user from the database.

func (Store) Update

func (s Store) Update(ctx context.Context, claims auth.Claims, userID string, uu UpdateUser, now time.Time) error

Update replaces a user document in the database.

type UpdateUser

type UpdateUser struct {
	Name            *string  `json:"name"`
	Email           *string  `json:"email" validate:"omitempty,email"`
	Roles           []string `json:"roles"`
	Password        *string  `json:"password"`
	PasswordConfirm *string  `json:"password_confirm" validate:"omitempty,eqfield=Password"`
}

UpdateUser defines what information may be provided to modify an existing User. All fields are optional so clients can send just the fields they want changed. It uses pointer fields so we can differentiate between a field that was not provided and a field that was provided as explicitly blank. Normally we do not want to use pointers to basic types but we make exceptions around marshalling/unmarshalling. [PS] here we are using pointer semantic because we expect nil in the value

type User

type User struct {
	ID           string         `db:"user_id" json:"id"`
	Name         string         `db:"name" json:"name"`
	Email        string         `db:"email" json:"email"`
	Roles        pq.StringArray `db:"roles" json:"roles"`
	PasswordHash []byte         `db:"password_hash" json:"-"`
	DateCreated  time.Time      `db:"date_created" json:"date_created"`
	DateUpdated  time.Time      `db:"date_updated" json:"date_updated"`
}

[PS] core data type for mapping it with database User represents an individual user.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL