generated from VLADIMIR/template
add scenarios methods
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package repos
|
||||
|
||||
import "time"
|
||||
|
||||
type Scenario struct {
|
||||
ID int
|
||||
Name string
|
||||
Description *string
|
||||
Image *string
|
||||
Scenario string
|
||||
Author *User
|
||||
Status string
|
||||
UpdatedAt time.Time
|
||||
CreatedAt time.Time
|
||||
PublishedAt *time.Time
|
||||
IsDeleted bool
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package scenarios_repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/repos"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrScenarioNotFound = errors.New("Сценарий не найден")
|
||||
)
|
||||
|
||||
type ScenariosRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewScenariosRepo(pool *pgxpool.Pool) *ScenariosRepo {
|
||||
return &ScenariosRepo{
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) AddScenario(
|
||||
ctx context.Context,
|
||||
name string,
|
||||
authorId int,
|
||||
) (int, error) {
|
||||
id := 0
|
||||
err := s.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO scenarios (name, author_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`,
|
||||
name,
|
||||
authorId,
|
||||
).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetScenariosByAuthorID(
|
||||
ctx context.Context,
|
||||
authorId int,
|
||||
) ([]*repos.Scenario, error) {
|
||||
rows, err := s.pool.Query(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenarios.id,
|
||||
scenarios.name,
|
||||
scenarios.description,
|
||||
scenarios.image,
|
||||
scenarios.scenario,
|
||||
users.id,
|
||||
users.username,
|
||||
scenarios.status,
|
||||
scenarios.updated_at,
|
||||
scenarios.created_at,
|
||||
scenarios.published_at
|
||||
FROM scenarios
|
||||
LEFT JOIN users on scenarios.author_id = users.id
|
||||
WHERE scenarios.author_id = $1 and is_deleted = FALSE
|
||||
ORDER BY created_at DESC`,
|
||||
authorId,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var scenarios []*repos.Scenario
|
||||
for rows.Next() {
|
||||
scenario := &repos.Scenario{
|
||||
Author: &repos.User{},
|
||||
}
|
||||
err := rows.Scan(
|
||||
&scenario.ID,
|
||||
&scenario.Name,
|
||||
&scenario.Description,
|
||||
&scenario.Image,
|
||||
&scenario.Scenario,
|
||||
&scenario.Author.ID,
|
||||
&scenario.Author.Username,
|
||||
&scenario.Status,
|
||||
&scenario.UpdatedAt,
|
||||
&scenario.CreatedAt,
|
||||
&scenario.PublishedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scenarios = append(scenarios, scenario)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return scenarios, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) GetScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) (*repos.Scenario, error) {
|
||||
scenario := &repos.Scenario{
|
||||
Author: &repos.User{},
|
||||
}
|
||||
row := s.pool.QueryRow(
|
||||
ctx,
|
||||
`SELECT
|
||||
scenarios.id,
|
||||
scenarios.name,
|
||||
scenarios.description,
|
||||
scenarios.image,
|
||||
scenarios.scenario,
|
||||
users.id,
|
||||
users.username,
|
||||
scenarios.status,
|
||||
scenarios.updated_at,
|
||||
scenarios.created_at,
|
||||
scenarios.published_at,
|
||||
scenarios.is_deleted
|
||||
FROM scenarios
|
||||
LEFT JOIN users on scenarios.author_id = users.id
|
||||
WHERE scenarios.id = $1`,
|
||||
id,
|
||||
)
|
||||
err := row.Scan(
|
||||
&scenario.ID,
|
||||
&scenario.Name,
|
||||
&scenario.Description,
|
||||
&scenario.Image,
|
||||
&scenario.Scenario,
|
||||
&scenario.Author.ID,
|
||||
&scenario.Author.Username,
|
||||
&scenario.Status,
|
||||
&scenario.UpdatedAt,
|
||||
&scenario.CreatedAt,
|
||||
&scenario.PublishedAt,
|
||||
&scenario.IsDeleted,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrScenarioNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return scenario, nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) UpdateScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
name string,
|
||||
description string,
|
||||
image string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
name = $1,
|
||||
description = $2,
|
||||
image = $3,
|
||||
updated_at = NOW()
|
||||
WHERE id = $4`,
|
||||
name,
|
||||
description,
|
||||
image,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrScenarioNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ScenariosRepo) DeleteScenarioByID(
|
||||
ctx context.Context,
|
||||
id int,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE scenarios
|
||||
SET
|
||||
is_deleted = TRUE
|
||||
WHERE id = $1`,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrScenarioNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -169,7 +169,6 @@ func (s *UsersRepo) GetUsers(
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user