This commit is contained in:
kenneth
2023-12-01 02:46:50 +00:00
parent 90e6ba5070
commit 1bb57bc94a
26 changed files with 1312 additions and 169 deletions

View File

@@ -15,3 +15,19 @@ type User struct {
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
}
type Video struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Images string `json:"images"`
OriginLink string `json:"origin_link"`
PlayLink string `json:"play_link"`
Status int32 `json:"status"`
IsDeleted bool `json:"is_deleted"`
UserID string `json:"user_id"`
CreateAt time.Time `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt time.Time `json:"update_at"`
UpdateBy string `json:"update_by"`
}

View File

@@ -10,12 +10,20 @@ import (
type Querier interface {
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
CreateVideo(ctx context.Context, arg CreateVideoParams) (Video, error)
DeleteUser(ctx context.Context, id string) error
DeleteVideo(ctx context.Context, id string) error
GetUser(ctx context.Context, id string) (User, error)
GetUserByEmail(ctx context.Context, email string) (User, error)
GetUserByName(ctx context.Context, username string) (User, error)
GetVideo(ctx context.Context, id string) (Video, error)
ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error)
ListVideos(ctx context.Context, arg ListVideosParams) ([]Video, error)
ListVideosByUser(ctx context.Context, arg ListVideosByUserParams) ([]Video, error)
SetVideoPlay(ctx context.Context, arg SetVideoPlayParams) (Video, error)
UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)
UpdateVideo(ctx context.Context, arg UpdateVideoParams) (Video, error)
UpdateVideoStatus(ctx context.Context, arg UpdateVideoStatusParams) (Video, error)
}
var _ Querier = (*Queries)(nil)

View File

@@ -0,0 +1,58 @@
-- name: CreateVideo :one
INSERT INTO videos (
id, title, description, images, origin_link, play_link, user_id, create_by
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
RETURNING *;
-- name: DeleteVideo :exec
UPDATE videos
SET is_deleted = TRUE
WHERE id = $1;
-- name: UpdateVideoStatus :one
UPDATE videos
SET status = $2,
update_at = $3,
update_by = $4
WHERE id = $1
RETURNING *;
-- name: SetVideoPlay :one
UPDATE videos
SET status = $2,
play_link = $3,
update_at = $4,
update_by = $5
WHERE id = $1
RETURNING *;
-- name: UpdateVideo :one
UPDATE videos
SET title = $2,
description = $3,
images = $4,
status = $5,
update_at = $6,
update_by = $7
WHERE id = $1
RETURNING *;
-- name: GetVideo :one
SELECT * FROM videos
WHERE id = $1 LIMIT 1;
-- name: ListVideos :many
SELECT * FROM videos
WHERE is_deleted = FALSE AND status=200
ORDER BY id DESC
LIMIT $1
OFFSET $2;
-- name: ListVideosByUser :many
SELECT * FROM videos
WHERE is_deleted = FALSE AND user_id = $1
ORDER BY id DESC
LIMIT $2
OFFSET $3;

View File

@@ -1 +1,2 @@
DROP TABLE "videos";
DROP TABLE "users";

View File

@@ -1,7 +1,39 @@
CREATE TABLE "users" (
"id" varchar NOT NULL PRIMARY KEY,
"username" varchar NOT NULL UNIQUE,
"username" varchar NOT NULL,
"hashed_password" varchar NOT NULL,
"email" varchar NOT NULL UNIQUE,
"email" varchar NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
);
ALTER TABLE "users" ADD CONSTRAINT "username_key" UNIQUE ("username");
ALTER TABLE "users" ADD CONSTRAINT "email_key" UNIQUE ("email");
CREATE INDEX ON "users" ("username");
CREATE INDEX ON "users" ("email");
CREATE TABLE "videos" (
"id" varchar NOT NULL PRIMARY KEY,
"title" varchar NOT NULL,
"description" varchar NOT NULL,
"images" varchar NOT NULL,
"origin_link" varchar NOT NULL,
"play_link" varchar NOT NULL,
-- 100: 下架
-- 0: 添加视频
-- 1: 视频转码中
-- 2: 视频转码失败
-- 200: 视频正常显示播放
"status" int NOT NULL DEFAULT (0),
"is_deleted" boolean NOT NULL DEFAULT false, -- 删除
"user_id" varchar NOT NULL,
"create_at" timestamptz NOT NULL DEFAULT (now()),
"create_by" varchar NOT NULL,
"update_at" timestamptz NOT NULL DEFAULT('0001-01-01 00:00:00Z'),
"update_by" varchar NOT NULL DEFAULT ('')
);
ALTER TABLE "videos" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
CREATE INDEX ON "videos" ("status");
CREATE INDEX ON "videos" ("user_id", "status", "create_at");
CREATE INDEX ON "videos" ("title");
CREATE INDEX ON "videos" ("user_id", "title");

337
internal/db/video.sql.go Normal file
View File

@@ -0,0 +1,337 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.24.0
// source: video.sql
package db
import (
"context"
"time"
)
const createVideo = `-- name: CreateVideo :one
INSERT INTO videos (
id, title, description, images, origin_link, play_link, user_id, create_by
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
RETURNING id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by
`
type CreateVideoParams struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Images string `json:"images"`
OriginLink string `json:"origin_link"`
PlayLink string `json:"play_link"`
UserID string `json:"user_id"`
CreateBy string `json:"create_by"`
}
func (q *Queries) CreateVideo(ctx context.Context, arg CreateVideoParams) (Video, error) {
row := q.db.QueryRowContext(ctx, createVideo,
arg.ID,
arg.Title,
arg.Description,
arg.Images,
arg.OriginLink,
arg.PlayLink,
arg.UserID,
arg.CreateBy,
)
var i Video
err := row.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
)
return i, err
}
const deleteVideo = `-- name: DeleteVideo :exec
UPDATE videos
SET is_deleted = TRUE
WHERE id = $1
`
func (q *Queries) DeleteVideo(ctx context.Context, id string) error {
_, err := q.db.ExecContext(ctx, deleteVideo, id)
return err
}
const getVideo = `-- name: GetVideo :one
SELECT id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by FROM videos
WHERE id = $1 LIMIT 1
`
func (q *Queries) GetVideo(ctx context.Context, id string) (Video, error) {
row := q.db.QueryRowContext(ctx, getVideo, id)
var i Video
err := row.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
)
return i, err
}
const listVideos = `-- name: ListVideos :many
SELECT id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by FROM videos
WHERE is_deleted = FALSE AND status=200
ORDER BY id DESC
LIMIT $1
OFFSET $2
`
type ListVideosParams struct {
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
func (q *Queries) ListVideos(ctx context.Context, arg ListVideosParams) ([]Video, error) {
rows, err := q.db.QueryContext(ctx, listVideos, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Video{}
for rows.Next() {
var i Video
if err := rows.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listVideosByUser = `-- name: ListVideosByUser :many
SELECT id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by FROM videos
WHERE is_deleted = FALSE AND user_id = $1
ORDER BY id DESC
LIMIT $2
OFFSET $3
`
type ListVideosByUserParams struct {
UserID string `json:"user_id"`
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
func (q *Queries) ListVideosByUser(ctx context.Context, arg ListVideosByUserParams) ([]Video, error) {
rows, err := q.db.QueryContext(ctx, listVideosByUser, arg.UserID, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Video{}
for rows.Next() {
var i Video
if err := rows.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const setVideoPlay = `-- name: SetVideoPlay :one
UPDATE videos
SET status = $2,
play_link = $3,
update_at = $4,
update_by = $5
WHERE id = $1
RETURNING id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by
`
type SetVideoPlayParams struct {
ID string `json:"id"`
Status int32 `json:"status"`
PlayLink string `json:"play_link"`
UpdateAt time.Time `json:"update_at"`
UpdateBy string `json:"update_by"`
}
func (q *Queries) SetVideoPlay(ctx context.Context, arg SetVideoPlayParams) (Video, error) {
row := q.db.QueryRowContext(ctx, setVideoPlay,
arg.ID,
arg.Status,
arg.PlayLink,
arg.UpdateAt,
arg.UpdateBy,
)
var i Video
err := row.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
)
return i, err
}
const updateVideo = `-- name: UpdateVideo :one
UPDATE videos
SET title = $2,
description = $3,
images = $4,
status = $5,
update_at = $6,
update_by = $7
WHERE id = $1
RETURNING id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by
`
type UpdateVideoParams struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Images string `json:"images"`
Status int32 `json:"status"`
UpdateAt time.Time `json:"update_at"`
UpdateBy string `json:"update_by"`
}
func (q *Queries) UpdateVideo(ctx context.Context, arg UpdateVideoParams) (Video, error) {
row := q.db.QueryRowContext(ctx, updateVideo,
arg.ID,
arg.Title,
arg.Description,
arg.Images,
arg.Status,
arg.UpdateAt,
arg.UpdateBy,
)
var i Video
err := row.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
)
return i, err
}
const updateVideoStatus = `-- name: UpdateVideoStatus :one
UPDATE videos
SET status = $2,
update_at = $3,
update_by = $4
WHERE id = $1
RETURNING id, title, description, images, origin_link, play_link, status, is_deleted, user_id, create_at, create_by, update_at, update_by
`
type UpdateVideoStatusParams struct {
ID string `json:"id"`
Status int32 `json:"status"`
UpdateAt time.Time `json:"update_at"`
UpdateBy string `json:"update_by"`
}
func (q *Queries) UpdateVideoStatus(ctx context.Context, arg UpdateVideoStatusParams) (Video, error) {
row := q.db.QueryRowContext(ctx, updateVideoStatus,
arg.ID,
arg.Status,
arg.UpdateAt,
arg.UpdateBy,
)
var i Video
err := row.Scan(
&i.ID,
&i.Title,
&i.Description,
&i.Images,
&i.OriginLink,
&i.PlayLink,
&i.Status,
&i.IsDeleted,
&i.UserID,
&i.CreateAt,
&i.CreateBy,
&i.UpdateAt,
&i.UpdateBy,
)
return i, err
}