feat: add permission check

This commit is contained in:
Vyacheslav1557 2024-08-20 16:18:23 +05:00
parent 56135ff5df
commit bebc7f3076
14 changed files with 490 additions and 95 deletions

View file

@ -2,14 +2,9 @@ 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"
"git.sch9.ru/new_gate/ms-tester/internal/models"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"time"
)
type ContestStorage struct {
@ -35,17 +30,17 @@ RETURNING id
rows, err := storage.db.QueryxContext(
ctx,
query,
contest.Name
contest.Name,
)
if err != nil {
return 0, storage.HandlePgErr(err)
return 0, handlePgErr(err)
}
defer rows.Close()
var id int32
err = rows.StructScan(&id)
if err != nil {
return 0, storage.HandlePgErr(err)
return 0, handlePgErr(err)
}
return id, nil
@ -57,16 +52,16 @@ func (storage *ContestStorage) ReadContestById(ctx context.Context, id int32) (*
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 nil, handlePgErr(err)
}
return &contest, nil
}
func (storage *ContestStorage) UpdateContest(ctx context.Context, id int32,contest models.Contest) error {
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)
return handlePgErr(err)
}
}
@ -74,9 +69,8 @@ func (storage *ContestStorage) DeleteContest(ctx context.Context, id int32) erro
query := storage.db.Rebind("DELETE FROM contests WHERE id=?")
_, err := storage.db.ExecContext(ctx, query, id)
if err != nil {
return storage.HandlePgErr(err)
return handlePgErr(err)
}
return nil
}

View file

@ -1,27 +1,21 @@
package storage
import (
"context"
"errors"
"git.sch9.ru/new_gate/ms-auth/internal/lib"
"git.sch9.ru/new_gate/ms-auth/internal/models"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"time"
)
func (storage *UserStorage) HandlePgErr(err error) error {
func handlePgErr(err error) error {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
storage.logger.DPanic("unexpected error from postgres", zap.String("err", err.Error()))
//storage.logger.DPanic("unexpected error from postgres", zap.String("err", err.Error()))
return lib.ErrUnexpected
}
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
return errors.New("unique key violation") // FIXME
}
storage.logger.DPanic("unexpected internal error from postgres", zap.String("err", err.Error()))
//storage.logger.DPanic("unexpected internal error from postgres", zap.String("err", err.Error()))
return lib.ErrInternal
}

42
internal/storage/user.go Normal file
View file

@ -0,0 +1,42 @@
package storage
import (
"context"
"git.sch9.ru/new_gate/ms-tester/internal/models"
"github.com/jmoiron/sqlx"
)
type UserStorage struct {
db *sqlx.DB
}
func NewUserStorage(db *sqlx.DB) *UserStorage {
return &UserStorage{
db: db,
}
}
func (storage *UserStorage) CreateUser(ctx context.Context, user *models.User) error {
query := storage.db.Rebind("INSERT INTO users (user_id, role) VALUES (?, ?)")
_, err := storage.db.ExecContext(ctx, query, user.UserId, user.Role)
if err != nil {
return err
}
return nil
}
func (storage *UserStorage) ReadUserById(ctx context.Context, userId int32) (*models.User, error) {
query := storage.db.Rebind(`
SELECT *
FROM users
WHERE user_id = ?
LIMIT 1;
`)
var user models.User
err := storage.db.GetContext(ctx, &user, query, userId)
if err != nil {
return nil, err
}
return &user, nil
}