From bff19f2ddb989c0a2e0c10dd2097481fab9b1b0e Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Sun, 18 Aug 2024 19:54:08 +0500 Subject: [PATCH] feat: contest and participant storage --- internal/storage/contests.go | 82 +++++++++++++++++++++++++++++++ internal/storage/participants.go | 84 ++++++++++++++++++++++++++++++++ internal/storage/solution.go | 1 + 3 files changed, 167 insertions(+) create mode 100644 internal/storage/contests.go create mode 100644 internal/storage/participants.go diff --git a/internal/storage/contests.go b/internal/storage/contests.go new file mode 100644 index 0000000..3891b9f --- /dev/null +++ b/internal/storage/contests.go @@ -0,0 +1,82 @@ +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 ContestStorage struct { + db *sqlx.DB + logger *zap.Logger +} + +func NewContestStorage(db *sqlx.DB, logger *zap.Logger) *ContestStorage { + return &ContestStorage{ + db: db, + logger: logger, + } +} + +func (storage *ContestStorage) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) { + query := storage.db.Rebind(` +INSERT INTO contests + (name) +VALUES (?) +RETURNING id +`) + + rows, err := storage.db.QueryxContext( + ctx, + query, + contest.Name + ) + 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 *ContestStorage) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) { + var contest models.Contest + query := storage.db.Rebind("SELECT * from contests WHERE id=? LIMIT 1") + err := storage.db.GetContext(ctx, &contest, query, id) + if err != nil { + return nil, storage.HandlePgErr(err) + } + return &contest, nil +} + +func (storage *ContestStorage) UpdateContest(ctx context.Context, id int32,contest models.Contest) error { + query := storage.db.Rebind("UPDATE contests SET name=?") + _, err := storage.db.ExecContext(ctx, query, contest.Name) + if err != nil { + return storage.HandlePgErr(err) + } +} + +func (storage *ContestStorage) DeleteContest(ctx context.Context, id int32) error { + query := storage.db.Rebind("DELETE FROM contests WHERE id=?") + _, err := storage.db.ExecContext(ctx, query, id) + if err != nil { + return storage.HandlePgErr(err) + } + + return nil +} + diff --git a/internal/storage/participants.go b/internal/storage/participants.go new file mode 100644 index 0000000..c328fe9 --- /dev/null +++ b/internal/storage/participants.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 ParticipantStorage struct { + db *sqlx.DB + logger *zap.Logger +} + +func NewParticipantStorage(db *sqlx.DB, logger *zap.Logger) *ParticipantStorage { + return &ParticipantStorage{ + db: db, + logger: logger, + } +} + +func (storage *ParticipantStorage) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) { + query := storage.db.Rebind(` +INSERT INTO participants + (user_id,contest_id,name) +VALUES (?,?,?) +RETURNING id +`) + + rows, err := storage.db.QueryxContext( + ctx, + query, + participant.UserId, + participant.ContestId, + participant.Name + ) + 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 *ParticipantStorage) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) { + var participant models.Participant + query := storage.db.Rebind("SELECT * from participants WHERE id=? LIMIT 1") + err := storage.db.GetContext(ctx, &participant, query, id) + if err != nil { + return nil, storage.HandlePgErr(err) + } + return &participant, nil +} + +func (storage *ParticipantStorage) UpdateParticipant(ctx context.Context, id int32,participant models.Participant) error { + query := storage.db.Rebind("UPDATE participants SET name=?") + _, err := storage.db.ExecContext(ctx, query, participant.Name) + if err != nil { + return storage.HandlePgErr(err) + } +} + +func (storage *ParticipantStorage) DeleteParticipant(ctx context.Context, id int32) error { + query := storage.db.Rebind("DELETE FROM participants WHERE id=?") + _, err := storage.db.ExecContext(ctx, query, id) + if err != nil { + return storage.HandlePgErr(err) + } + + return nil +} + diff --git a/internal/storage/solution.go b/internal/storage/solution.go index 6360f7d..6b97a45 100644 --- a/internal/storage/solution.go +++ b/internal/storage/solution.go @@ -58,6 +58,7 @@ RETURNING id "",//FIXME models.NotTested, ) + //TODO: add testing tree if err != nil { return 0, storage.HandlePgErr(err) }