ms-tester/internal/storage/language.go
2024-08-21 03:54:46 +05:00

81 lines
1.8 KiB
Go

package storage
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/models"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
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, handlePgErr(err)
}
defer rows.Close()
var id int32
err = rows.StructScan(&id)
if err != nil {
return 0, 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, 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 handlePgErr(err)
}
return nil
}
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 handlePgErr(err)
}
return nil
}