update user model

This commit is contained in:
nochill 2023-09-12 17:07:03 +07:00
parent 4f962bb104
commit d1ba0251c0
4 changed files with 248 additions and 16 deletions

View File

@ -1,6 +1,22 @@
-- name: CreateUser :exec -- name: CreateUser :one
INSERT INTO users ( INSERT INTO users (
ipv4_address username,
) VALUES ( password
$1 ) VALUES ($1, $2)
); RETURNING *;
-- name: UpdateUser :one
UPDATE users
SET
email = COALESCE(sqlc.narg(email), email),
username = COALESCE(sqlc.narg(username), username),
avatar_picture = COALESCE(sqlc.narg(avatar_picture), avatar_picture)
WHERE
id = sqlc.arg(id)
RETURNING *;
-- name: UpdatePassword :exec
UPDATE users
SET password = $1
WHERE id = $2;

View File

@ -6,10 +6,124 @@ package db
import ( import (
"database/sql" "database/sql"
"database/sql/driver"
"fmt"
"time"
"github.com/sqlc-dev/pqtype" "github.com/sqlc-dev/pqtype"
) )
type CommentType string
const (
CommentTypeStories CommentType = "stories"
CommentTypeNews CommentType = "news"
CommentTypeReviews CommentType = "reviews"
)
func (e *CommentType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = CommentType(s)
case string:
*e = CommentType(s)
default:
return fmt.Errorf("unsupported scan type for CommentType: %T", src)
}
return nil
}
type NullCommentType struct {
CommentType CommentType `json:"comment_type"`
Valid bool `json:"valid"` // Valid is true if CommentType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullCommentType) Scan(value interface{}) error {
if value == nil {
ns.CommentType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.CommentType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullCommentType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.CommentType), nil
}
type UserReportsType string
const (
UserReportsTypeComments UserReportsType = "comments"
UserReportsTypeReviews UserReportsType = "reviews"
UserReportsTypeLocations UserReportsType = "locations"
UserReportsTypeUsers UserReportsType = "users"
UserReportsTypeStories UserReportsType = "stories"
)
func (e *UserReportsType) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = UserReportsType(s)
case string:
*e = UserReportsType(s)
default:
return fmt.Errorf("unsupported scan type for UserReportsType: %T", src)
}
return nil
}
type NullUserReportsType struct {
UserReportsType UserReportsType `json:"user_reports_type"`
Valid bool `json:"valid"` // Valid is true if UserReportsType is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullUserReportsType) Scan(value interface{}) error {
if value == nil {
ns.UserReportsType, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.UserReportsType.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullUserReportsType) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.UserReportsType), nil
}
type ClientIp struct {
ID int32 `json:"id"`
Ipv4 string `json:"ipv4"`
Ipv6 sql.NullString `json:"ipv6"`
BannedAt sql.NullTime `json:"banned_at"`
BannedUntil sql.NullTime `json:"banned_until"`
Reason sql.NullString `json:"reason"`
IsPermaban sql.NullBool `json:"is_permaban"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
type Comment struct {
ID int32 `json:"id"`
SubmittedBy int32 `json:"submitted_by"`
CommentOn int32 `json:"comment_on"`
CommentType CommentType `json:"comment_type"`
ReplyTo sql.NullInt32 `json:"reply_to"`
IsHide sql.NullBool `json:"is_hide"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}
type Location struct { type Location struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Address sql.NullString `json:"address"` Address sql.NullString `json:"address"`
@ -79,17 +193,29 @@ type Tag struct {
type User struct { type User struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Email sql.NullString `json:"email"` Email sql.NullString `json:"email"`
Username sql.NullString `json:"username"` Username string `json:"username"`
Password sql.NullString `json:"password"` Password string `json:"password"`
AvatarPicture sql.NullString `json:"avatar_picture"` AvatarPicture sql.NullString `json:"avatar_picture"`
GoogleSignInPayload sql.NullString `json:"google_sign_in_payload"` GoogleSignInPayload sql.NullString `json:"google_sign_in_payload"`
BannedAt sql.NullTime `json:"banned_at"` BannedAt sql.NullTime `json:"banned_at"`
BannedUntil sql.NullTime `json:"banned_until"` BannedUntil sql.NullTime `json:"banned_until"`
BanReason sql.NullString `json:"ban_reason"`
IsPermaban sql.NullBool `json:"is_permaban"`
IsAdmin sql.NullBool `json:"is_admin"` IsAdmin sql.NullBool `json:"is_admin"`
IsCritics sql.NullBool `json:"is_critics"` IsCritics sql.NullBool `json:"is_critics"`
IsVerified sql.NullBool `json:"is_verified"` IsVerified sql.NullBool `json:"is_verified"`
Ipv4Address string `json:"ipv4_address"`
SocialMedia pqtype.NullRawMessage `json:"social_media"` SocialMedia pqtype.NullRawMessage `json:"social_media"`
CreatedAt sql.NullTime `json:"created_at"` CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"` UpdatedAt sql.NullTime `json:"updated_at"`
} }
type UserReport struct {
ID int32 `json:"id"`
Message string `json:"message"`
Date time.Time `json:"date"`
ReportTarget int32 `json:"report_target"`
ReportType UserReportsType `json:"report_type"`
SubmittedBy int32 `json:"submitted_by"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
}

View File

@ -9,7 +9,9 @@ import (
) )
type Querier interface { type Querier interface {
CreateUser(ctx context.Context, ipv4Address string) error CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
UpdatePassword(ctx context.Context, arg UpdatePasswordParams) error
UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)
} }
var _ Querier = (*Queries)(nil) var _ Querier = (*Queries)(nil)

View File

@ -7,17 +7,105 @@ package db
import ( import (
"context" "context"
"database/sql"
) )
const createUser = `-- name: CreateUser :exec const createUser = `-- name: CreateUser :one
INSERT INTO users ( INSERT INTO users (
ipv4_address username,
) VALUES ( password
$1 ) VALUES ($1, $2)
) RETURNING id, email, username, password, avatar_picture, google_sign_in_payload, banned_at, banned_until, ban_reason, is_permaban, is_admin, is_critics, is_verified, social_media, created_at, updated_at
` `
func (q *Queries) CreateUser(ctx context.Context, ipv4Address string) error { type CreateUserParams struct {
_, err := q.db.ExecContext(ctx, createUser, ipv4Address) Username string `json:"username"`
Password string `json:"password"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRowContext(ctx, createUser, arg.Username, arg.Password)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Username,
&i.Password,
&i.AvatarPicture,
&i.GoogleSignInPayload,
&i.BannedAt,
&i.BannedUntil,
&i.BanReason,
&i.IsPermaban,
&i.IsAdmin,
&i.IsCritics,
&i.IsVerified,
&i.SocialMedia,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const updatePassword = `-- name: UpdatePassword :exec
UPDATE users
SET password = $1
WHERE id = $2
`
type UpdatePasswordParams struct {
Password string `json:"password"`
ID int32 `json:"id"`
}
func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) error {
_, err := q.db.ExecContext(ctx, updatePassword, arg.Password, arg.ID)
return err return err
} }
const updateUser = `-- name: UpdateUser :one
UPDATE users
SET
email = COALESCE($1, email),
username = COALESCE($2, username),
avatar_picture = COALESCE($3, avatar_picture)
WHERE
id = $4
RETURNING id, email, username, password, avatar_picture, google_sign_in_payload, banned_at, banned_until, ban_reason, is_permaban, is_admin, is_critics, is_verified, social_media, created_at, updated_at
`
type UpdateUserParams struct {
Email sql.NullString `json:"email"`
Username sql.NullString `json:"username"`
AvatarPicture sql.NullString `json:"avatar_picture"`
ID int32 `json:"id"`
}
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) {
row := q.db.QueryRowContext(ctx, updateUser,
arg.Email,
arg.Username,
arg.AvatarPicture,
arg.ID,
)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Username,
&i.Password,
&i.AvatarPicture,
&i.GoogleSignInPayload,
&i.BannedAt,
&i.BannedUntil,
&i.BanReason,
&i.IsPermaban,
&i.IsAdmin,
&i.IsCritics,
&i.IsVerified,
&i.SocialMedia,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}