added logic with quiz_privilege_usage
This commit is contained in:
parent
a61f264ccf
commit
e5a305490f
@ -1475,3 +1475,19 @@ INSERT into quiz_utm (quizID,utm) values ($1,$2) RETURNING *;
|
|||||||
|
|
||||||
-- name: SoftDeleteQuizUtm :exec
|
-- name: SoftDeleteQuizUtm :exec
|
||||||
UPDATE quiz_utm set deleted = true where id = $1;
|
UPDATE quiz_utm set deleted = true where id = $1;
|
||||||
|
|
||||||
|
-- name: GetQuizPrivilegeUsage :one
|
||||||
|
SELECT * FROM quiz_privilege_usage
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3 LIMIT 1;
|
||||||
|
|
||||||
|
-- name: InsertQuizPrivilegeUsage :one
|
||||||
|
INSERT INTO quiz_privilege_usage (quiz_id, privilege_id, privilege_id_str, used_count)
|
||||||
|
VALUES ($1, $2, $3, 1) RETURNING *;
|
||||||
|
|
||||||
|
-- name: IncrementQuizPrivilegeUsage :exec
|
||||||
|
UPDATE quiz_privilege_usage SET used_count = used_count + 1, updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3;
|
||||||
|
|
||||||
|
-- name: ResetQuizPrivilegeUsageCount :exec
|
||||||
|
UPDATE quiz_privilege_usage SET used_count = 0, updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3;
|
2
dal/schema/000026_init.down.sql
Normal file
2
dal/schema/000026_init.down.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
DROP INDEX IF EXISTS idx_quiz_privilege_unique;
|
||||||
|
drop table if exists quiz_privilege_usage;
|
11
dal/schema/000026_init.up.sql
Normal file
11
dal/schema/000026_init.up.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
CREATE TABLE quiz_privilege_usage (
|
||||||
|
id bigserial UNIQUE NOT NULL PRIMARY KEY,
|
||||||
|
quiz_id BIGINT NOT NULL REFERENCES quiz(id) ON DELETE CASCADE,
|
||||||
|
privilege_id BIGINT NOT NULL REFERENCES privilege(id) ON DELETE CASCADE,
|
||||||
|
privilege_id_str VARCHAR(50) NOT NULL,
|
||||||
|
used_count INT NOT NULL DEFAULT 0,
|
||||||
|
created_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX idx_quiz_privilege_unique ON quiz_privilege_usage (quiz_id, privilege_id, privilege_id_str);
|
@ -259,6 +259,16 @@ type Quiz struct {
|
|||||||
Gigachat bool `db:"gigachat" json:"gigachat"`
|
Gigachat bool `db:"gigachat" json:"gigachat"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QuizPrivilegeUsage struct {
|
||||||
|
ID int64 `db:"id" json:"id"`
|
||||||
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `db:"privilege_id" json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `db:"privilege_id_str" json:"privilege_id_str"`
|
||||||
|
UsedCount int32 `db:"used_count" json:"used_count"`
|
||||||
|
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||||
|
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type QuizUtm struct {
|
type QuizUtm struct {
|
||||||
ID int64 `db:"id" json:"id"`
|
ID int64 `db:"id" json:"id"`
|
||||||
Quizid int64 `db:"quizid" json:"quizid"`
|
Quizid int64 `db:"quizid" json:"quizid"`
|
||||||
|
@ -3450,6 +3450,32 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams)
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getQuizPrivilegeUsage = `-- name: GetQuizPrivilegeUsage :one
|
||||||
|
SELECT id, quiz_id, privilege_id, privilege_id_str, used_count, created_at, updated_at FROM quiz_privilege_usage
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3 LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetQuizPrivilegeUsageParams struct {
|
||||||
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `db:"privilege_id" json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `db:"privilege_id_str" json:"privilege_id_str"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetQuizPrivilegeUsage(ctx context.Context, arg GetQuizPrivilegeUsageParams) (QuizPrivilegeUsage, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, getQuizPrivilegeUsage, arg.QuizID, arg.PrivilegeID, arg.PrivilegeIDStr)
|
||||||
|
var i QuizPrivilegeUsage
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.QuizID,
|
||||||
|
&i.PrivilegeID,
|
||||||
|
&i.PrivilegeIDStr,
|
||||||
|
&i.UsedCount,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const getQuizRule = `-- name: GetQuizRule :one
|
const getQuizRule = `-- name: GetQuizRule :one
|
||||||
SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat, tagstoadd FROM rules WHERE QuizID = $1 AND AccountID = (SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $2 AND accountsAmo.Deleted = false) AND Deleted = false
|
SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat, tagstoadd FROM rules WHERE QuizID = $1 AND AccountID = (SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $2 AND accountsAmo.Deleted = false) AND Deleted = false
|
||||||
`
|
`
|
||||||
@ -4372,6 +4398,22 @@ func (q *Queries) GettingBitrixUsersTrueResults(ctx context.Context) ([]GettingB
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const incrementQuizPrivilegeUsage = `-- name: IncrementQuizPrivilegeUsage :exec
|
||||||
|
UPDATE quiz_privilege_usage SET used_count = used_count + 1, updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
type IncrementQuizPrivilegeUsageParams struct {
|
||||||
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `db:"privilege_id" json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `db:"privilege_id_str" json:"privilege_id_str"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) IncrementQuizPrivilegeUsage(ctx context.Context, arg IncrementQuizPrivilegeUsageParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, incrementQuizPrivilegeUsage, arg.QuizID, arg.PrivilegeID, arg.PrivilegeIDStr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const insertAnswers = `-- name: InsertAnswers :one
|
const insertAnswers = `-- name: InsertAnswers :one
|
||||||
INSERT INTO answer(
|
INSERT INTO answer(
|
||||||
content,
|
content,
|
||||||
@ -4654,6 +4696,32 @@ func (q *Queries) InsertQuiz(ctx context.Context, arg InsertQuizParams) (InsertQ
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const insertQuizPrivilegeUsage = `-- name: InsertQuizPrivilegeUsage :one
|
||||||
|
INSERT INTO quiz_privilege_usage (quiz_id, privilege_id, privilege_id_str, used_count)
|
||||||
|
VALUES ($1, $2, $3, 1) RETURNING id, quiz_id, privilege_id, privilege_id_str, used_count, created_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type InsertQuizPrivilegeUsageParams struct {
|
||||||
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `db:"privilege_id" json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `db:"privilege_id_str" json:"privilege_id_str"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) InsertQuizPrivilegeUsage(ctx context.Context, arg InsertQuizPrivilegeUsageParams) (QuizPrivilegeUsage, error) {
|
||||||
|
row := q.db.QueryRowContext(ctx, insertQuizPrivilegeUsage, arg.QuizID, arg.PrivilegeID, arg.PrivilegeIDStr)
|
||||||
|
var i QuizPrivilegeUsage
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.QuizID,
|
||||||
|
&i.PrivilegeID,
|
||||||
|
&i.PrivilegeIDStr,
|
||||||
|
&i.UsedCount,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const moveToHistory = `-- name: MoveToHistory :one
|
const moveToHistory = `-- name: MoveToHistory :one
|
||||||
INSERT INTO question(
|
INSERT INTO question(
|
||||||
quiz_id, title, description, questiontype, required,
|
quiz_id, title, description, questiontype, required,
|
||||||
@ -4895,6 +4963,22 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resetQuizPrivilegeUsageCount = `-- name: ResetQuizPrivilegeUsageCount :exec
|
||||||
|
UPDATE quiz_privilege_usage SET used_count = 0, updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE quiz_id = $1 AND privilege_id = $2 AND privilege_id_str = $3
|
||||||
|
`
|
||||||
|
|
||||||
|
type ResetQuizPrivilegeUsageCountParams struct {
|
||||||
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `db:"privilege_id" json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `db:"privilege_id_str" json:"privilege_id_str"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ResetQuizPrivilegeUsageCount(ctx context.Context, arg ResetQuizPrivilegeUsageCountParams) error {
|
||||||
|
_, err := q.db.ExecContext(ctx, resetQuizPrivilegeUsageCount, arg.QuizID, arg.PrivilegeID, arg.PrivilegeIDStr)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const searchIDByAppIDanAppHash = `-- name: SearchIDByAppIDanAppHash :one
|
const searchIDByAppIDanAppHash = `-- name: SearchIDByAppIDanAppHash :one
|
||||||
SELECT id, apiid, apihash, phonenumber, password, status, deleted, createdat FROM tgAccounts WHERE ApiID = $1 and ApiHash=$2 and Deleted = false
|
SELECT id, apiid, apihash, phonenumber, password, status, deleted, createdat FROM tgAccounts WHERE ApiID = $1 and ApiHash=$2 and Deleted = false
|
||||||
`
|
`
|
||||||
|
@ -357,3 +357,13 @@ type QuizUTM struct {
|
|||||||
Deleted bool `json:"deleted"`
|
Deleted bool `json:"deleted"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QuizUsagePrivilege struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
QuizID int64 `json:"quiz_id"`
|
||||||
|
PrivilegeID int64 `json:"privilege_id"`
|
||||||
|
PrivilegeIDStr string `json:"privilege_id_str"`
|
||||||
|
UsedCount int32 `json:"used_count"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
@ -809,3 +809,60 @@ func (r *QuizRepository) GetAllQuizUtms(ctx context.Context, quizID int64) ([]mo
|
|||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *QuizRepository) GetQuizPrivilegeUsage(ctx context.Context, quizID int64, privilegeID int64, privilegeIDStr string) (*model.QuizUsagePrivilege, error) {
|
||||||
|
usage, err := r.queries.GetQuizPrivilegeUsage(ctx, sqlcgen.GetQuizPrivilegeUsageParams{
|
||||||
|
QuizID: quizID,
|
||||||
|
PrivilegeID: privilegeID,
|
||||||
|
PrivilegeIDStr: privilegeIDStr,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, nil // нет записи не ошибка
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &model.QuizUsagePrivilege{
|
||||||
|
ID: usage.ID,
|
||||||
|
QuizID: usage.QuizID,
|
||||||
|
PrivilegeID: usage.PrivilegeID,
|
||||||
|
PrivilegeIDStr: usage.PrivilegeIDStr,
|
||||||
|
CreatedAt: usage.CreatedAt,
|
||||||
|
UpdatedAt: usage.UpdatedAt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *QuizRepository) InsertQuizPrivilegeUsage(ctx context.Context, quizID int64, privilegeID int64, privilegeIDStr string) (*model.QuizUsagePrivilege, error) {
|
||||||
|
usage, err := r.queries.InsertQuizPrivilegeUsage(ctx, sqlcgen.InsertQuizPrivilegeUsageParams{
|
||||||
|
QuizID: quizID,
|
||||||
|
PrivilegeID: privilegeID,
|
||||||
|
PrivilegeIDStr: privilegeIDStr,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &model.QuizUsagePrivilege{
|
||||||
|
ID: usage.ID,
|
||||||
|
QuizID: usage.QuizID,
|
||||||
|
PrivilegeID: usage.PrivilegeID,
|
||||||
|
PrivilegeIDStr: usage.PrivilegeIDStr,
|
||||||
|
CreatedAt: usage.CreatedAt,
|
||||||
|
UpdatedAt: usage.UpdatedAt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *QuizRepository) IncrementQuizPrivilegeUsage(ctx context.Context, quizID int64, privilegeID int64, privilegeIDStr string) error {
|
||||||
|
return r.queries.IncrementQuizPrivilegeUsage(ctx, sqlcgen.IncrementQuizPrivilegeUsageParams{
|
||||||
|
QuizID: quizID,
|
||||||
|
PrivilegeID: privilegeID,
|
||||||
|
PrivilegeIDStr: privilegeIDStr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *QuizRepository) ResetQuizPrivilegeUsageCount(ctx context.Context, quizID int64, privilegeID int64, privilegeIDStr string) error {
|
||||||
|
return r.queries.ResetQuizPrivilegeUsageCount(ctx, sqlcgen.ResetQuizPrivilegeUsageCountParams{
|
||||||
|
QuizID: quizID,
|
||||||
|
PrivilegeID: privilegeID,
|
||||||
|
PrivilegeIDStr: privilegeIDStr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -52,6 +52,8 @@ packages:
|
|||||||
- "./dal/schema/000024_init.down.sql"
|
- "./dal/schema/000024_init.down.sql"
|
||||||
- "./dal/schema/000025_init.up.sql"
|
- "./dal/schema/000025_init.up.sql"
|
||||||
- "./dal/schema/000025_init.down.sql"
|
- "./dal/schema/000025_init.down.sql"
|
||||||
|
- "./dal/schema/000026_init.up.sql"
|
||||||
|
- "./dal/schema/000026_init.down.sql"
|
||||||
engine: "postgresql"
|
engine: "postgresql"
|
||||||
emit_json_tags: true
|
emit_json_tags: true
|
||||||
emit_db_tags: true
|
emit_db_tags: true
|
||||||
|
Loading…
Reference in New Issue
Block a user