hiling_go/db/sqlc/users.go

234 lines
6.7 KiB
Go
Raw Normal View History

2023-09-23 15:24:21 +07:00
package db
import (
"context"
2023-10-04 19:51:29 +07:00
"encoding/json"
2023-10-09 16:25:39 +07:00
"time"
2024-03-04 16:09:55 +07:00
"github.com/jackc/pgx/v5/pgtype"
2023-09-23 15:24:21 +07:00
)
2023-10-04 19:51:29 +07:00
const getUserQ = `-- name: GetUser :one
2023-09-23 15:24:21 +07:00
SELECT
id,
COALESCE(email, '') as email,
password,
username,
2023-10-09 16:25:39 +07:00
COALESCE(google_sign_in_payload, '') as google_sign_in_payload,
COALESCE(about, '') as about,
COALESCE(website, '') as website,
2023-09-23 15:24:21 +07:00
COALESCE(avatar_picture, '') as avatar_picture,
banned_at,
banned_until,
COALESCE(ban_reason, '') as ban_reason,
is_permaban,
is_admin,
is_critics,
is_verified,
2023-10-09 16:25:39 +07:00
COALESCE(social_media, '[]'),
created_at,
updated_at
2023-09-23 15:24:21 +07:00
FROM USERS
WHERE username = $1
`
type GetUserRow struct {
2024-03-04 16:09:55 +07:00
ID int32 `json:"id"`
Email string `json:"email"`
Password string `json:"-"`
About string `json:"about"`
Website string `json:"website"`
Username string `json:"username"`
GoogleSignInPayload string `json:"google_sign_in_payload"`
AvatarPicture string `json:"avatar_picture"`
BannedAt pgtype.Timestamp `json:"banned_at"`
BannedUntil pgtype.Timestamp `json:"banned_until"`
BanReason string `json:"ban_reason"`
IsPermaban bool `json:"is_permaban"`
IsAdmin bool `json:"is_admin"`
IsCritics bool `json:"is_critics"`
IsVerified bool `json:"is_verified"`
SocialMedia json.RawMessage `json:"social_media"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
2023-09-23 15:24:21 +07:00
}
func (q *Queries) GetUser(ctx context.Context, username string) (GetUserRow, error) {
2024-02-06 11:55:25 +07:00
row := q.db.QueryRow(ctx, getUserQ, username)
2023-09-23 15:24:21 +07:00
var i GetUserRow
err := row.Scan(
&i.ID,
&i.Email,
&i.Password,
&i.Username,
2023-10-09 16:25:39 +07:00
&i.GoogleSignInPayload,
&i.About,
&i.Website,
2023-09-23 15:24:21 +07:00
&i.AvatarPicture,
&i.BannedAt,
&i.BannedUntil,
&i.BanReason,
&i.IsPermaban,
&i.IsAdmin,
&i.IsCritics,
&i.IsVerified,
&i.SocialMedia,
2023-10-09 16:25:39 +07:00
&i.CreatedAt,
&i.UpdatedAt,
2023-09-23 15:24:21 +07:00
)
return i, err
}
2023-10-04 19:51:29 +07:00
const getUserStatsQ = `
SELECT
json_agg(ur.*) AS reviews,
( SELECT COUNT(id) FROM user_follow u WHERE u.followee_id = $1) as followers,
( SELECT COUNT(id) FROM reviews r WHERE r.submitted_by = $1 ) as score_count,
( SELECT
json_agg(r1.*) AS scores_distribution
FROM
(
SELECT
*
FROM
( SELECT COUNT(id) as "0" FROM reviews r WHERE r.score >= 0 AND r.score <= 9 AND r.submitted_by = $1) as score0,
( SELECT COUNT(id) as "1" FROM reviews r WHERE r.score >= 10 AND r.score <= 19 AND r.submitted_by = $1) as score1,
( SELECT COUNT(id) as "2" FROM reviews r WHERE r.score >= 20 AND r.score <= 29 AND r.submitted_by = $1) as score2,
( SELECT COUNT(id) as "3" FROM reviews r WHERE r.score >= 30 AND r.score <= 39 AND r.submitted_by = $1) as score3,
( SELECT COUNT(id) as "4" FROM reviews r WHERE r.score >= 40 AND r.score <= 49 AND r.submitted_by = $1) as score4,
( SELECT COUNT(id) as "5" FROM reviews r WHERE r.score >= 50 AND r.score <= 59 AND r.submitted_by = $1) as score5,
( SELECT COUNT(id) as "6" FROM reviews r WHERE r.score >= 60 AND r.score <= 69 AND r.submitted_by = $1) as score6,
( SELECT COUNT(id) as "7" FROM reviews r WHERE r.score >= 70 AND r.score <= 79 AND r.submitted_by = $1) as score7,
( SELECT COUNT(id) as "8" FROM reviews r WHERE r.score >= 80 AND r.score <= 89 AND r.submitted_by = $1) as score8,
( SELECT COUNT(id) as "9" FROM reviews r WHERE r.score >= 90 AND r.score <= 99 AND r.submitted_by = $1) as score9,
( SELECT COUNT(id) as "99" FROM reviews r WHERE r.score = 100 AND r.submitted_by = $1) as score10
) r1
)
FROM (
SELECT
reviews.id,
reviews.comments,
score,
l.name,
p.province_name,
COALESCE(l.thumbnail, '') as thumbnail
FROM
reviews
JOIN locations l on reviews.location_id = l.id
JOIN regencies r on l.regency_id = r.id
JOIN provinces p on r.province_id = p.id
WHERE reviews.submitted_by = $1
ORDER BY score ASC
LIMIT 10
) ur
`
type GetUserStatsRow struct {
Reviews []byte `json:"-"`
Followers int32 `json:"followers"`
ScoreCount int32 `json:"score_count"`
ScoresDistribution []byte `json:"-"`
}
func (q *Queries) GetUserStats(ctx context.Context, user_id int32) (GetUserStatsRow, error) {
var i GetUserStatsRow
2024-02-06 11:55:25 +07:00
row := q.db.QueryRow(ctx, getUserStatsQ, user_id)
2023-10-04 19:51:29 +07:00
err := row.Scan(
&i.Reviews,
&i.Followers,
&i.ScoreCount,
&i.ScoresDistribution,
)
2023-10-09 16:25:39 +07:00
return i, err
}
2023-10-04 19:51:29 +07:00
2023-10-09 16:25:39 +07:00
const updateUser = `-- name: UpdateUser :one
UPDATE users
SET
about = $1,
social_media = $2,
website = $3
WHERE
id = $4
RETURNING
id,
COALESCE(email, ''),
username,
COALESCE(avatar_picture, ''),
COALESCE(about, ''),
COALESCE(website, ''),
COALESCE(google_sign_in_payload, ''),
banned_at,
banned_until,
COALESCE(ban_reason, ''),
is_permaban,
is_admin,
is_critics,
is_verified,
is_active,
COALESCE(social_media, '[]'),
created_at,
updated_at
`
2023-10-04 19:51:29 +07:00
2023-10-09 16:25:39 +07:00
type UpdateUserParams struct {
2024-03-04 16:09:55 +07:00
About pgtype.Text `json:"about"`
SocialMedia interface{} `json:"social_media"`
Website pgtype.Text `json:"website"`
ID int32 `json:"id"`
2023-10-09 16:25:39 +07:00
}
2023-10-04 19:51:29 +07:00
2023-10-09 16:25:39 +07:00
type UpdateUserRow struct {
2024-03-04 16:09:55 +07:00
ID int32 `json:"id"`
Email string `json:"email"`
Username string `json:"username"`
AvatarPicture string `json:"avatar_picture"`
About string `json:"about"`
Website string `json:"website"`
GoogleSignInPayload string `json:"google_sign_in_payload"`
BannedAt pgtype.Timestamp `json:"banned_at"`
BannedUntil pgtype.Timestamp `json:"banned_until"`
BanReason string `json:"ban_reason"`
IsPermaban bool `json:"is_permaban"`
IsAdmin bool `json:"is_admin"`
IsCritics bool `json:"is_critics"`
IsVerified bool `json:"is_verified"`
IsActive bool `json:"is_active"`
SocialMedia json.RawMessage `json:"social_media"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
2023-10-09 16:25:39 +07:00
}
2023-10-04 19:51:29 +07:00
2023-10-09 16:25:39 +07:00
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (UpdateUserRow, error) {
2024-02-06 11:55:25 +07:00
row := q.db.QueryRow(ctx, updateUser,
2023-10-09 16:25:39 +07:00
arg.About,
arg.SocialMedia,
arg.Website,
arg.ID,
)
var i UpdateUserRow
err := row.Scan(
&i.ID,
&i.Email,
&i.Username,
&i.AvatarPicture,
&i.About,
&i.Website,
&i.GoogleSignInPayload,
&i.BannedAt,
&i.BannedUntil,
&i.BanReason,
&i.IsPermaban,
&i.IsAdmin,
&i.IsCritics,
&i.IsVerified,
&i.IsActive,
&i.SocialMedia,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
2023-10-04 19:51:29 +07:00
}