feat: language storage
This commit is contained in:
parent
2504e278a3
commit
2793b50830
2 changed files with 85 additions and 0 deletions
84
internal/storage/language.go
Normal file
84
internal/storage/language.go
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
1
todo.md
1
todo.md
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
## in future:
|
## in future:
|
||||||
|
|
||||||
|
* add problem notes
|
||||||
* add interactive and run-twice problems
|
* add interactive and run-twice problems
|
||||||
* add ability to change testset
|
* add ability to change testset
|
||||||
* add public contests
|
* add public contests
|
||||||
|
|
Loading…
Reference in a new issue