feat: move language storage to array from postgres

This commit is contained in:
dragonmuffin 2024-08-25 17:15:41 +05:00
parent a8ba5677d3
commit 02a7a49ac3
4 changed files with 23 additions and 93 deletions

View file

@ -1,12 +1,13 @@
package models package models
import "time"
type Language struct { type Language struct {
Id *int32 `db:"id"` Name string
Name *string `db:"name"` CompileCmd []string //source: src;result:executable
BuildFileHash *string `db:"build_file_hash"` RunCmd []string //source: executable
ExecuteFileHash *string `db:"execute_file_hash"` }
CreatedAt *time.Time `db:"created_at"`
UpdatedAt *time.Time `db:"updated_at"` var languages = [...]Language {
{Name : "gcc std=c90",
CompileCmd : []string{"gcc", "src","-std=c90","-o","executable"},
RunCmd : []string{"executable"}},
} }

View file

@ -6,10 +6,7 @@ import (
) )
type LanguageStorage interface { type LanguageStorage interface {
CreateLanguage(ctx context.Context, language *models.Language) (int32, error)
ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error)
UpdateLanguage(ctx context.Context, language *models.Language) error
DeleteLanguage(ctx context.Context, id int32) error
} }
type LanguageService struct { type LanguageService struct {
@ -24,26 +21,8 @@ func NewLanguageService(
} }
} }
func (service *LanguageService) CreateLanguage(ctx context.Context, language *models.Language) (int32, error) {
//userId := ctx.Value("user_id").(int32)
panic("access control is not implemented yet")
return service.languageStorage.CreateLanguage(ctx, language)
}
func (service *LanguageService) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) { func (service *LanguageService) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) {
//userId := ctx.Value("user_id").(int32) //userId := ctx.Value("user_id").(int32)
panic("access control is not implemented yet") panic("access control is not implemented yet")
return service.languageStorage.ReadLanguageById(ctx, id) return service.languageStorage.ReadLanguageById(ctx, id)
} }
func (service *LanguageService) UpdateLanguage(ctx context.Context, language *models.Language) error {
//userId := ctx.Value("user_id").(int32)
panic("access control is not implemented yet")
return service.languageStorage.UpdateLanguage(ctx, language)
}
func (service *LanguageService) DeleteLanguage(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32)
panic("access control is not implemented yet")
return service.languageStorage.DeleteLanguage(ctx, id)
}

View file

@ -19,36 +19,6 @@ func NewLanguageStorage(db *sqlx.DB, logger *zap.Logger) *LanguageStorage {
} }
} }
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) { func (storage *LanguageStorage) ReadLanguageById(ctx context.Context, id int32) (*models.Language, error) {
var language models.Language var language models.Language
query := storage.db.Rebind("SELECT * from languages WHERE id=? LIMIT 1") query := storage.db.Rebind("SELECT * from languages WHERE id=? LIMIT 1")
@ -58,23 +28,3 @@ func (storage *LanguageStorage) ReadLanguageById(ctx context.Context, id int32)
} }
return &language, nil 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
}

View file

@ -1,16 +1,16 @@
-- +goose Up -- +goose Up
-- +goose StatementBegin -- +goose StatementBegin
CREATE TABLE IF NOT EXISTS languages --CREATE TABLE IF NOT EXISTS languages
( --(
id serial NOT NULL, -- id serial NOT NULL,
name VARCHAR(60) NOT NULL, -- name VARCHAR(60) NOT NULL,
build_file_hash CHAR(128) NULL, -- build_file_hash CHAR(128) NULL,
execute_file_hash CHAR(128) NULL, -- execute_file_hash CHAR(128) NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
--
PRIMARY KEY (id) -- PRIMARY KEY (id)
); --);
@ -123,7 +123,7 @@ CREATE TABLE IF NOT EXISTS solutions
id serial NOT NULL, id serial NOT NULL,
participant_id INT REFERENCES participants ON DELETE CASCADE, participant_id INT REFERENCES participants ON DELETE CASCADE,
task_id INT REFERENCES problems ON DELETE CASCADE, task_id INT REFERENCES problems ON DELETE CASCADE,
language_id INT REFERENCES languages ON DELETE CASCADE, language_id INT NOT NULL,REFERENCES languages ON DELETE CASCADE,
solution_hash CHAR(128) NOT NULL, solution_hash CHAR(128) NOT NULL,
result INT NOT NULL, result INT NOT NULL,
score INT NOT NULL, score INT NOT NULL,
@ -285,7 +285,7 @@ END;
$$; $$;
CREATE TRIGGER languages_upd_trg BEFORE UPDATE ON languages FOR EACH ROW EXECUTE FUNCTION updated_at_update(); --CREATE TRIGGER languages_upd_trg BEFORE UPDATE ON languages FOR EACH ROW EXECUTE FUNCTION updated_at_update();
CREATE TRIGGER problems_upd_trg BEFORE UPDATE ON problems FOR EACH ROW EXECUTE FUNCTION updated_at_update(); CREATE TRIGGER problems_upd_trg BEFORE UPDATE ON problems FOR EACH ROW EXECUTE FUNCTION updated_at_update();
CREATE TRIGGER contests_upd_trg BEFORE UPDATE ON contests FOR EACH ROW EXECUTE FUNCTION updated_at_update(); CREATE TRIGGER contests_upd_trg BEFORE UPDATE ON contests FOR EACH ROW EXECUTE FUNCTION updated_at_update();
CREATE TRIGGER users_upd_trg BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION updated_at_update(); CREATE TRIGGER users_upd_trg BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION updated_at_update();
@ -301,7 +301,7 @@ DROP FUNCTION IF EXISTS on_new_solution CASCADE;
DROP FUNCTION IF EXISTS on_new_subtaskrun CASCADE; DROP FUNCTION IF EXISTS on_new_subtaskrun CASCADE;
DROP TABLE IF EXISTS tests CASCADE; DROP TABLE IF EXISTS tests CASCADE;
DROP TABLE IF EXISTS solutions CASCADE; DROP TABLE IF EXISTS solutions CASCADE;
DROP TABLE IF EXISTS languages CASCADE; --DROP TABLE IF EXISTS languages CASCADE;
DROP TABLE IF EXISTS testgroups CASCADE; DROP TABLE IF EXISTS testgroups CASCADE;
DROP TABLE IF EXISTS testruns CASCADE; DROP TABLE IF EXISTS testruns CASCADE;
DROP TABLE IF EXISTS subtaskruns CASCADE; DROP TABLE IF EXISTS subtaskruns CASCADE;