From d59eb04dc60673a14d622e4d5c21325691f9885c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 13 Mar 2024 19:21:37 +0300 Subject: [PATCH] some update --- dal/db_query/queries.sql | 19 +++++++----- dal/schema/000005_init.down.sql | 2 ++ dal/schema/000005_init.up.sql | 2 ++ dal/sqlcgen/models.go | 1 + dal/sqlcgen/queries.sql.go | 53 ++++++++++++++++++++++----------- go.mod | 2 +- healthchecks/healthchecks.go | 18 ----------- model/model.go | 9 ++++++ repository/account/account.go | 20 ++++++------- repository/answer/answer.go | 1 + repository/quiz/quiz.go | 1 + sqlc.yaml | 2 ++ 12 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 dal/schema/000005_init.down.sql create mode 100644 dal/schema/000005_init.up.sql delete mode 100644 healthchecks/healthchecks.go diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 78393e1..79444a0 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -118,11 +118,15 @@ SELECT id,privilegeID,privilege_name,amount, created_at FROM privileges WHERE ac -- name: GetAccountWithPrivileges :many SELECT a.id, a.user_id, a.created_at, a.deleted, - p.id AS privilege_id, p.privilegeID, p.privilege_name, p.amount, p.created_at AS privilege_created_at + coalesce(p.id,0) AS privilege_id, + coalesce(p.privilegeID,''), + coalesce(p.privilege_name,''), + coalesce(p.amount,0), coalesce(p.created_at,Now()) AS privilege_created_at FROM account a LEFT JOIN privileges AS p ON a.id = p.account_id WHERE a.user_id = $1; + -- name: GetPrivilegesQuizAccount :many SELECT p.privilegeID, @@ -249,10 +253,10 @@ SELECT a.user_id, a.email, a.created_at, - p.ID, - p.privilegeid, - p.amount, - p.created_at + COALESCE(p.ID,0), + coalesce(p.privilegeid,''), + coalesce(p.amount,0), + coalesce(p.created_at,Now()) FROM account AS a LEFT JOIN privileges AS p ON a.id = p.account_id @@ -287,8 +291,9 @@ INSERT INTO answer( question_id, fingerprint, session, - result -) VALUES ($1,$2,$3,$4,$5,$6); + result, + email +) VALUES ($1,$2,$3,$4,$5,$6,$7); -- name: GetResultAnswers :many SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted FROM answer WHERE session = ( diff --git a/dal/schema/000005_init.down.sql b/dal/schema/000005_init.down.sql new file mode 100644 index 0000000..ccae4f5 --- /dev/null +++ b/dal/schema/000005_init.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE answer DROP COLUMN IF EXISTS email; +DROP INDEX IF EXISTS answer_email_unique_idx; \ No newline at end of file diff --git a/dal/schema/000005_init.up.sql b/dal/schema/000005_init.up.sql new file mode 100644 index 0000000..f2bfe97 --- /dev/null +++ b/dal/schema/000005_init.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE answer ADD COLUMN email VARCHAR(50) NOT NULL DEFAULT ''; +CREATE UNIQUE INDEX IF NOT EXISTS answer_email_unique_idx ON answer (quiz_id, email) WHERE email <> ''; \ No newline at end of file diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 4eae354..ada2cfc 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -29,6 +29,7 @@ type Answer struct { Result sql.NullBool `db:"result" json:"result"` New sql.NullBool `db:"new" json:"new"` Deleted sql.NullBool `db:"deleted" json:"deleted"` + Email string `db:"email" json:"email"` } type Privilege struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fe88502..20443b8 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -381,10 +381,10 @@ SELECT a.user_id, a.email, a.created_at, - p.ID, - p.privilegeid, - p.amount, - p.created_at + COALESCE(p.ID,0), + coalesce(p.privilegeid,''), + coalesce(p.amount,0), + coalesce(p.created_at,Now()) FROM account AS a LEFT JOIN privileges AS p ON a.id = p.account_id @@ -397,9 +397,9 @@ type GetAccAndPrivilegeByEmailRow struct { UserID sql.NullString `db:"user_id" json:"user_id"` Email sql.NullString `db:"email" json:"email"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - ID_2 sql.NullInt32 `db:"id_2" json:"id_2"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - Amount sql.NullInt32 `db:"amount" json:"amount"` + ID_2 int32 `db:"id_2" json:"id_2"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + Amount int32 `db:"amount" json:"amount"` CreatedAt_2 sql.NullTime `db:"created_at_2" json:"created_at_2"` } @@ -421,7 +421,10 @@ func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID sql.Null const getAccountWithPrivileges = `-- name: GetAccountWithPrivileges :many SELECT a.id, a.user_id, a.created_at, a.deleted, - p.id AS privilege_id, p.privilegeID, p.privilege_name, p.amount, p.created_at AS privilege_created_at + coalesce(p.id,0) AS privilege_id, + coalesce(p.privilegeID,''), + coalesce(p.privilege_name,''), + coalesce(p.amount,0), coalesce(p.created_at,Now()) AS privilege_created_at FROM account a LEFT JOIN privileges AS p ON a.id = p.account_id WHERE a.user_id = $1 @@ -432,10 +435,10 @@ type GetAccountWithPrivilegesRow struct { UserID sql.NullString `db:"user_id" json:"user_id"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` Deleted sql.NullBool `db:"deleted" json:"deleted"` - PrivilegeID sql.NullInt32 `db:"privilege_id" json:"privilege_id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` + PrivilegeID int32 `db:"privilege_id" json:"privilege_id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` PrivilegeCreatedAt sql.NullTime `db:"privilege_created_at" json:"privilege_created_at"` } @@ -985,15 +988,28 @@ SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC ` -func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]Answer, error) { +type GetResultAnswersRow struct { + ID int64 `db:"id" json:"id"` + Content sql.NullString `db:"content" json:"content"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + QuestionID int64 `db:"question_id" json:"question_id"` + Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"` + Session sql.NullString `db:"session" json:"session"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + Result sql.NullBool `db:"result" json:"result"` + New sql.NullBool `db:"new" json:"new"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` +} + +func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) { rows, err := q.db.QueryContext(ctx, getResultAnswers, id) if err != nil { return nil, err } defer rows.Close() - var items []Answer + var items []GetResultAnswersRow for rows.Next() { - var i Answer + var i GetResultAnswersRow if err := rows.Scan( &i.ID, &i.Content, @@ -1026,8 +1042,9 @@ INSERT INTO answer( question_id, fingerprint, session, - result -) VALUES ($1,$2,$3,$4,$5,$6) + result, + email +) VALUES ($1,$2,$3,$4,$5,$6,$7) ` type InsertAnswersParams struct { @@ -1037,6 +1054,7 @@ type InsertAnswersParams struct { Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"` Session sql.NullString `db:"session" json:"session"` Result sql.NullBool `db:"result" json:"result"` + Email string `db:"email" json:"email"` } func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error { @@ -1047,6 +1065,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) er arg.Fingerprint, arg.Session, arg.Result, + arg.Email, ) return err } diff --git a/go.mod b/go.mod index 815faf9..ad968e2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module penahub.gitlab.yandexcloud.net/backend/quiz/common.git go 1.21.4 require ( - github.com/gofiber/fiber/v2 v2.52.0 github.com/golang-migrate/migrate/v4 v4.17.0 github.com/golang/protobuf v1.5.3 github.com/google/uuid v1.6.0 @@ -15,6 +14,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect + github.com/gofiber/fiber/v2 v2.52.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/klauspost/compress v1.17.0 // indirect diff --git a/healthchecks/healthchecks.go b/healthchecks/healthchecks.go deleted file mode 100644 index ad0a945..0000000 --- a/healthchecks/healthchecks.go +++ /dev/null @@ -1,18 +0,0 @@ -package healthchecks - -import ( - "github.com/gofiber/fiber/v2" -) - -func Liveness(c *fiber.Ctx) error { - return c.SendStatus(fiber.StatusOK) -} - -func Readiness(err *error) fiber.Handler { - return func(c *fiber.Ctx) error { - if *err != nil { - return c.SendString((*err).Error()) - } - return c.SendStatus(fiber.StatusOK) - } -} diff --git a/model/model.go b/model/model.go index 6c15df9..b2c131f 100644 --- a/model/model.go +++ b/model/model.go @@ -122,6 +122,7 @@ type Answer struct { CreatedAt time.Time New bool `json:"new"` Deleted bool + Email string } type ResultContent struct { @@ -167,6 +168,14 @@ var ( Type: "day", Value: "день", }, + { + PrivilegeID: "squizHideBadge", + Name: "Скрытие шильдика в опроснике", + ServiceKey: skey, + Description: "Количество дней скрытия шильдика в опроснике", + Type: "day", + Value: "день", + }, } ) diff --git a/repository/account/account.go b/repository/account/account.go index e74b218..1d93013 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -51,15 +51,15 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) ( account.Deleted = row.Deleted.Bool } - if row.PrivilegeID.Valid { + if row.PrivilegeID != 0 { privilege := model.ShortPrivilege{ - ID: fmt.Sprintf("%d", row.PrivilegeID.Int32), - PrivilegeID: row.Privilegeid.String, - PrivilegeName: row.PrivilegeName.String, - Amount: uint64(row.Amount.Int32), + ID: fmt.Sprintf("%d", row.PrivilegeID), + PrivilegeID: row.Privilegeid, + PrivilegeName: row.PrivilegeName, + Amount: uint64(row.Amount), CreatedAt: row.PrivilegeCreatedAt.Time, } - privileges[privilege.PrivilegeName] = privilege + privileges[privilege.PrivilegeID] = privilege } } @@ -295,11 +295,11 @@ func (r *AccountRepository) GetAccAndPrivilegeByEmail(ctx context.Context, email account.Email = row.Email.String account.CreatedAt = row.CreatedAt.Time - if row.ID_2.Valid { + if row.ID_2 != 0 { privilege := model.ShortPrivilege{ - ID: fmt.Sprint(row.ID_2.Int32), - PrivilegeID: row.Privilegeid.String, - Amount: uint64(row.Amount.Int32), + ID: fmt.Sprint(row.ID_2), + PrivilegeID: row.Privilegeid, + Amount: uint64(row.Amount), CreatedAt: row.CreatedAt_2.Time, } privileges = append(privileges, privilege) diff --git a/repository/answer/answer.go b/repository/answer/answer.go index 524f526..dcd37bc 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -44,6 +44,7 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An Fingerprint: sql.NullString{String: fp, Valid: true}, Session: sql.NullString{String: session, Valid: true}, Result: sql.NullBool{Bool: ans.Result, Valid: true}, + Email: ans.Email, } err := r.queries.InsertAnswers(ctx, params) diff --git a/repository/quiz/quiz.go b/repository/quiz/quiz.go index 2583d0c..9df6892 100644 --- a/repository/quiz/quiz.go +++ b/repository/quiz/quiz.go @@ -10,6 +10,7 @@ import ( "github.com/lib/pq" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "strings" "sync" ) diff --git a/sqlc.yaml b/sqlc.yaml index 6b159e2..ad50b35 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -12,6 +12,8 @@ packages: - "./dal/schema/000003_init.down.sql" - "./dal/schema/000004_init.up.sql" - "./dal/schema/000004_init.down.sql" + - "./dal/schema/000005_init.up.sql" + - "./dal/schema/000005_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true