From 2793b50830b6e16f770b25045415ae0977d93e6a Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Sun, 18 Aug 2024 19:04:33 +0500 Subject: [PATCH] feat: language storage --- internal/storage/language.go | 84 ++++++++++++++++++++++++++++++++++++ todo.md | 1 + 2 files changed, 85 insertions(+) create mode 100644 internal/storage/language.go diff --git a/internal/storage/language.go b/internal/storage/language.go new file mode 100644 index 0000000..e88ce9e --- /dev/null +++ b/internal/storage/language.go @@ -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 +} + diff --git a/todo.md b/todo.md index e4e1b3f..81c7b55 100644 --- a/todo.md +++ b/todo.md @@ -8,6 +8,7 @@ ## in future: +* add problem notes * add interactive and run-twice problems * add ability to change testset * add public contests