implement JWT tokens, regenerate docs and sqlc
This commit is contained in:
+5
-7
@@ -25,13 +25,11 @@ type Artist struct {
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
UserMail string `json:"user_mail"`
|
||||
Password string `json:"password"`
|
||||
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||
SessionToken pgtype.Text `json:"session_token"`
|
||||
SessionExpiry pgtype.Timestamp `json:"session_expiry"`
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
UserMail string `json:"user_mail"`
|
||||
Password string `json:"password"`
|
||||
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserAlbum struct {
|
||||
|
||||
+1
-3
@@ -12,7 +12,6 @@ import (
|
||||
|
||||
type Querier interface {
|
||||
AddUserAlbum(ctx context.Context, arg AddUserAlbumParams) error
|
||||
CheckUserExistsByEmail(ctx context.Context, userMail string) (pgtype.UUID, error)
|
||||
CreateAlbum(ctx context.Context, arg CreateAlbumParams) (pgtype.UUID, error)
|
||||
CreateArtist(ctx context.Context, arg CreateArtistParams) (pgtype.UUID, error)
|
||||
// users.sql
|
||||
@@ -29,9 +28,9 @@ type Querier interface {
|
||||
GetUserAlbum(ctx context.Context, arg GetUserAlbumParams) (GetUserAlbumRow, error)
|
||||
// user_albums.sql
|
||||
GetUserAlbums(ctx context.Context, userID pgtype.UUID) ([]GetUserAlbumsRow, error)
|
||||
GetUserAuthByEmail(ctx context.Context, userMail string) (GetUserAuthByEmailRow, error)
|
||||
GetUserByID(ctx context.Context, id pgtype.UUID) (GetUserByIDRow, error)
|
||||
GetUsernameByID(ctx context.Context, id pgtype.UUID) (string, error)
|
||||
LoginUser(ctx context.Context, arg LoginUserParams) (LoginUserRow, error)
|
||||
RemoveUserAlbum(ctx context.Context, arg RemoveUserAlbumParams) error
|
||||
SearchAlbums(ctx context.Context, dollar_1 pgtype.Text) ([]Album, error)
|
||||
SearchArtists(ctx context.Context, dollar_1 pgtype.Text) ([]Artist, error)
|
||||
@@ -39,7 +38,6 @@ type Querier interface {
|
||||
UpdateArtist(ctx context.Context, arg UpdateArtistParams) error
|
||||
UpdateUser(ctx context.Context, arg UpdateUserParams) error
|
||||
UpdateUserAlbumStatus(ctx context.Context, arg UpdateUserAlbumStatusParams) error
|
||||
UpdateUserSession(ctx context.Context, arg UpdateUserSessionParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
|
||||
+25
-48
@@ -11,17 +11,6 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const checkUserExistsByEmail = `-- name: CheckUserExistsByEmail :one
|
||||
SELECT id FROM users WHERE user_mail = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CheckUserExistsByEmail(ctx context.Context, userMail string) (pgtype.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, checkUserExistsByEmail, userMail)
|
||||
var id pgtype.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO users (id, user_name, user_mail, password) VALUES ($1, $2, $3, $4) RETURNING id
|
||||
`
|
||||
@@ -46,6 +35,31 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (pgtype.
|
||||
return id, err
|
||||
}
|
||||
|
||||
const getUserAuthByEmail = `-- name: GetUserAuthByEmail :one
|
||||
SELECT id, user_name, user_mail, password
|
||||
FROM users
|
||||
WHERE user_mail = $1
|
||||
`
|
||||
|
||||
type GetUserAuthByEmailRow struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
UserMail string `json:"user_mail"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserAuthByEmail(ctx context.Context, userMail string) (GetUserAuthByEmailRow, error) {
|
||||
row := q.db.QueryRow(ctx, getUserAuthByEmail, userMail)
|
||||
var i GetUserAuthByEmailRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UserName,
|
||||
&i.UserMail,
|
||||
&i.Password,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByID = `-- name: GetUserByID :one
|
||||
SELECT id, user_name, user_mail, created_at FROM users WHERE id = $1
|
||||
`
|
||||
@@ -80,28 +94,6 @@ func (q *Queries) GetUsernameByID(ctx context.Context, id pgtype.UUID) (string,
|
||||
return user_name, err
|
||||
}
|
||||
|
||||
const loginUser = `-- name: LoginUser :one
|
||||
SELECT id, user_name, user_mail FROM users WHERE user_mail = $1 AND password = $2
|
||||
`
|
||||
|
||||
type LoginUserParams struct {
|
||||
UserMail string `json:"user_mail"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type LoginUserRow struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
UserMail string `json:"user_mail"`
|
||||
}
|
||||
|
||||
func (q *Queries) LoginUser(ctx context.Context, arg LoginUserParams) (LoginUserRow, error) {
|
||||
row := q.db.QueryRow(ctx, loginUser, arg.UserMail, arg.Password)
|
||||
var i LoginUserRow
|
||||
err := row.Scan(&i.ID, &i.UserName, &i.UserMail)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateUser = `-- name: UpdateUser :exec
|
||||
UPDATE users SET user_name = $2, user_mail = $3, password = $4 WHERE id = $1
|
||||
`
|
||||
@@ -122,18 +114,3 @@ func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateUserSession = `-- name: UpdateUserSession :exec
|
||||
UPDATE users SET session_token = $2, session_expiry = $3 WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateUserSessionParams struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
SessionToken pgtype.Text `json:"session_token"`
|
||||
SessionExpiry pgtype.Timestamp `json:"session_expiry"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUserSession(ctx context.Context, arg UpdateUserSessionParams) error {
|
||||
_, err := q.db.Exec(ctx, updateUserSession, arg.ID, arg.SessionToken, arg.SessionExpiry)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user