ms-tester/internal/storage/language.go

81 lines
1.8 KiB
Go
Raw Normal View History

2024-08-18 14:04:33 +00:00
package storage
import (
"context"
2024-08-20 22:54:46 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/models"
2024-08-18 14:04:33 +00:00
"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,
2024-08-20 22:54:46 +00:00
"", //FIXME
"",
2024-08-18 14:04:33 +00:00
)
if err != nil {
2024-08-20 22:54:46 +00:00
return 0, handlePgErr(err)
2024-08-18 14:04:33 +00:00
}
defer rows.Close()
var id int32
err = rows.StructScan(&id)
if err != nil {
2024-08-20 22:54:46 +00:00
return 0, handlePgErr(err)
2024-08-18 14:04:33 +00:00
}
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 {
2024-08-20 22:54:46 +00:00
return nil, handlePgErr(err)
2024-08-18 14:04:33 +00:00
}
return &language, nil
}
2024-08-20 22:54:46 +00:00
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
2024-08-18 14:04:33 +00:00
_, err := storage.db.ExecContext(ctx, query, language.Name)
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
2024-08-18 14:04:33 +00:00
}
2024-08-20 22:54:46 +00:00
return nil
2024-08-18 14:04:33 +00:00
}
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 {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
2024-08-18 14:04:33 +00:00
}
return nil
}