some update

This commit is contained in:
Pavel 2024-03-13 19:21:37 +03:00
parent 7f8de986a6
commit d59eb04dc6
12 changed files with 77 additions and 53 deletions

@ -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 = (

@ -0,0 +1,2 @@
ALTER TABLE answer DROP COLUMN IF EXISTS email;
DROP INDEX IF EXISTS answer_email_unique_idx;

@ -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 <> '';

@ -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 {

@ -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
}

2
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

@ -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)
}
}

@ -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: "день",
},
}
)

@ -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)

@ -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)

@ -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"
)

@ -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