85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"git.sch9.ru/new_gate/ms-auth/internal/lib"
|
||
|
"git.sch9.ru/new_gate/ms-auth/internal/models"
|
||
|
"github.com/jackc/pgerrcode"
|
||
|
"github.com/jackc/pgx/v5/pgconn"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
"go.uber.org/zap"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type LanguageStorage struct {
|
||
|
db *sqlx.DB
|
||
|
logger *zap.Logger
|
||
|
}
|
||
|
|
||
|
func NewLanguageStorage(db *sqlx.DB, logger *zap.Logger) *LanguageStorage {
|
||
|
return &LanguageStorage{
|
||
|
db: db,
|
||
|
logger: logger,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (storage *LanguageStorage) CreateLanguage(ctx context.Context, language *models.Language) (int32, error) {
|
||
|
query := storage.db.Rebind(`
|
||
|
INSERT INTO languages
|
||
|
(name,build_file_hash,execute_file_hash)
|
||
|
VALUES (?, ?, ?)
|
||
|
RETURNING id
|
||
|
`)
|
||
|
|
||
|
rows, err := storage.db.QueryxContext(
|
||
|
ctx,
|
||
|
query,
|
||
|
language.Name,
|
||
|
"",//FIXME
|
||
|
""
|
||
|
)
|
||
|
if err != nil {
|
||
|
return 0, storage.HandlePgErr(err)
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
var id int32
|
||
|
err = rows.StructScan(&id)
|
||
|
if err != nil {
|
||
|
return 0, storage.HandlePgErr(err)
|
||
|
}
|
||
|
|
||
|
return id, nil
|
||
|
|
||
|
}
|
||
|
|
||
|
func (storage *LanguageStorage) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) {
|
||
|
var language models.Language
|
||
|
query := storage.db.Rebind("SELECT * from languages WHERE id=? LIMIT 1")
|
||
|
err := storage.db.GetContext(ctx, &language, query, id)
|
||
|
if err != nil {
|
||
|
return nil, storage.HandlePgErr(err)
|
||
|
}
|
||
|
return &language, nil
|
||
|
}
|
||
|
|
||
|
func (storage *LanguageStorage) UpdateLanguage(ctx context.Context, id int32,language models.Language) error {
|
||
|
query := storage.db.Rebind("UPDATE languages SET name=?")//FIXME add build file and execute file loading
|
||
|
_, err := storage.db.ExecContext(ctx, query, language.Name)
|
||
|
if err != nil {
|
||
|
return storage.HandlePgErr(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (storage *LanguageStorage) DeleteLanguage(ctx context.Context, id int32) error {
|
||
|
query := storage.db.Rebind("DELETE FROM languages WHERE id=?")
|
||
|
_, err := storage.db.ExecContext(ctx, query, id)
|
||
|
if err != nil {
|
||
|
return storage.HandlePgErr(err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|