Added badge migration

This commit is contained in:
Max 2025-02-04 21:33:08 +03:00
parent 547bc9e9e9
commit c0dcbcf2c5
5 changed files with 334 additions and 3 deletions

View File

@ -0,0 +1,243 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// source: badge_queries.sql
package db_repo
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const addBadgeToUser = `-- name: AddBadgeToUser :one
INSERT INTO public.users_badges (user_id, badge_id)
VALUES
(
$1,
$2
)
RETURNING user_id, badge_id
`
type AddBadgeToUserParams struct {
UserID int64 `json:"user_id"`
BadgeID uuid.UUID `json:"badge_id"`
}
func (q *Queries) AddBadgeToUser(ctx context.Context, arg AddBadgeToUserParams) (UsersBadge, error) {
row := q.db.QueryRow(ctx, addBadgeToUser, arg.UserID, arg.BadgeID)
var i UsersBadge
err := row.Scan(&i.UserID, &i.BadgeID)
return i, err
}
const deleteBadge = `-- name: DeleteBadge :exec
DELETE FROM public.badges
WHERE
id = $1
`
func (q *Queries) DeleteBadge(ctx context.Context, id uuid.UUID) error {
_, err := q.db.Exec(ctx, deleteBadge, id)
return err
}
const getAllBadges = `-- name: GetAllBadges :many
SELECT id, name, description, color
FROM public.badges
`
func (q *Queries) GetAllBadges(ctx context.Context) ([]Badge, error) {
rows, err := q.db.Query(ctx, getAllBadges)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Badge
for rows.Next() {
var i Badge
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.Color,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getBadgeByID = `-- name: GetBadgeByID :one
SELECT id, name, description, color
FROM public.badges
WHERE id = $1
`
func (q *Queries) GetBadgeByID(ctx context.Context, id uuid.UUID) (Badge, error) {
row := q.db.QueryRow(ctx, getBadgeByID, id)
var i Badge
err := row.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.Color,
)
return i, err
}
const getUserBadges = `-- name: GetUserBadges :many
SELECT b.id, b.name, b.description, b.color
FROM public.badges b
JOIN public.users_badges ub ON b.id = ub.badge_id
WHERE ub.user_id = $1
`
func (q *Queries) GetUserBadges(ctx context.Context, userID int64) ([]Badge, error) {
rows, err := q.db.Query(ctx, getUserBadges, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Badge
for rows.Next() {
var i Badge
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.Color,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getUsersWithBadge = `-- name: GetUsersWithBadge :many
SELECT u.user_id, u.username, u.email, u.password, u.created_at, u.is_admin, u.display_name
FROM public.users u
JOIN public.users_badges ub ON u.user_id = ub.user_id
WHERE ub.badge_id = $1
`
func (q *Queries) GetUsersWithBadge(ctx context.Context, badgeID uuid.UUID) ([]User, error) {
rows, err := q.db.Query(ctx, getUsersWithBadge, badgeID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []User
for rows.Next() {
var i User
if err := rows.Scan(
&i.UserID,
&i.Username,
&i.Email,
&i.Password,
&i.CreatedAt,
&i.IsAdmin,
&i.DisplayName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertBadge = `-- name: InsertBadge :one
INSERT INTO public.badges (id, name, description, color)
VALUES
(
uuid_generate_v4(),
$1,
$2,
$3
)
RETURNING id, name, description, color
`
type InsertBadgeParams struct {
Name string `json:"name"`
Description pgtype.Text `json:"description"`
Color pgtype.Text `json:"color"`
}
func (q *Queries) InsertBadge(ctx context.Context, arg InsertBadgeParams) (Badge, error) {
row := q.db.QueryRow(ctx, insertBadge, arg.Name, arg.Description, arg.Color)
var i Badge
err := row.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.Color,
)
return i, err
}
const removeBadgeFromUser = `-- name: RemoveBadgeFromUser :exec
DELETE FROM public.users_badges
WHERE
user_id = $1
AND badge_id = $2
`
type RemoveBadgeFromUserParams struct {
UserID int64 `json:"user_id"`
BadgeID uuid.UUID `json:"badge_id"`
}
func (q *Queries) RemoveBadgeFromUser(ctx context.Context, arg RemoveBadgeFromUserParams) error {
_, err := q.db.Exec(ctx, removeBadgeFromUser, arg.UserID, arg.BadgeID)
return err
}
const updateBadge = `-- name: UpdateBadge :one
UPDATE public.badges
SET
name = $2,
description = $3,
color = $4
WHERE
id = $1
RETURNING id, name, description, color
`
type UpdateBadgeParams struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description pgtype.Text `json:"description"`
Color pgtype.Text `json:"color"`
}
func (q *Queries) UpdateBadge(ctx context.Context, arg UpdateBadgeParams) (Badge, error) {
row := q.db.QueryRow(ctx, updateBadge,
arg.ID,
arg.Name,
arg.Description,
arg.Color,
)
var i Badge
err := row.Scan(
&i.ID,
&i.Name,
&i.Description,
&i.Color,
)
return i, err
}

View File

@ -5,9 +5,17 @@
package db_repo
import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
type Badge struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description pgtype.Text `json:"description"`
Color pgtype.Text `json:"color"`
}
type Blog struct {
BlogID int64 `json:"blog_id"`
UserID int64 `json:"user_id"`
@ -92,3 +100,8 @@ type User struct {
IsAdmin bool `json:"is_admin"`
DisplayName pgtype.Text `json:"display_name"`
}
type UsersBadge struct {
UserID int64 `json:"user_id"`
BadgeID uuid.UUID `json:"badge_id"`
}

View File

@ -1 +1,3 @@
DROP TABLE badges;
DROP TABLE IF EXISTS users_badges;
DROP TABLE IF EXISTS badges;

View File

@ -1,4 +1,16 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE IF NOT EXISTS public.badges(
id uuid PRIMARY KEY,
name VARCHAR(255) NOT NULL
)
name VARCHAR(255) NOT NULL,
description TEXT,
color VARCHAR(32)
);
CREATE TABLE IF NOT EXISTS public.users_badges(
user_id BIGINT NOT NULL,
badge_id uuid NOT NULL,
constraint users_badges_pkey primary key (user_id, badge_id),
CONSTRAINT "user_id_badge_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON DELETE CASCADE,
CONSTRAINT "badge_id_user_id_fkey" FOREIGN KEY ("badge_id") REFERENCES "public"."badges" ("id") ON DELETE CASCADE
);

View File

@ -0,0 +1,61 @@
-- name: InsertBadge :one
INSERT INTO public.badges (id, name, description, color)
VALUES
(
uuid_generate_v4(),
$1,
$2,
$3
)
RETURNING *;
-- name: UpdateBadge :one
UPDATE public.badges
SET
name = $2,
description = $3,
color = $4
WHERE
id = $1
RETURNING *;
-- name: DeleteBadge :exec
DELETE FROM public.badges
WHERE
id = $1;
-- name: GetAllBadges :many
SELECT *
FROM public.badges;
-- name: GetBadgeByID :one
SELECT *
FROM public.badges
WHERE id = $1;
-- name: AddBadgeToUser :one
INSERT INTO public.users_badges (user_id, badge_id)
VALUES
(
$1,
$2
)
RETURNING *;
-- name: RemoveBadgeFromUser :exec
DELETE FROM public.users_badges
WHERE
user_id = $1
AND badge_id = $2;
-- name: GetUserBadges :many
SELECT b.*
FROM public.badges b
JOIN public.users_badges ub ON b.id = ub.badge_id
WHERE ub.user_id = $1;
-- name: GetUsersWithBadge :many
SELECT u.*
FROM public.users u
JOIN public.users_badges ub ON u.user_id = ub.user_id
WHERE ub.badge_id = $1;