Sql request for blogs/users/categories/profiles tables

This commit is contained in:
Max 2024-10-20 15:29:33 +03:00
parent 29b35e931a
commit 6b4fe31d67
17 changed files with 566 additions and 53 deletions

View File

@ -1 +1,3 @@
# Enshi
# Enshi
This (probably) will be good blog-platform.

View File

@ -12,11 +12,6 @@ import {
ThemePanel,
} from "@radix-ui/themes";
import { useTranslation } from "react-i18next";
import { useRef, useState } from "react";
import parse from "html-react-parser";
import Editor from "./Components/Editor/Editor";
import {
Router,
Route,

View File

@ -0,0 +1,123 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: blogs_queries.sql
package db_repo
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const createBlogByUserId = `-- name: CreateBlogByUserId :one
INSERT INTO public.blogs
(blog_id, user_id, title, description, category_id, created_at)
VALUES($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)
RETURNING blog_id, user_id, title, description, category_id, created_at
`
type CreateBlogByUserIdParams struct {
BlogID int64 `json:"blog_id"`
UserID int64 `json:"user_id"`
Title pgtype.Text `json:"title"`
Description pgtype.Text `json:"description"`
CategoryID pgtype.Int4 `json:"category_id"`
}
func (q *Queries) CreateBlogByUserId(ctx context.Context, arg CreateBlogByUserIdParams) (Blog, error) {
row := q.db.QueryRow(ctx, createBlogByUserId,
arg.BlogID,
arg.UserID,
arg.Title,
arg.Description,
arg.CategoryID,
)
var i Blog
err := row.Scan(
&i.BlogID,
&i.UserID,
&i.Title,
&i.Description,
&i.CategoryID,
&i.CreatedAt,
)
return i, err
}
const deleteBlogByBlogId = `-- name: DeleteBlogByBlogId :exec
DELETE FROM public.blogs
WHERE blog_id=$1
`
func (q *Queries) DeleteBlogByBlogId(ctx context.Context, blogID int64) error {
_, err := q.db.Exec(ctx, deleteBlogByBlogId, blogID)
return err
}
const getBlogsByUserId = `-- name: GetBlogsByUserId :many
SELECT blog_id, user_id, title, description, category_id, created_at
FROM public.blogs
WHERE user_id = $1
`
func (q *Queries) GetBlogsByUserId(ctx context.Context, userID int64) ([]Blog, error) {
rows, err := q.db.Query(ctx, getBlogsByUserId, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Blog
for rows.Next() {
var i Blog
if err := rows.Scan(
&i.BlogID,
&i.UserID,
&i.Title,
&i.Description,
&i.CategoryID,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateBlogInfoByBlogId = `-- name: UpdateBlogInfoByBlogId :one
UPDATE public.blogs
SET title=$1, description=$2, category_id=$3
WHERE blog_id=$4
RETURNING blog_id, user_id, title, description, category_id, created_at
`
type UpdateBlogInfoByBlogIdParams struct {
Title pgtype.Text `json:"title"`
Description pgtype.Text `json:"description"`
CategoryID pgtype.Int4 `json:"category_id"`
BlogID int64 `json:"blog_id"`
}
func (q *Queries) UpdateBlogInfoByBlogId(ctx context.Context, arg UpdateBlogInfoByBlogIdParams) (Blog, error) {
row := q.db.QueryRow(ctx, updateBlogInfoByBlogId,
arg.Title,
arg.Description,
arg.CategoryID,
arg.BlogID,
)
var i Blog
err := row.Scan(
&i.BlogID,
&i.UserID,
&i.Title,
&i.Description,
&i.CategoryID,
&i.CreatedAt,
)
return i, err
}

View File

@ -0,0 +1,74 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: categories_queries.sql
package db_repo
import (
"context"
)
const createCategory = `-- name: CreateCategory :one
INSERT INTO public.categories
(category_id, category_name)
VALUES($1, $2)
RETURNING category_id, category_name
`
type CreateCategoryParams struct {
CategoryID int32 `json:"category_id"`
CategoryName string `json:"category_name"`
}
func (q *Queries) CreateCategory(ctx context.Context, arg CreateCategoryParams) (Category, error) {
row := q.db.QueryRow(ctx, createCategory, arg.CategoryID, arg.CategoryName)
var i Category
err := row.Scan(&i.CategoryID, &i.CategoryName)
return i, err
}
const deleteCategoryById = `-- name: DeleteCategoryById :exec
DELETE FROM public.categories
WHERE category_id=$1
`
func (q *Queries) DeleteCategoryById(ctx context.Context, categoryID int32) error {
_, err := q.db.Exec(ctx, deleteCategoryById, categoryID)
return err
}
const getAllCategories = `-- name: GetAllCategories :many
SELECT category_id, category_name FROM public.categories
`
func (q *Queries) GetAllCategories(ctx context.Context) ([]Category, error) {
rows, err := q.db.Query(ctx, getAllCategories)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Category
for rows.Next() {
var i Category
if err := rows.Scan(&i.CategoryID, &i.CategoryName); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCategoryByName = `-- name: GetCategoryByName :one
SELECT category_id, category_name FROM public.categories WHERE category_name = $1
`
func (q *Queries) GetCategoryByName(ctx context.Context, categoryName string) (Category, error) {
row := q.db.QueryRow(ctx, getCategoryByName, categoryName)
var i Category
err := row.Scan(&i.CategoryID, &i.CategoryName)
return i, err
}

View File

@ -89,4 +89,5 @@ type User struct {
Email string `json:"email"`
Password string `json:"password"`
CreatedAt pgtype.Timestamp `json:"created_at"`
IsAdmin bool `json:"is_admin"`
}

View File

@ -0,0 +1,116 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: profiles_queries.sql
package db_repo
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const clearProfileByUserId = `-- name: ClearProfileByUserId :one
UPDATE public.profiles
SET bio='', avatar_url='', website_url=''
WHERE user_id=$1
RETURNING profile_id, user_id, bio, avatar_url, website_url
`
func (q *Queries) ClearProfileByUserId(ctx context.Context, userID pgtype.Int8) (Profile, error) {
row := q.db.QueryRow(ctx, clearProfileByUserId, userID)
var i Profile
err := row.Scan(
&i.ProfileID,
&i.UserID,
&i.Bio,
&i.AvatarUrl,
&i.WebsiteUrl,
)
return i, err
}
const createProfileForUser = `-- name: CreateProfileForUser :one
INSERT INTO public.profiles
(profile_id, user_id, bio, avatar_url, website_url)
VALUES($1, $2, '', '', '')
RETURNING profile_id, user_id, bio, avatar_url, website_url
`
type CreateProfileForUserParams struct {
ProfileID int64 `json:"profile_id"`
UserID pgtype.Int8 `json:"user_id"`
}
func (q *Queries) CreateProfileForUser(ctx context.Context, arg CreateProfileForUserParams) (Profile, error) {
row := q.db.QueryRow(ctx, createProfileForUser, arg.ProfileID, arg.UserID)
var i Profile
err := row.Scan(
&i.ProfileID,
&i.UserID,
&i.Bio,
&i.AvatarUrl,
&i.WebsiteUrl,
)
return i, err
}
const deleteProfileByUserId = `-- name: DeleteProfileByUserId :exec
DELETE FROM public.profiles
WHERE user_id=$1
`
func (q *Queries) DeleteProfileByUserId(ctx context.Context, userID pgtype.Int8) error {
_, err := q.db.Exec(ctx, deleteProfileByUserId, userID)
return err
}
const getProfileByUserId = `-- name: GetProfileByUserId :one
SELECT profile_id, user_id, bio, avatar_url, website_url FROM public.profiles WHERE user_id = $1
`
func (q *Queries) GetProfileByUserId(ctx context.Context, userID pgtype.Int8) (Profile, error) {
row := q.db.QueryRow(ctx, getProfileByUserId, userID)
var i Profile
err := row.Scan(
&i.ProfileID,
&i.UserID,
&i.Bio,
&i.AvatarUrl,
&i.WebsiteUrl,
)
return i, err
}
const updateProfileByUserId = `-- name: UpdateProfileByUserId :one
UPDATE public.profiles
SET bio=$2, avatar_url=$3, website_url=$4
WHERE profile_id=$1
RETURNING profile_id, user_id, bio, avatar_url, website_url
`
type UpdateProfileByUserIdParams struct {
ProfileID int64 `json:"profile_id"`
Bio pgtype.Text `json:"bio"`
AvatarUrl pgtype.Text `json:"avatar_url"`
WebsiteUrl pgtype.Text `json:"website_url"`
}
func (q *Queries) UpdateProfileByUserId(ctx context.Context, arg UpdateProfileByUserIdParams) (Profile, error) {
row := q.db.QueryRow(ctx, updateProfileByUserId,
arg.ProfileID,
arg.Bio,
arg.AvatarUrl,
arg.WebsiteUrl,
)
var i Profile
err := row.Scan(
&i.ProfileID,
&i.UserID,
&i.Bio,
&i.AvatarUrl,
&i.WebsiteUrl,
)
return i, err
}

View File

@ -0,0 +1,156 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: users_queries.sql
package db_repo
import (
"context"
)
const createUser = `-- name: CreateUser :one
INSERT INTO public.users
(user_id, username, email, "password", created_at, is_admin)
VALUES($1, $2, $3, $4, CURRENT_TIMESTAMP, false)
RETURNING user_id, username, email, password, created_at, is_admin
`
type CreateUserParams struct {
UserID int64 `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRow(ctx, createUser,
arg.UserID,
arg.Username,
arg.Email,
arg.Password,
)
var i User
err := row.Scan(
&i.UserID,
&i.Username,
&i.Email,
&i.Password,
&i.CreatedAt,
&i.IsAdmin,
)
return i, err
}
const deleteUserById = `-- name: DeleteUserById :exec
DELETE FROM public.users
WHERE user_id=$1
`
func (q *Queries) DeleteUserById(ctx context.Context, userID int64) error {
_, err := q.db.Exec(ctx, deleteUserById, userID)
return err
}
const deleteUserByUsername = `-- name: DeleteUserByUsername :exec
DELETE FROM public.users
WHERE username=$1
`
func (q *Queries) DeleteUserByUsername(ctx context.Context, username string) error {
_, err := q.db.Exec(ctx, deleteUserByUsername, username)
return err
}
const getAllUsers = `-- name: GetAllUsers :many
SELECT user_id, username, email, password, created_at, is_admin FROM users
`
func (q *Queries) GetAllUsers(ctx context.Context) ([]User, error) {
rows, err := q.db.Query(ctx, getAllUsers)
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,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getUserById = `-- name: GetUserById :one
SELECT user_id, username, email, password, created_at, is_admin FROM users WHERE user_id = $1
`
func (q *Queries) GetUserById(ctx context.Context, userID int64) (User, error) {
row := q.db.QueryRow(ctx, getUserById, userID)
var i User
err := row.Scan(
&i.UserID,
&i.Username,
&i.Email,
&i.Password,
&i.CreatedAt,
&i.IsAdmin,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT user_id, username, email, password, created_at, is_admin FROM users WHERE username = $1
`
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
row := q.db.QueryRow(ctx, getUserByUsername, username)
var i User
err := row.Scan(
&i.UserID,
&i.Username,
&i.Email,
&i.Password,
&i.CreatedAt,
&i.IsAdmin,
)
return i, err
}
const updateUserPasswordHash = `-- name: UpdateUserPasswordHash :one
UPDATE public.users
SET "password"=$1
WHERE user_id=$2
RETURNING user_id, username, email, password, created_at, is_admin
`
type UpdateUserPasswordHashParams struct {
Password string `json:"password"`
UserID int64 `json:"user_id"`
}
func (q *Queries) UpdateUserPasswordHash(ctx context.Context, arg UpdateUserPasswordHashParams) (User, error) {
row := q.db.QueryRow(ctx, updateUserPasswordHash, arg.Password, arg.UserID)
var i User
err := row.Scan(
&i.UserID,
&i.Username,
&i.Email,
&i.Password,
&i.CreatedAt,
&i.IsAdmin,
)
return i, err
}

View File

@ -5,7 +5,7 @@ COMMENT ON SCHEMA "public" IS 'standard public schema';
-- Create "categories" table
CREATE TABLE "public"."categories" ("category_id" integer NOT NULL, "category_name" character varying(50) NOT NULL, PRIMARY KEY ("category_id"), CONSTRAINT "categories_category_name_key" UNIQUE ("category_name"));
-- Create "users" table
CREATE TABLE "public"."users" ("user_id" bigint NOT NULL, "username" character varying(50) NOT NULL, "email" character varying(100) NOT NULL, "password" character varying(255) NOT NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("user_id"), CONSTRAINT "users_email_key" UNIQUE ("email"), CONSTRAINT "users_username_key" UNIQUE ("username"));
CREATE TABLE "public"."users" ("user_id" bigint NOT NULL, "username" character varying(50) NOT NULL, "email" character varying(100) NOT NULL, "password" character varying(255) NOT NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, "is_admin" boolean NOT NULL, PRIMARY KEY ("user_id"), CONSTRAINT "users_email_key" UNIQUE ("email"), CONSTRAINT "users_username_key" UNIQUE ("username"));
-- Create "blogs" table
CREATE TABLE "public"."blogs" ("blog_id" bigint NOT NULL, "user_id" bigint NOT NULL, "title" character varying(255) NULL, "description" text NULL, "category_id" integer NULL, "created_at" timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ("blog_id"), CONSTRAINT "blogs_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "public"."categories" ("category_id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "blogs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("user_id") ON UPDATE NO ACTION ON DELETE CASCADE);
-- Create "posts" table

View File

@ -0,0 +1,20 @@
-- name: CreateBlogByUserId :one
INSERT INTO public.blogs
(blog_id, user_id, title, description, category_id, created_at)
VALUES($1, $2, $3, $4, $5, CURRENT_TIMESTAMP)
RETURNING *;
-- name: UpdateBlogInfoByBlogId :one
UPDATE public.blogs
SET title=$1, description=$2, category_id=$3
WHERE blog_id=$4
RETURNING *;
-- name: GetBlogsByUserId :many
SELECT *
FROM public.blogs
WHERE user_id = $1;
-- name: DeleteBlogByBlogId :exec
DELETE FROM public.blogs
WHERE blog_id=$1;

View File

@ -0,0 +1,16 @@
-- name: CreateCategory :one
INSERT INTO public.categories
(category_id, category_name)
VALUES($1, $2)
RETURNING *;
-- name: GetAllCategories :many
SELECT * FROM public.categories;
-- name: GetCategoryByName :one
SELECT * FROM public.categories WHERE category_name = $1;
-- name: DeleteCategoryById :exec
DELETE FROM public.categories
WHERE category_id=$1;

View File

@ -0,0 +1,25 @@
-- name: CreateProfileForUser :one
INSERT INTO public.profiles
(profile_id, user_id, bio, avatar_url, website_url)
VALUES($1, $2, '', '', '')
RETURNING *;
-- name: ClearProfileByUserId :one
UPDATE public.profiles
SET bio='', avatar_url='', website_url=''
WHERE user_id=$1
RETURNING *;
-- name: DeleteProfileByUserId :exec
DELETE FROM public.profiles
WHERE user_id=$1;
-- name: GetProfileByUserId :one
SELECT * FROM public.profiles WHERE user_id = $1;
-- name: UpdateProfileByUserId :one
UPDATE public.profiles
SET bio=$2, avatar_url=$3, website_url=$4
WHERE profile_id=$1
RETURNING *;

View File

@ -1,4 +0,0 @@
-- name: GetAllUsers :many
SELECT * FROM users;

View File

@ -0,0 +1,28 @@
-- name: GetAllUsers :many
SELECT * FROM users;
-- name: GetUserById :one
SELECT * FROM users WHERE user_id = $1;
-- name: GetUserByUsername :one
SELECT * FROM users WHERE username = $1;
-- name: CreateUser :one
INSERT INTO public.users
(user_id, username, email, "password", created_at, is_admin)
VALUES($1, $2, $3, $4, CURRENT_TIMESTAMP, false)
RETURNING *;
-- name: UpdateUserPasswordHash :one
UPDATE public.users
SET "password"=$1
WHERE user_id=$2
RETURNING *;
-- name: DeleteUserById :exec
DELETE FROM public.users
WHERE user_id=$1;
-- name: DeleteUserByUsername :exec
DELETE FROM public.users
WHERE username=$1;

View File

@ -7,7 +7,7 @@ sql:
go:
emit_json_tags: true
package: "db_repo"
out: "."
out: "./go_queries"
sql_package: "pgx/v5"
overrides:
- db_type: "uuid"

View File

@ -1,40 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: user_queries.sql
package db_repo
import (
"context"
)
const getAllUsers = `-- name: GetAllUsers :many
SELECT user_id, username, email, password, created_at FROM users
`
func (q *Queries) GetAllUsers(ctx context.Context) ([]User, error) {
rows, err := q.db.Query(ctx, getAllUsers)
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,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}

View File

@ -2,7 +2,7 @@ package main
import (
"context"
db_repo "enshi/db"
db_repo "enshi/db/go_queries"
"enshi/utils"
"fmt"
@ -34,6 +34,7 @@ func main() {
defer tx.Rollback(context.Background())
repo := db_repo.New(tx)
users, _ := repo.GetAllUsers(context.Background())
for _, user := range users {