feat: add question type definition groups with nullable group_id
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
c5347f5b6f
commit
527ee238dd
|
|
@ -0,0 +1,8 @@
|
|||
DROP INDEX IF EXISTS idx_question_type_definitions_group_id;
|
||||
|
||||
ALTER TABLE question_type_definitions
|
||||
DROP COLUMN IF EXISTS group_id;
|
||||
|
||||
DROP INDEX IF EXISTS idx_question_type_definition_groups_status;
|
||||
|
||||
DROP TABLE IF EXISTS question_type_definition_groups;
|
||||
19
db/migrations/000090_question_type_definition_groups.up.sql
Normal file
19
db/migrations/000090_question_type_definition_groups.up.sql
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
CREATE TABLE IF NOT EXISTS question_type_definition_groups (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(120) NOT NULL,
|
||||
description TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE' CHECK (status IN ('ACTIVE', 'INACTIVE')),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_question_type_definition_groups_status
|
||||
ON question_type_definition_groups(status);
|
||||
|
||||
ALTER TABLE question_type_definitions
|
||||
ADD COLUMN IF NOT EXISTS group_id BIGINT NULL
|
||||
REFERENCES question_type_definition_groups(id) ON DELETE SET NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_question_type_definitions_group_id
|
||||
ON question_type_definitions(group_id);
|
||||
1025
docs/question-type-definition-groups-admin-panel-integration.md
Normal file
1025
docs/question-type-definition-groups-admin-panel-integration.md
Normal file
File diff suppressed because it is too large
Load Diff
43
internal/domain/question_type_definition_groups.go
Normal file
43
internal/domain/question_type_definition_groups.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrQuestionTypeDefinitionGroupNotFound = errors.New("question type definition group not found")
|
||||
|
||||
type QuestionTypeDefinitionGroup struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
DisplayOrder int32 `json:"display_order"`
|
||||
Status string `json:"status"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
type QuestionTypeDefinitionGroupDetail struct {
|
||||
QuestionTypeDefinitionGroup
|
||||
Definitions []QuestionTypeDefinition `json:"definitions"`
|
||||
}
|
||||
|
||||
type CreateQuestionTypeDefinitionGroupInput struct {
|
||||
Name string
|
||||
Description *string
|
||||
DisplayOrder *int32
|
||||
Status *string
|
||||
}
|
||||
|
||||
type UpdateQuestionTypeDefinitionGroupInput struct {
|
||||
Name *string
|
||||
Description *string
|
||||
DisplayOrder *int32
|
||||
Status *string
|
||||
}
|
||||
|
||||
// NullableIDPatch supports three-state updates: omit (Set=false), clear (Set=true, Value=nil), set (Set=true, Value!=nil).
|
||||
type NullableIDPatch struct {
|
||||
Set bool
|
||||
Value *int64
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ type QuestionTypeDefinition struct {
|
|||
Key string
|
||||
DisplayName string
|
||||
Description *string
|
||||
GroupID *int64 `json:"group_id"`
|
||||
StimulusComponentKinds []string
|
||||
ResponseComponentKinds []string
|
||||
StimulusSchema []DynamicElementDefinition
|
||||
|
|
@ -21,6 +22,7 @@ type CreateQuestionTypeDefinitionInput struct {
|
|||
Key string
|
||||
DisplayName string
|
||||
Description *string
|
||||
GroupID *int64
|
||||
StimulusComponentKinds []string
|
||||
ResponseComponentKinds []string
|
||||
StimulusSchema []DynamicElementDefinition
|
||||
|
|
@ -32,6 +34,7 @@ type CreateQuestionTypeDefinitionInput struct {
|
|||
type UpdateQuestionTypeDefinitionInput struct {
|
||||
DisplayName *string
|
||||
Description *string
|
||||
GroupID NullableIDPatch
|
||||
StimulusComponentKinds []string
|
||||
ResponseComponentKinds []string
|
||||
StimulusSchema []DynamicElementDefinition
|
||||
|
|
|
|||
|
|
@ -9,10 +9,18 @@ type QuestionStore interface {
|
|||
// Question Type Definitions (dynamic builder presets)
|
||||
CreateQuestionTypeDefinition(ctx context.Context, input domain.CreateQuestionTypeDefinitionInput) (domain.QuestionTypeDefinition, error)
|
||||
GetQuestionTypeDefinitionByID(ctx context.Context, id int64) (domain.QuestionTypeDefinition, error)
|
||||
ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error)
|
||||
ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, groupID *int64, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error)
|
||||
UpdateQuestionTypeDefinition(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionInput) error
|
||||
DeleteQuestionTypeDefinition(ctx context.Context, id int64) error
|
||||
|
||||
// Question Type Definition Groups
|
||||
CreateQuestionTypeDefinitionGroup(ctx context.Context, input domain.CreateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error)
|
||||
GetQuestionTypeDefinitionGroupByID(ctx context.Context, id int64) (domain.QuestionTypeDefinitionGroupDetail, error)
|
||||
ListQuestionTypeDefinitionGroups(ctx context.Context, status *string, limit, offset int32) ([]domain.QuestionTypeDefinitionGroup, int64, error)
|
||||
UpdateQuestionTypeDefinitionGroup(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error)
|
||||
DeleteQuestionTypeDefinitionGroup(ctx context.Context, id int64) error
|
||||
QuestionTypeDefinitionGroupExists(ctx context.Context, id int64) (bool, error)
|
||||
|
||||
// Questions
|
||||
CreateQuestion(ctx context.Context, input domain.CreateQuestionInput) (domain.Question, error)
|
||||
GetQuestionByID(ctx context.Context, id int64) (domain.Question, error)
|
||||
|
|
|
|||
234
internal/repository/question_type_definition_groups.go
Normal file
234
internal/repository/question_type_definition_groups.go
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"Yimaru-Backend/internal/domain"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func normalizeQuestionTypeDefinitionGroupStatus(status *string) string {
|
||||
if status == nil || strings.TrimSpace(*status) == "" {
|
||||
return "ACTIVE"
|
||||
}
|
||||
return strings.ToUpper(strings.TrimSpace(*status))
|
||||
}
|
||||
|
||||
func questionTypeDefinitionGroupToDomain(
|
||||
id int64,
|
||||
name string,
|
||||
description pgtype.Text,
|
||||
displayOrder int32,
|
||||
status string,
|
||||
createdAt pgtype.Timestamptz,
|
||||
updatedAt pgtype.Timestamptz,
|
||||
) domain.QuestionTypeDefinitionGroup {
|
||||
return domain.QuestionTypeDefinitionGroup{
|
||||
ID: id,
|
||||
Name: name,
|
||||
Description: fromPgText(description),
|
||||
DisplayOrder: displayOrder,
|
||||
Status: status,
|
||||
CreatedAt: createdAt.Time,
|
||||
UpdatedAt: timePtr(updatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) validateQuestionTypeDefinitionGroupID(ctx context.Context, groupID *int64) error {
|
||||
if groupID == nil || *groupID <= 0 {
|
||||
return nil
|
||||
}
|
||||
exists, err := s.QuestionTypeDefinitionGroupExists(ctx, *groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return domain.ErrQuestionTypeDefinitionGroupNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) QuestionTypeDefinitionGroupExists(ctx context.Context, id int64) (bool, error) {
|
||||
var exists bool
|
||||
err := s.conn.QueryRow(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM question_type_definition_groups WHERE id = $1)
|
||||
`, id).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func (s *Store) CreateQuestionTypeDefinitionGroup(ctx context.Context, input domain.CreateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
||||
displayOrder := int32(0)
|
||||
if input.DisplayOrder != nil {
|
||||
displayOrder = *input.DisplayOrder
|
||||
}
|
||||
|
||||
row := s.conn.QueryRow(ctx, `
|
||||
INSERT INTO question_type_definition_groups (name, description, display_order, status)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, name, description, display_order, status, created_at, updated_at
|
||||
`,
|
||||
strings.TrimSpace(input.Name),
|
||||
toPgText(input.Description),
|
||||
displayOrder,
|
||||
normalizeQuestionTypeDefinitionGroupStatus(input.Status),
|
||||
)
|
||||
|
||||
var (
|
||||
id int64
|
||||
name string
|
||||
description pgtype.Text
|
||||
orderVal int32
|
||||
status string
|
||||
createdAt pgtype.Timestamptz
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := row.Scan(&id, &name, &description, &orderVal, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinitionGroup{}, err
|
||||
}
|
||||
|
||||
return questionTypeDefinitionGroupToDomain(id, name, description, orderVal, status, createdAt, updatedAt), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetQuestionTypeDefinitionGroupByID(ctx context.Context, id int64) (domain.QuestionTypeDefinitionGroupDetail, error) {
|
||||
row := s.conn.QueryRow(ctx, `
|
||||
SELECT id, name, description, display_order, status, created_at, updated_at
|
||||
FROM question_type_definition_groups
|
||||
WHERE id = $1
|
||||
`, id)
|
||||
|
||||
var (
|
||||
name string
|
||||
description pgtype.Text
|
||||
displayOrder int32
|
||||
status string
|
||||
createdAt pgtype.Timestamptz
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := row.Scan(&id, &name, &description, &displayOrder, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinitionGroupDetail{}, err
|
||||
}
|
||||
|
||||
group := questionTypeDefinitionGroupToDomain(id, name, description, displayOrder, status, createdAt, updatedAt)
|
||||
groupID := id
|
||||
defs, _, err := s.ListQuestionTypeDefinitions(ctx, nil, true, &groupID, 0, 0)
|
||||
if err != nil {
|
||||
return domain.QuestionTypeDefinitionGroupDetail{}, err
|
||||
}
|
||||
if defs == nil {
|
||||
defs = []domain.QuestionTypeDefinition{}
|
||||
}
|
||||
|
||||
return domain.QuestionTypeDefinitionGroupDetail{
|
||||
QuestionTypeDefinitionGroup: group,
|
||||
Definitions: defs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListQuestionTypeDefinitionGroups(ctx context.Context, status *string, limit, offset int32) ([]domain.QuestionTypeDefinitionGroup, int64, error) {
|
||||
const baseWhere = `
|
||||
FROM question_type_definition_groups
|
||||
WHERE ($1::VARCHAR IS NULL OR status = $1)`
|
||||
|
||||
var total int64
|
||||
if err := s.conn.QueryRow(ctx, `SELECT COUNT(*)`+baseWhere, status).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
listSQL := `
|
||||
SELECT id, name, description, display_order, status, created_at, updated_at` + baseWhere + `
|
||||
ORDER BY display_order ASC, name ASC`
|
||||
|
||||
args := []interface{}{status}
|
||||
if limit > 0 {
|
||||
listSQL += ` LIMIT $2 OFFSET $3`
|
||||
args = append(args, limit, offset)
|
||||
}
|
||||
|
||||
rows, err := s.conn.Query(ctx, listSQL, args...)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]domain.QuestionTypeDefinitionGroup, 0)
|
||||
for rows.Next() {
|
||||
var (
|
||||
id int64
|
||||
name string
|
||||
description pgtype.Text
|
||||
displayOrder int32
|
||||
statusVal string
|
||||
createdAt pgtype.Timestamptz
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := rows.Scan(&id, &name, &description, &displayOrder, &statusVal, &createdAt, &updatedAt); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, questionTypeDefinitionGroupToDomain(id, name, description, displayOrder, statusVal, createdAt, updatedAt))
|
||||
}
|
||||
|
||||
return out, total, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) UpdateQuestionTypeDefinitionGroup(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
||||
row := s.conn.QueryRow(ctx, `
|
||||
SELECT id, name, description, display_order, status, created_at, updated_at
|
||||
FROM question_type_definition_groups
|
||||
WHERE id = $1
|
||||
`, id)
|
||||
|
||||
var (
|
||||
name string
|
||||
description pgtype.Text
|
||||
displayOrder int32
|
||||
status string
|
||||
createdAt pgtype.Timestamptz
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := row.Scan(&id, &name, &description, &displayOrder, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinitionGroup{}, err
|
||||
}
|
||||
|
||||
if input.Name != nil && strings.TrimSpace(*input.Name) != "" {
|
||||
name = strings.TrimSpace(*input.Name)
|
||||
}
|
||||
if input.Description != nil {
|
||||
description = toPgText(input.Description)
|
||||
}
|
||||
if input.DisplayOrder != nil {
|
||||
displayOrder = *input.DisplayOrder
|
||||
}
|
||||
if input.Status != nil {
|
||||
status = normalizeQuestionTypeDefinitionGroupStatus(input.Status)
|
||||
}
|
||||
|
||||
updateRow := s.conn.QueryRow(ctx, `
|
||||
UPDATE question_type_definition_groups
|
||||
SET name = $2,
|
||||
description = $3,
|
||||
display_order = $4,
|
||||
status = $5,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id, name, description, display_order, status, created_at, updated_at
|
||||
`, id, name, description, displayOrder, status)
|
||||
|
||||
if err := updateRow.Scan(&id, &name, &description, &displayOrder, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinitionGroup{}, err
|
||||
}
|
||||
|
||||
return questionTypeDefinitionGroupToDomain(id, name, description, displayOrder, status, createdAt, updatedAt), nil
|
||||
}
|
||||
|
||||
func (s *Store) DeleteQuestionTypeDefinitionGroup(ctx context.Context, id int64) error {
|
||||
cmd, err := s.conn.Exec(ctx, `DELETE FROM question_type_definition_groups WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cmd.RowsAffected() == 0 {
|
||||
return pgx.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -180,6 +180,7 @@ func questionTypeDefinitionToDomain(
|
|||
key string,
|
||||
displayName string,
|
||||
description pgtype.Text,
|
||||
groupID pgtype.Int8,
|
||||
stimulusKinds []string,
|
||||
responseKinds []string,
|
||||
stimulusSchema []byte,
|
||||
|
|
@ -203,6 +204,7 @@ func questionTypeDefinitionToDomain(
|
|||
Key: key,
|
||||
DisplayName: displayName,
|
||||
Description: fromPgText(description),
|
||||
GroupID: fromPgInt8(groupID),
|
||||
StimulusComponentKinds: stimulusKinds,
|
||||
ResponseComponentKinds: responseKinds,
|
||||
StimulusSchema: stimulusSchemaDef,
|
||||
|
|
@ -304,16 +306,20 @@ func (s *Store) CreateQuestionTypeDefinition(ctx context.Context, input domain.C
|
|||
if err := domain.ValidatePersistableQuestionTypeDefinition(normalizedKey, responseKinds); err != nil {
|
||||
return domain.QuestionTypeDefinition{}, err
|
||||
}
|
||||
if err := s.validateQuestionTypeDefinitionGroupID(ctx, input.GroupID); err != nil {
|
||||
return domain.QuestionTypeDefinition{}, err
|
||||
}
|
||||
|
||||
row := s.conn.QueryRow(ctx, `
|
||||
INSERT INTO question_type_definitions
|
||||
(key, display_name, description, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7::jsonb, $8, $9)
|
||||
RETURNING id, key, display_name, description, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at
|
||||
(key, display_name, description, group_id, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7::jsonb, $8::jsonb, $9, $10)
|
||||
RETURNING id, key, display_name, description, group_id, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at
|
||||
`,
|
||||
normalizedKey,
|
||||
strings.TrimSpace(input.DisplayName),
|
||||
toPgText(input.Description),
|
||||
toPgInt8(input.GroupID),
|
||||
stimulusKinds,
|
||||
responseKinds,
|
||||
mustJSON(stimulusSchema),
|
||||
|
|
@ -327,6 +333,7 @@ func (s *Store) CreateQuestionTypeDefinition(ctx context.Context, input domain.C
|
|||
key string
|
||||
displayName string
|
||||
description pgtype.Text
|
||||
groupID pgtype.Int8
|
||||
stimulus []string
|
||||
response []string
|
||||
stimulusSch []byte
|
||||
|
|
@ -336,16 +343,16 @@ func (s *Store) CreateQuestionTypeDefinition(ctx context.Context, input domain.C
|
|||
createdAt time.Time
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := row.Scan(&id, &key, &displayName, &description, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &status, &createdAt, &updatedAt); err != nil {
|
||||
if err := row.Scan(&id, &key, &displayName, &description, &groupID, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinition{}, err
|
||||
}
|
||||
|
||||
return questionTypeDefinitionToDomain(id, key, displayName, description, stimulus, response, stimulusSch, responseSch, isSystem, status, createdAt, updatedAt), nil
|
||||
return questionTypeDefinitionToDomain(id, key, displayName, description, groupID, stimulus, response, stimulusSch, responseSch, isSystem, status, createdAt, updatedAt), nil
|
||||
}
|
||||
|
||||
func (s *Store) GetQuestionTypeDefinitionByID(ctx context.Context, id int64) (domain.QuestionTypeDefinition, error) {
|
||||
row := s.conn.QueryRow(ctx, `
|
||||
SELECT id, key, display_name, description, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at
|
||||
SELECT id, key, display_name, description, group_id, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at
|
||||
FROM question_type_definitions
|
||||
WHERE id = $1
|
||||
`, id)
|
||||
|
|
@ -354,6 +361,7 @@ func (s *Store) GetQuestionTypeDefinitionByID(ctx context.Context, id int64) (do
|
|||
key string
|
||||
displayName string
|
||||
description pgtype.Text
|
||||
groupID pgtype.Int8
|
||||
stimulus []string
|
||||
response []string
|
||||
stimulusSch []byte
|
||||
|
|
@ -363,30 +371,31 @@ func (s *Store) GetQuestionTypeDefinitionByID(ctx context.Context, id int64) (do
|
|||
createdAt time.Time
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := row.Scan(&id, &key, &displayName, &description, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &status, &createdAt, &updatedAt); err != nil {
|
||||
if err := row.Scan(&id, &key, &displayName, &description, &groupID, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &status, &createdAt, &updatedAt); err != nil {
|
||||
return domain.QuestionTypeDefinition{}, err
|
||||
}
|
||||
return questionTypeDefinitionToDomain(id, key, displayName, description, stimulus, response, stimulusSch, responseSch, isSystem, status, createdAt, updatedAt), nil
|
||||
return questionTypeDefinitionToDomain(id, key, displayName, description, groupID, stimulus, response, stimulusSch, responseSch, isSystem, status, createdAt, updatedAt), nil
|
||||
}
|
||||
|
||||
func (s *Store) ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error) {
|
||||
func (s *Store) ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, groupID *int64, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error) {
|
||||
const baseWhere = `
|
||||
FROM question_type_definitions
|
||||
WHERE ($1::VARCHAR IS NULL OR status = $1)
|
||||
AND ($2::BOOLEAN = TRUE OR is_system = FALSE)`
|
||||
AND ($2::BOOLEAN = TRUE OR is_system = FALSE)
|
||||
AND ($3::BIGINT IS NULL OR group_id = $3)`
|
||||
|
||||
var total int64
|
||||
if err := s.conn.QueryRow(ctx, `SELECT COUNT(*)`+baseWhere, status, includeSystem).Scan(&total); err != nil {
|
||||
if err := s.conn.QueryRow(ctx, `SELECT COUNT(*)`+baseWhere, status, includeSystem, toPgInt8(groupID)).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
listSQL := `
|
||||
SELECT id, key, display_name, description, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at` + baseWhere + `
|
||||
SELECT id, key, display_name, description, group_id, stimulus_component_kinds, response_component_kinds, stimulus_schema, response_schema, is_system, status, created_at, updated_at` + baseWhere + `
|
||||
ORDER BY is_system DESC, display_name ASC`
|
||||
|
||||
args := []interface{}{status, includeSystem}
|
||||
args := []interface{}{status, includeSystem, toPgInt8(groupID)}
|
||||
if limit > 0 {
|
||||
listSQL += ` LIMIT $3 OFFSET $4`
|
||||
listSQL += ` LIMIT $4 OFFSET $5`
|
||||
args = append(args, limit, offset)
|
||||
}
|
||||
|
||||
|
|
@ -403,6 +412,7 @@ func (s *Store) ListQuestionTypeDefinitions(ctx context.Context, status *string,
|
|||
key string
|
||||
displayName string
|
||||
description pgtype.Text
|
||||
groupID pgtype.Int8
|
||||
stimulus []string
|
||||
response []string
|
||||
stimulusSch []byte
|
||||
|
|
@ -412,10 +422,10 @@ func (s *Store) ListQuestionTypeDefinitions(ctx context.Context, status *string,
|
|||
createdAt time.Time
|
||||
updatedAt pgtype.Timestamptz
|
||||
)
|
||||
if err := rows.Scan(&id, &key, &displayName, &description, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &defStatus, &createdAt, &updatedAt); err != nil {
|
||||
if err := rows.Scan(&id, &key, &displayName, &description, &groupID, &stimulus, &response, &stimulusSch, &responseSch, &isSystem, &defStatus, &createdAt, &updatedAt); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, questionTypeDefinitionToDomain(id, key, displayName, description, stimulus, response, stimulusSch, responseSch, isSystem, defStatus, createdAt, updatedAt))
|
||||
out = append(out, questionTypeDefinitionToDomain(id, key, displayName, description, groupID, stimulus, response, stimulusSch, responseSch, isSystem, defStatus, createdAt, updatedAt))
|
||||
}
|
||||
|
||||
return out, total, rows.Err()
|
||||
|
|
@ -477,18 +487,31 @@ func (s *Store) UpdateQuestionTypeDefinition(ctx context.Context, id int64, inpu
|
|||
status = normalizeDefinitionStatus(input.Status)
|
||||
}
|
||||
|
||||
groupID := existing.GroupID
|
||||
if input.GroupID.Set {
|
||||
if input.GroupID.Value != nil && *input.GroupID.Value > 0 {
|
||||
if err := s.validateQuestionTypeDefinitionGroupID(ctx, input.GroupID.Value); err != nil {
|
||||
return err
|
||||
}
|
||||
groupID = input.GroupID.Value
|
||||
} else {
|
||||
groupID = nil
|
||||
}
|
||||
}
|
||||
|
||||
_, err = s.conn.Exec(ctx, `
|
||||
UPDATE question_type_definitions
|
||||
SET display_name = $2,
|
||||
description = $3,
|
||||
stimulus_component_kinds = $4,
|
||||
response_component_kinds = $5,
|
||||
stimulus_schema = $6::jsonb,
|
||||
response_schema = $7::jsonb,
|
||||
status = $8,
|
||||
group_id = $4,
|
||||
stimulus_component_kinds = $5,
|
||||
response_component_kinds = $6,
|
||||
stimulus_schema = $7::jsonb,
|
||||
response_schema = $8::jsonb,
|
||||
status = $9,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
`, id, displayName, toPgText(description), stimulusKinds, responseKinds, mustJSON(stimulusSchema), mustJSON(responseSchema), status)
|
||||
`, id, displayName, toPgText(description), toPgInt8(groupID), stimulusKinds, responseKinds, mustJSON(stimulusSchema), mustJSON(responseSchema), status)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,14 +40,14 @@ func clampQuestionTypeDefinitionPage(limit, offset int32) (int32, int32) {
|
|||
return limit, offset
|
||||
}
|
||||
|
||||
func (s *Service) ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error) {
|
||||
func (s *Service) ListQuestionTypeDefinitions(ctx context.Context, status *string, includeSystem bool, groupID *int64, limit, offset int32) ([]domain.QuestionTypeDefinition, int64, error) {
|
||||
limit, offset = clampQuestionTypeDefinitionPage(limit, offset)
|
||||
return s.questionStore.ListQuestionTypeDefinitions(ctx, status, includeSystem, limit, offset)
|
||||
return s.questionStore.ListQuestionTypeDefinitions(ctx, status, includeSystem, groupID, limit, offset)
|
||||
}
|
||||
|
||||
func (s *Service) ActiveQuestionTypeDefinitionCatalog(ctx context.Context) ([]domain.QuestionTypeDefinition, error) {
|
||||
active := "ACTIVE"
|
||||
catalog, _, err := s.questionStore.ListQuestionTypeDefinitions(ctx, &active, true, 0, 0)
|
||||
catalog, _, err := s.questionStore.ListQuestionTypeDefinitions(ctx, &active, true, nil, 0, 0)
|
||||
return catalog, err
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +59,31 @@ func (s *Service) DeleteQuestionTypeDefinition(ctx context.Context, id int64) er
|
|||
return s.questionStore.DeleteQuestionTypeDefinition(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) CreateQuestionTypeDefinitionGroup(ctx context.Context, input domain.CreateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
||||
return s.questionStore.CreateQuestionTypeDefinitionGroup(ctx, input)
|
||||
}
|
||||
|
||||
func (s *Service) GetQuestionTypeDefinitionGroupByID(ctx context.Context, id int64) (domain.QuestionTypeDefinitionGroupDetail, error) {
|
||||
return s.questionStore.GetQuestionTypeDefinitionGroupByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) ListQuestionTypeDefinitionGroups(ctx context.Context, status *string, limit, offset int32) ([]domain.QuestionTypeDefinitionGroup, int64, error) {
|
||||
limit, offset = clampQuestionTypeDefinitionPage(limit, offset)
|
||||
return s.questionStore.ListQuestionTypeDefinitionGroups(ctx, status, limit, offset)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateQuestionTypeDefinitionGroup(ctx context.Context, id int64, input domain.UpdateQuestionTypeDefinitionGroupInput) (domain.QuestionTypeDefinitionGroup, error) {
|
||||
return s.questionStore.UpdateQuestionTypeDefinitionGroup(ctx, id, input)
|
||||
}
|
||||
|
||||
func (s *Service) DeleteQuestionTypeDefinitionGroup(ctx context.Context, id int64) error {
|
||||
return s.questionStore.DeleteQuestionTypeDefinitionGroup(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) QuestionTypeDefinitionGroupExists(ctx context.Context, id int64) (bool, error) {
|
||||
return s.questionStore.QuestionTypeDefinitionGroupExists(ctx, id)
|
||||
}
|
||||
|
||||
// Questions
|
||||
|
||||
func (s *Service) CreateQuestion(ctx context.Context, input domain.CreateQuestionInput) (domain.Question, error) {
|
||||
|
|
@ -280,7 +305,7 @@ func (s *Service) GetQuestionTypesInSet(ctx context.Context, setID int64) (domai
|
|||
return domain.QuestionSetQuestionTypesSummary{}, err
|
||||
}
|
||||
active := "ACTIVE"
|
||||
catalog, _, err := s.questionStore.ListQuestionTypeDefinitions(ctx, &active, true, 0, 0)
|
||||
catalog, _, err := s.questionStore.ListQuestionTypeDefinitions(ctx, &active, true, nil, 0, 0)
|
||||
if err != nil {
|
||||
return domain.QuestionSetQuestionTypesSummary{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ type createQuestionTypeDefinitionReq struct {
|
|||
Key string `json:"key" validate:"required"`
|
||||
DisplayName string `json:"display_name" validate:"required"`
|
||||
Description *string `json:"description"`
|
||||
GroupID *int64 `json:"group_id"`
|
||||
StimulusComponentKinds []string `json:"stimulus_component_kinds"`
|
||||
ResponseComponentKinds []string `json:"response_component_kinds"`
|
||||
StimulusSchema []domain.DynamicElementDefinition `json:"stimulus_schema"`
|
||||
|
|
@ -44,6 +45,7 @@ type createQuestionTypeDefinitionReq struct {
|
|||
type updateQuestionTypeDefinitionReq struct {
|
||||
DisplayName *string `json:"display_name"`
|
||||
Description *string `json:"description"`
|
||||
GroupID json.RawMessage `json:"group_id"`
|
||||
StimulusComponentKinds []string `json:"stimulus_component_kinds"`
|
||||
ResponseComponentKinds []string `json:"response_component_kinds"`
|
||||
StimulusSchema []domain.DynamicElementDefinition `json:"stimulus_schema"`
|
||||
|
|
@ -51,6 +53,56 @@ type updateQuestionTypeDefinitionReq struct {
|
|||
Status *string `json:"status"`
|
||||
}
|
||||
|
||||
type createQuestionTypeDefinitionGroupReq struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description *string `json:"description"`
|
||||
DisplayOrder *int32 `json:"display_order"`
|
||||
Status *string `json:"status"`
|
||||
}
|
||||
|
||||
type updateQuestionTypeDefinitionGroupReq struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
DisplayOrder *int32 `json:"display_order"`
|
||||
Status *string `json:"status"`
|
||||
}
|
||||
|
||||
type listQuestionTypeDefinitionGroupsData struct {
|
||||
Groups []domain.QuestionTypeDefinitionGroup `json:"groups"`
|
||||
TotalCount int64 `json:"total_count"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
func parseNullableIDPatch(raw json.RawMessage) (domain.NullableIDPatch, error) {
|
||||
if raw == nil {
|
||||
return domain.NullableIDPatch{}, nil
|
||||
}
|
||||
if string(raw) == "null" {
|
||||
return domain.NullableIDPatch{Set: true, Value: nil}, nil
|
||||
}
|
||||
var id int64
|
||||
if err := json.Unmarshal(raw, &id); err != nil {
|
||||
return domain.NullableIDPatch{}, err
|
||||
}
|
||||
if id <= 0 {
|
||||
return domain.NullableIDPatch{Set: true, Value: nil}, nil
|
||||
}
|
||||
return domain.NullableIDPatch{Set: true, Value: &id}, nil
|
||||
}
|
||||
|
||||
func parseOptionalGroupIDQuery(c *fiber.Ctx) (*int64, error) {
|
||||
raw := strings.TrimSpace(c.Query("group_id"))
|
||||
if raw == "" {
|
||||
return nil, nil
|
||||
}
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return nil, fmt.Errorf("group_id must be a positive integer")
|
||||
}
|
||||
return &id, nil
|
||||
}
|
||||
|
||||
// GetQuestionTypeComponentCatalog godoc
|
||||
// @Summary Question-type builder component catalog
|
||||
// @Description Valid stimulus and response component kind codes for dynamic question-type definitions
|
||||
|
|
@ -130,6 +182,7 @@ func (h *Handler) CreateQuestionTypeDefinition(c *fiber.Ctx) error {
|
|||
Key: strings.TrimSpace(req.Key),
|
||||
DisplayName: strings.TrimSpace(req.DisplayName),
|
||||
Description: req.Description,
|
||||
GroupID: req.GroupID,
|
||||
StimulusComponentKinds: req.StimulusComponentKinds,
|
||||
ResponseComponentKinds: req.ResponseComponentKinds,
|
||||
StimulusSchema: req.StimulusSchema,
|
||||
|
|
@ -138,6 +191,12 @@ func (h *Handler) CreateQuestionTypeDefinition(c *fiber.Ctx) error {
|
|||
IsSystem: false,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrQuestionTypeDefinitionGroupNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Unable to create question type definition",
|
||||
Error: err.Error(),
|
||||
|
|
@ -190,6 +249,28 @@ func (h *Handler) ListQuestionTypeDefinitions(c *fiber.Ctx) error {
|
|||
statusPtr = &status
|
||||
}
|
||||
includeSystem := strings.EqualFold(c.Query("include_system", "true"), "true")
|
||||
groupID, err := parseOptionalGroupIDQuery(c)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid group_id",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if groupID != nil {
|
||||
exists, err := h.questionsSvc.QuestionTypeDefinitionGroupExists(c.Context(), *groupID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to validate group_id",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if !exists {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: domain.ErrQuestionTypeDefinitionGroupNotFound.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
limit, err := strconv.Atoi(c.Query("limit", "20"))
|
||||
if err != nil {
|
||||
|
|
@ -206,7 +287,7 @@ func (h *Handler) ListQuestionTypeDefinitions(c *fiber.Ctx) error {
|
|||
})
|
||||
}
|
||||
|
||||
defs, total, err := h.questionsSvc.ListQuestionTypeDefinitions(c.Context(), statusPtr, includeSystem, int32(limit), int32(offset))
|
||||
defs, total, err := h.questionsSvc.ListQuestionTypeDefinitions(c.Context(), statusPtr, includeSystem, groupID, int32(limit), int32(offset))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to list question type definitions",
|
||||
|
|
@ -344,9 +425,18 @@ func (h *Handler) UpdateQuestionTypeDefinition(c *fiber.Ctx) error {
|
|||
req.DisplayName = &displayName
|
||||
}
|
||||
|
||||
groupIDPatch, err := parseNullableIDPatch(req.GroupID)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid group_id",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
err = h.questionsSvc.UpdateQuestionTypeDefinition(c.Context(), id, domain.UpdateQuestionTypeDefinitionInput{
|
||||
DisplayName: req.DisplayName,
|
||||
Description: req.Description,
|
||||
GroupID: groupIDPatch,
|
||||
StimulusComponentKinds: req.StimulusComponentKinds,
|
||||
ResponseComponentKinds: req.ResponseComponentKinds,
|
||||
StimulusSchema: req.StimulusSchema,
|
||||
|
|
@ -354,6 +444,12 @@ func (h *Handler) UpdateQuestionTypeDefinition(c *fiber.Ctx) error {
|
|||
Status: req.Status,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrQuestionTypeDefinitionGroupNotFound) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Unable to update question type definition",
|
||||
Error: err.Error(),
|
||||
|
|
@ -436,3 +532,229 @@ func (h *Handler) DeleteQuestionTypeDefinition(c *fiber.Ctx) error {
|
|||
Data: fiber.Map{"id": id},
|
||||
})
|
||||
}
|
||||
|
||||
// CreateQuestionTypeDefinitionGroup godoc
|
||||
// @Summary Create question type definition group
|
||||
// @Tags questions
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body createQuestionTypeDefinitionGroupReq true "Group payload"
|
||||
// @Success 201 {object} domain.Response
|
||||
// @Router /api/v1/questions/type-definition-groups [post]
|
||||
func (h *Handler) CreateQuestionTypeDefinitionGroup(c *fiber.Ctx) error {
|
||||
var req createQuestionTypeDefinitionGroupReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid request body",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if valErrs, ok := h.validator.Validate(c, req); !ok {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Validation failed",
|
||||
Error: firstValidationError(valErrs),
|
||||
})
|
||||
}
|
||||
|
||||
group, err := h.questionsSvc.CreateQuestionTypeDefinitionGroup(c.Context(), domain.CreateQuestionTypeDefinitionGroupInput{
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Description: req.Description,
|
||||
DisplayOrder: req.DisplayOrder,
|
||||
Status: req.Status,
|
||||
})
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Unable to create question type definition group",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(domain.Response{
|
||||
Message: "Question type definition group created",
|
||||
Data: group,
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusCreated,
|
||||
})
|
||||
}
|
||||
|
||||
// ListQuestionTypeDefinitionGroups godoc
|
||||
// @Summary List question type definition groups
|
||||
// @Tags questions
|
||||
// @Produce json
|
||||
// @Param status query string false "Filter by status (ACTIVE, INACTIVE)"
|
||||
// @Param limit query int false "Page size (default 20, max 200)" default(20)
|
||||
// @Param offset query int false "Page offset" default(0)
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Router /api/v1/questions/type-definition-groups [get]
|
||||
func (h *Handler) ListQuestionTypeDefinitionGroups(c *fiber.Ctx) error {
|
||||
status := strings.ToUpper(strings.TrimSpace(c.Query("status")))
|
||||
var statusPtr *string
|
||||
if status != "" {
|
||||
statusPtr = &status
|
||||
}
|
||||
|
||||
limit, err := strconv.Atoi(c.Query("limit", "20"))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid limit",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
offset, err := strconv.Atoi(c.Query("offset", "0"))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid offset",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
groups, total, err := h.questionsSvc.ListQuestionTypeDefinitionGroups(c.Context(), statusPtr, int32(limit), int32(offset))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to list question type definition groups",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if groups == nil {
|
||||
groups = []domain.QuestionTypeDefinitionGroup{}
|
||||
}
|
||||
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Question type definition groups",
|
||||
Data: listQuestionTypeDefinitionGroupsData{
|
||||
Groups: groups,
|
||||
TotalCount: total,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
},
|
||||
Success: true,
|
||||
StatusCode: fiber.StatusOK,
|
||||
})
|
||||
}
|
||||
|
||||
// GetQuestionTypeDefinitionGroupByID godoc
|
||||
// @Summary Get question type definition group with definitions
|
||||
// @Tags questions
|
||||
// @Produce json
|
||||
// @Param id path int true "Group id"
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Failure 404 {object} domain.ErrorResponse
|
||||
// @Router /api/v1/questions/type-definition-groups/{id} [get]
|
||||
func (h *Handler) GetQuestionTypeDefinitionGroupByID(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid id",
|
||||
Error: "id must be a positive integer",
|
||||
})
|
||||
}
|
||||
|
||||
group, err := h.questionsSvc.GetQuestionTypeDefinitionGroupByID(c.Context(), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
||||
Message: "Failed to get question type definition group",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Question type definition group",
|
||||
Data: group,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateQuestionTypeDefinitionGroup godoc
|
||||
// @Summary Update question type definition group
|
||||
// @Tags questions
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Group id"
|
||||
// @Param body body updateQuestionTypeDefinitionGroupReq true "Update group payload"
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Router /api/v1/questions/type-definition-groups/{id} [put]
|
||||
func (h *Handler) UpdateQuestionTypeDefinitionGroup(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid id",
|
||||
Error: "id must be a positive integer",
|
||||
})
|
||||
}
|
||||
|
||||
var req updateQuestionTypeDefinitionGroupReq
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid request body",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
if req.Name != nil {
|
||||
name := strings.TrimSpace(*req.Name)
|
||||
req.Name = &name
|
||||
}
|
||||
|
||||
group, err := h.questionsSvc.UpdateQuestionTypeDefinitionGroup(c.Context(), id, domain.UpdateQuestionTypeDefinitionGroupInput{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
DisplayOrder: req.DisplayOrder,
|
||||
Status: req.Status,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Unable to update question type definition group",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Question type definition group updated",
|
||||
Data: group,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteQuestionTypeDefinitionGroup godoc
|
||||
// @Summary Delete question type definition group
|
||||
// @Tags questions
|
||||
// @Produce json
|
||||
// @Param id path int true "Group id"
|
||||
// @Success 200 {object} domain.Response
|
||||
// @Router /api/v1/questions/type-definition-groups/{id} [delete]
|
||||
func (h *Handler) DeleteQuestionTypeDefinitionGroup(c *fiber.Ctx) error {
|
||||
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Invalid id",
|
||||
Error: "id must be a positive integer",
|
||||
})
|
||||
}
|
||||
|
||||
if err := h.questionsSvc.DeleteQuestionTypeDefinitionGroup(c.Context(), id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(domain.ErrorResponse{
|
||||
Message: "Question type definition group not found",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
||||
Message: "Unable to delete question type definition group",
|
||||
Error: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(domain.Response{
|
||||
Message: "Question type definition group deleted",
|
||||
Data: fiber.Map{"id": id},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -195,6 +195,11 @@ func (a *App) initAppRoutes() {
|
|||
groupV1.Get("/questions/search", a.authMiddleware, a.RequirePermission("questions.search"), h.SearchQuestions)
|
||||
groupV1.Get("/questions/component-catalog", a.authMiddleware, a.RequirePermission("questions.list"), h.GetQuestionTypeComponentCatalog)
|
||||
groupV1.Post("/questions/validate-question-type-definition", a.authMiddleware, a.RequirePermission("questions.create"), h.ValidateQuestionTypeDefinition)
|
||||
groupV1.Post("/questions/type-definition-groups", a.authMiddleware, a.RequirePermission("questions.create"), h.CreateQuestionTypeDefinitionGroup)
|
||||
groupV1.Get("/questions/type-definition-groups", a.authMiddleware, a.RequirePermission("questions.list"), h.ListQuestionTypeDefinitionGroups)
|
||||
groupV1.Get("/questions/type-definition-groups/:id", a.authMiddleware, a.RequirePermission("questions.get"), h.GetQuestionTypeDefinitionGroupByID)
|
||||
groupV1.Put("/questions/type-definition-groups/:id", a.authMiddleware, a.RequirePermission("questions.update"), h.UpdateQuestionTypeDefinitionGroup)
|
||||
groupV1.Delete("/questions/type-definition-groups/:id", a.authMiddleware, a.RequirePermission("questions.delete"), h.DeleteQuestionTypeDefinitionGroup)
|
||||
groupV1.Post("/questions/type-definitions", a.authMiddleware, a.RequirePermission("questions.create"), h.CreateQuestionTypeDefinition)
|
||||
groupV1.Get("/questions/type-definitions", a.authMiddleware, a.RequirePermission("questions.list"), h.ListQuestionTypeDefinitions)
|
||||
groupV1.Get("/questions/type-definitions/:id", a.authMiddleware, a.RequirePermission("questions.get"), h.GetQuestionTypeDefinitionByID)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user