From d1ba0251c0754f103b518604f99e20c0620cb120 Mon Sep 17 00:00:00 2001 From: nochill Date: Tue, 12 Sep 2023 17:07:03 +0700 Subject: [PATCH] update user model --- db/queries/users.sql | 26 +++++++-- db/sqlc/models.go | 132 ++++++++++++++++++++++++++++++++++++++++++- db/sqlc/querier.go | 4 +- db/sqlc/users.sql.go | 102 ++++++++++++++++++++++++++++++--- 4 files changed, 248 insertions(+), 16 deletions(-) diff --git a/db/queries/users.sql b/db/queries/users.sql index c023d65..6a05c1d 100644 --- a/db/queries/users.sql +++ b/db/queries/users.sql @@ -1,6 +1,22 @@ --- name: CreateUser :exec +-- name: CreateUser :one INSERT INTO users ( - ipv4_address -) VALUES ( - $1 -); \ No newline at end of file + username, + password +) 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; diff --git a/db/sqlc/models.go b/db/sqlc/models.go index c6c5dbe..bb21739 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -6,10 +6,124 @@ package db import ( "database/sql" + "database/sql/driver" + "fmt" + "time" "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 { ID int32 `json:"id"` Address sql.NullString `json:"address"` @@ -79,17 +193,29 @@ type Tag struct { type User struct { ID int32 `json:"id"` Email sql.NullString `json:"email"` - Username sql.NullString `json:"username"` - Password sql.NullString `json:"password"` + Username string `json:"username"` + Password string `json:"password"` AvatarPicture sql.NullString `json:"avatar_picture"` GoogleSignInPayload sql.NullString `json:"google_sign_in_payload"` BannedAt sql.NullTime `json:"banned_at"` BannedUntil sql.NullTime `json:"banned_until"` + BanReason sql.NullString `json:"ban_reason"` + IsPermaban sql.NullBool `json:"is_permaban"` IsAdmin sql.NullBool `json:"is_admin"` IsCritics sql.NullBool `json:"is_critics"` IsVerified sql.NullBool `json:"is_verified"` - Ipv4Address string `json:"ipv4_address"` SocialMedia pqtype.NullRawMessage `json:"social_media"` CreatedAt sql.NullTime `json:"created_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"` +} diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 56e8b18..c1a8c45 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -9,7 +9,9 @@ import ( ) 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) diff --git a/db/sqlc/users.sql.go b/db/sqlc/users.sql.go index 484c376..649ac34 100644 --- a/db/sqlc/users.sql.go +++ b/db/sqlc/users.sql.go @@ -7,17 +7,105 @@ package db import ( "context" + "database/sql" ) -const createUser = `-- name: CreateUser :exec +const createUser = `-- name: CreateUser :one INSERT INTO users ( - ipv4_address -) VALUES ( - $1 -) + username, + password +) 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 { - _, err := q.db.ExecContext(ctx, createUser, ipv4Address) +type CreateUserParams struct { + 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 } + +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 +}