Skip to content

Commit

Permalink
add isDuplicateAnswerAllowed
Browse files Browse the repository at this point in the history
  • Loading branch information
ramdos0207 committed Dec 20, 2023
1 parent 5919e79 commit d62152f
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 115 deletions.
4 changes: 2 additions & 2 deletions model/questionnaires.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

// IQuestionnaire QuestionnaireのRepository
type IQuestionnaire interface {
InsertQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string) (int, error)
UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int) error
InsertQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, IsDuplicateAnswerAllowed bool) (int, error)
UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, IsDuplicateAnswerAllowed bool, questionnaireID int) error
DeleteQuestionnaire(ctx context.Context, questionnaireID int) error
GetQuestionnaires(ctx context.Context, userID string, sort string, search string, pageNum int, nontargeted bool) ([]QuestionnaireInfo, int, error)
GetAdminQuestionnaires(ctx context.Context, userID string) ([]Questionnaires, error)
Expand Down
91 changes: 49 additions & 42 deletions model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ func NewQuestionnaire() *Questionnaire {
return new(Questionnaire)
}

//Questionnaires questionnairesテーブルの構造体
// Questionnaires questionnairesテーブルの構造体
type Questionnaires struct {
ID int `json:"questionnaireID" gorm:"type:int(11) AUTO_INCREMENT;not null;primaryKey"`
Title string `json:"title" gorm:"type:char(50);size:50;not null"`
Description string `json:"description" gorm:"type:text;not null"`
ResTimeLimit null.Time `json:"res_time_limit,omitempty" gorm:"type:TIMESTAMP NULL;default:NULL;"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"type:TIMESTAMP NULL;default:NULL;"`
ResSharedTo string `json:"res_shared_to" gorm:"type:char(30);size:30;not null;default:administrators"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
ModifiedAt time.Time `json:"modified_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
Administrators []Administrators `json:"-" gorm:"foreignKey:QuestionnaireID"`
Targets []Targets `json:"-" gorm:"foreignKey:QuestionnaireID"`
Questions []Questions `json:"-" gorm:"foreignKey:QuestionnaireID"`
Respondents []Respondents `json:"-" gorm:"foreignKey:QuestionnaireID"`
ID int `json:"questionnaireID" gorm:"type:int(11) AUTO_INCREMENT;not null;primaryKey"`
Title string `json:"title" gorm:"type:char(50);size:50;not null"`
Description string `json:"description" gorm:"type:text;not null"`
ResTimeLimit null.Time `json:"res_time_limit,omitempty" gorm:"type:TIMESTAMP NULL;default:NULL;"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"type:TIMESTAMP NULL;default:NULL;"`
ResSharedTo string `json:"res_shared_to" gorm:"type:char(30);size:30;not null;default:administrators"`
IsDuplicateAnswerAllowed bool `json:"is_duplicate_answer_allowed" gorm:"type:tinyint(4);size:4;not null;default:0"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
ModifiedAt time.Time `json:"modified_at" gorm:"type:timestamp;not null;default:CURRENT_TIMESTAMP"`
Administrators []Administrators `json:"-" gorm:"foreignKey:QuestionnaireID"`
Targets []Targets `json:"-" gorm:"foreignKey:QuestionnaireID"`
Questions []Questions `json:"-" gorm:"foreignKey:QuestionnaireID"`
Respondents []Respondents `json:"-" gorm:"foreignKey:QuestionnaireID"`
}

// BeforeCreate Update時に自動でmodified_atを現在時刻に
Expand All @@ -44,28 +45,28 @@ func (questionnaire *Questionnaires) BeforeCreate(tx *gorm.DB) error {
return nil
}

//BeforeUpdate Update時に自動でmodified_atを現在時刻に
// BeforeUpdate Update時に自動でmodified_atを現在時刻に
func (questionnaire *Questionnaires) BeforeUpdate(tx *gorm.DB) error {
questionnaire.ModifiedAt = time.Now()

return nil
}

//QuestionnaireInfo Questionnaireにtargetかの情報追加
// QuestionnaireInfo Questionnaireにtargetかの情報追加
type QuestionnaireInfo struct {
Questionnaires
IsTargeted bool `json:"is_targeted" gorm:"type:boolean"`
}

//QuestionnaireDetail Questionnaireの詳細
// QuestionnaireDetail Questionnaireの詳細
type QuestionnaireDetail struct {
Targets []string
Respondents []string
Administrators []string
Questionnaires
}

//TargettedQuestionnaire targetになっているアンケートの情報
// TargettedQuestionnaire targetになっているアンケートの情報
type TargettedQuestionnaire struct {
Questionnaires
RespondedAt null.Time `json:"responded_at"`
Expand All @@ -78,8 +79,8 @@ type ResponseReadPrivilegeInfo struct {
IsRespondent bool
}

//InsertQuestionnaire アンケートの追加
func (*Questionnaire) InsertQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string) (int, error) {
// InsertQuestionnaire アンケートの追加
func (*Questionnaire) InsertQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, isDuplicateAnswerAllowed bool) (int, error) {
db, err := getTx(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get tx: %w", err)
Expand All @@ -88,16 +89,18 @@ func (*Questionnaire) InsertQuestionnaire(ctx context.Context, title string, des
var questionnaire Questionnaires
if !resTimeLimit.Valid {
questionnaire = Questionnaires{
Title: title,
Description: description,
ResSharedTo: resSharedTo,
Title: title,
Description: description,
ResSharedTo: resSharedTo,
IsDuplicateAnswerAllowed: isDuplicateAnswerAllowed,
}
} else {
questionnaire = Questionnaires{
Title: title,
Description: description,
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
Title: title,
Description: description,
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
IsDuplicateAnswerAllowed: isDuplicateAnswerAllowed,
}
}

Expand All @@ -109,8 +112,8 @@ func (*Questionnaire) InsertQuestionnaire(ctx context.Context, title string, des
return questionnaire.ID, nil
}

//UpdateQuestionnaire アンケートの更新
func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int) error {
// UpdateQuestionnaire アンケートの更新
func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, isDuplicateAnswerAllowed bool, questionnaireID int) error {
db, err := getTx(ctx)
if err != nil {
return fmt.Errorf("failed to get tx: %w", err)
Expand All @@ -119,17 +122,19 @@ func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, des
var questionnaire interface{}
if resTimeLimit.Valid {
questionnaire = Questionnaires{
Title: title,
Description: description,
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
Title: title,
Description: description,
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
IsDuplicateAnswerAllowed: isDuplicateAnswerAllowed,
}
} else {
questionnaire = map[string]interface{}{
"title": title,
"description": description,
"res_time_limit": gorm.Expr("NULL"),
"res_shared_to": resSharedTo,
"title": title,
"description": description,
"res_time_limit": gorm.Expr("NULL"),
"res_shared_to": resSharedTo,
"is_duplicate_answer_allowed": isDuplicateAnswerAllowed,
}
}

Expand All @@ -148,7 +153,7 @@ func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, des
return nil
}

//DeleteQuestionnaire アンケートの削除
// DeleteQuestionnaire アンケートの削除
func (*Questionnaire) DeleteQuestionnaire(ctx context.Context, questionnaireID int) error {
db, err := getTx(ctx)
if err != nil {
Expand All @@ -167,8 +172,10 @@ func (*Questionnaire) DeleteQuestionnaire(ctx context.Context, questionnaireID i
return nil
}

/*GetQuestionnaires アンケートの一覧
2つ目の戻り値はページ数の最大値*/
/*
GetQuestionnaires アンケートの一覧
2つ目の戻り値はページ数の最大値
*/
func (*Questionnaire) GetQuestionnaires(ctx context.Context, userID string, sort string, search string, pageNum int, nontargeted bool) ([]QuestionnaireInfo, int, error) {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
Expand Down Expand Up @@ -263,7 +270,7 @@ func (*Questionnaire) GetAdminQuestionnaires(ctx context.Context, userID string)
return questionnaires, nil
}

//GetQuestionnaireInfo アンケートの詳細な情報取得
// GetQuestionnaireInfo アンケートの詳細な情報取得
func (*Questionnaire) GetQuestionnaireInfo(ctx context.Context, questionnaireID int) (*Questionnaires, []string, []string, []string, error) {
db, err := getTx(ctx)
if err != nil {
Expand Down Expand Up @@ -315,7 +322,7 @@ func (*Questionnaire) GetQuestionnaireInfo(ctx context.Context, questionnaireID
return &questionnaire, targets, administrators, respondents, nil
}

//GetTargettedQuestionnaires targetになっているアンケートの取得
// GetTargettedQuestionnaires targetになっているアンケートの取得
func (*Questionnaire) GetTargettedQuestionnaires(ctx context.Context, userID string, answered string, sort string) ([]TargettedQuestionnaire, error) {
db, err := getTx(ctx)
if err != nil {
Expand Down Expand Up @@ -359,7 +366,7 @@ func (*Questionnaire) GetTargettedQuestionnaires(ctx context.Context, userID str
return questionnaires, nil
}

//GetQuestionnaireLimit アンケートの回答期限の取得
// GetQuestionnaireLimit アンケートの回答期限の取得
func (*Questionnaire) GetQuestionnaireLimit(ctx context.Context, questionnaireID int) (null.Time, error) {
db, err := getTx(ctx)
if err != nil {
Expand Down
54 changes: 31 additions & 23 deletions model/questionnaires_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@ func insertQuestionnaireTest(t *testing.T) {
assertion := assert.New(t)

type args struct {
title string
description string
resTimeLimit null.Time
resSharedTo string
title string
description string
resTimeLimit null.Time
resSharedTo string
isDuplicateAnswerAllowed bool
}
type expect struct {
isErr bool
Expand Down Expand Up @@ -451,7 +452,7 @@ func insertQuestionnaireTest(t *testing.T) {
for _, testCase := range testCases {
ctx := context.Background()

questionnaireID, err := questionnaireImpl.InsertQuestionnaire(ctx, testCase.args.title, testCase.args.description, testCase.args.resTimeLimit, testCase.args.resSharedTo)
questionnaireID, err := questionnaireImpl.InsertQuestionnaire(ctx, testCase.args.title, testCase.args.description, testCase.args.resTimeLimit, testCase.args.resSharedTo, testCase.args.isDuplicateAnswerAllowed)

if !testCase.expect.isErr {
assertion.NoError(err, testCase.description, "no error")
Expand All @@ -475,6 +476,7 @@ func insertQuestionnaireTest(t *testing.T) {
assertion.Equal(testCase.args.description, questionnaire.Description, testCase.description, "description")
assertion.WithinDuration(testCase.args.resTimeLimit.ValueOrZero(), questionnaire.ResTimeLimit.ValueOrZero(), 2*time.Second, testCase.description, "res_time_limit")
assertion.Equal(testCase.args.resSharedTo, questionnaire.ResSharedTo, testCase.description, "res_shared_to")
assertion.Equal(testCase.args.isDuplicateAnswerAllowed, questionnaire.IsDuplicateAnswerAllowed, testCase.description, "is_duplicate_answer_allowed")

assertion.WithinDuration(time.Now(), questionnaire.CreatedAt, 2*time.Second, testCase.description, "created_at")
assertion.WithinDuration(time.Now(), questionnaire.ModifiedAt, 2*time.Second, testCase.description, "modified_at")
Expand All @@ -488,10 +490,11 @@ func updateQuestionnaireTest(t *testing.T) {
assertion := assert.New(t)

type args struct {
title string
description string
resTimeLimit null.Time
resSharedTo string
title string
description string
resTimeLimit null.Time
resSharedTo string
isDuplicateAnswerAllowed bool
}
type expect struct {
isErr bool
Expand Down Expand Up @@ -648,10 +651,11 @@ func updateQuestionnaireTest(t *testing.T) {

before := &testCase.before
questionnaire := Questionnaires{
Title: before.title,
Description: before.description,
ResTimeLimit: before.resTimeLimit,
ResSharedTo: before.resSharedTo,
Title: before.title,
Description: before.description,
ResTimeLimit: before.resTimeLimit,
ResSharedTo: before.resSharedTo,
IsDuplicateAnswerAllowed: before.isDuplicateAnswerAllowed,
}
err := db.
Session(&gorm.Session{NewDB: true}).
Expand All @@ -663,7 +667,7 @@ func updateQuestionnaireTest(t *testing.T) {
createdAt := questionnaire.CreatedAt
questionnaireID := questionnaire.ID
after := &testCase.after
err = questionnaireImpl.UpdateQuestionnaire(ctx, after.title, after.description, after.resTimeLimit, after.resSharedTo, questionnaireID)
err = questionnaireImpl.UpdateQuestionnaire(ctx, after.title, after.description, after.resTimeLimit, after.resSharedTo, after.isDuplicateAnswerAllowed, questionnaireID)

if !testCase.expect.isErr {
assertion.NoError(err, testCase.description, "no error")
Expand All @@ -687,6 +691,7 @@ func updateQuestionnaireTest(t *testing.T) {
assertion.Equal(after.description, questionnaire.Description, testCase.description, "description")
assertion.WithinDuration(after.resTimeLimit.ValueOrZero(), questionnaire.ResTimeLimit.ValueOrZero(), 2*time.Second, testCase.description, "res_time_limit")
assertion.Equal(after.resSharedTo, questionnaire.ResSharedTo, testCase.description, "res_shared_to")
assertion.Equal(after.isDuplicateAnswerAllowed, questionnaire.IsDuplicateAnswerAllowed, testCase.description, "is_duplicate_answer_allowed")

assertion.WithinDuration(createdAt, questionnaire.CreatedAt, 2*time.Second, testCase.description, "created_at")
assertion.WithinDuration(time.Now(), questionnaire.ModifiedAt, 2*time.Second, testCase.description, "modified_at")
Expand Down Expand Up @@ -727,7 +732,7 @@ func updateQuestionnaireTest(t *testing.T) {
for _, arg := range invalidTestCases {
ctx := context.Background()

err := questionnaireImpl.UpdateQuestionnaire(ctx, arg.title, arg.description, arg.resTimeLimit, arg.resSharedTo, invalidQuestionnaireID)
err := questionnaireImpl.UpdateQuestionnaire(ctx, arg.title, arg.description, arg.resTimeLimit, arg.resSharedTo, arg.isDuplicateAnswerAllowed, invalidQuestionnaireID)
if !errors.Is(err, ErrNoRecordUpdated) {
if err == nil {
t.Errorf("Succeeded with invalid questionnaireID")
Expand All @@ -745,10 +750,11 @@ func deleteQuestionnaireTest(t *testing.T) {
assertion := assert.New(t)

type args struct {
title string
description string
resTimeLimit null.Time
resSharedTo string
title string
description string
resTimeLimit null.Time
resSharedTo string
isDuplicateAnswerAllowed bool
}
type expect struct {
isErr bool
Expand All @@ -774,10 +780,11 @@ func deleteQuestionnaireTest(t *testing.T) {
ctx := context.Background()

questionnaire := Questionnaires{
Title: testCase.args.title,
Description: testCase.args.description,
ResTimeLimit: testCase.args.resTimeLimit,
ResSharedTo: testCase.args.resSharedTo,
Title: testCase.args.title,
Description: testCase.args.description,
ResTimeLimit: testCase.args.resTimeLimit,
ResSharedTo: testCase.args.resSharedTo,
IsDuplicateAnswerAllowed: testCase.args.isDuplicateAnswerAllowed,
}
err := db.
Session(&gorm.Session{NewDB: true}).
Expand Down Expand Up @@ -1334,6 +1341,7 @@ func getQuestionnaireInfoTest(t *testing.T) {
assertion.Equal(testCase.expect.questionnaire.Title, actualQuestionnaire.Title, testCase.description, "questionnaire(Title)")
assertion.Equal(testCase.expect.questionnaire.Description, actualQuestionnaire.Description, testCase.description, "questionnaire(Description)")
assertion.Equal(testCase.expect.questionnaire.ResSharedTo, actualQuestionnaire.ResSharedTo, testCase.description, "questionnaire(ResSharedTo)")
assertion.Equal(testCase.expect.questionnaire.IsDuplicateAnswerAllowed, actualQuestionnaire.IsDuplicateAnswerAllowed, testCase.description, "questionnaire(IsDuplicateAnswerAllowed)")
assertion.WithinDuration(testCase.expect.questionnaire.ResTimeLimit.ValueOrZero(), actualQuestionnaire.ResTimeLimit.ValueOrZero(), 2*time.Second, testCase.description, "questionnaire(ResTimeLimit)")
assertion.WithinDuration(testCase.expect.questionnaire.CreatedAt, actualQuestionnaire.CreatedAt, 2*time.Second, testCase.description, "questionnaire(CreatedAt)")
assertion.WithinDuration(testCase.expect.questionnaire.ModifiedAt, actualQuestionnaire.ModifiedAt, 2*time.Second, testCase.description, "questionnaire(ModifiedAt)")
Expand Down
Loading

0 comments on commit d62152f

Please sign in to comment.