Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

質問の下書き機能の実装 #1216

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/db_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
| res_shared_to | char(30) | NO | | administrators | | アンケートの結果を, 運営は見られる ("administrators"), 回答済みの人は見られる ("respondents") 誰でも見られる ("public") |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが作成された日時 |
| modified_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが更新された日時 |
| is_published | boolean | NO | | false | | アンケートが公開かどうか |

### respondents

Expand Down
4 changes: 3 additions & 1 deletion model/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

// Migrations is all db migrations
func Migrations() []*gormigrate.Migration {
return []*gormigrate.Migration{}
return []*gormigrate.Migration{
v3(),
}
}

func AllTables() []interface{} {
Expand Down
5 changes: 3 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, isPublished bool) (int, error)
UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int, isPublished bool) 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 All @@ -21,4 +21,5 @@ type IQuestionnaire interface {
GetQuestionnaireLimitByResponseID(ctx context.Context, responseID int) (null.Time, error)
GetResponseReadPrivilegeInfoByResponseID(ctx context.Context, userID string, responseID int) (*ResponseReadPrivilegeInfo, error)
GetResponseReadPrivilegeInfoByQuestionnaireID(ctx context.Context, userID string, questionnaireID int) (*ResponseReadPrivilegeInfo, error)
CheckQuestionnairePublished(ctx context.Context, questionnaireID int) (bool, error)
}
32 changes: 29 additions & 3 deletions model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Questionnaires struct {
Targets []Targets `json:"-" gorm:"foreignKey:QuestionnaireID"`
Questions []Questions `json:"-" gorm:"foreignKey:QuestionnaireID"`
Respondents []Respondents `json:"-" gorm:"foreignKey:QuestionnaireID"`
IsPublished bool `json:"is_published" gorm:"type:boolean;default:false"`
}

// BeforeCreate Update時に自動でmodified_atを現在時刻に
Expand Down Expand Up @@ -79,7 +80,7 @@ type ResponseReadPrivilegeInfo struct {
}

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

Expand All @@ -110,7 +113,7 @@ func (*Questionnaire) InsertQuestionnaire(ctx context.Context, title string, des
}

// UpdateQuestionnaire アンケートの更新
func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int) error {
func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int, isPublished bool) error {
db, err := getTx(ctx)
if err != nil {
return fmt.Errorf("failed to get tx: %w", err)
Expand All @@ -123,13 +126,15 @@ func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, des
Description: description,
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
IsPublished: isPublished,
}
} else {
questionnaire = map[string]interface{}{
"title": title,
"description": description,
"res_time_limit": gorm.Expr("NULL"),
"res_shared_to": resSharedTo,
"is_published": isPublished,
}
}

Expand Down Expand Up @@ -184,7 +189,7 @@ func (*Questionnaire) GetQuestionnaires(ctx context.Context, userID string, sort

query := db.
Table("questionnaires").
Where("deleted_at IS NULL").
Where("deleted_at IS NULL AND is_published IS TRUE").
Joins("LEFT OUTER JOIN targets ON questionnaires.id = targets.questionnaire_id")

query, err = setQuestionnairesOrder(query, sort)
Expand Down Expand Up @@ -458,6 +463,27 @@ func (*Questionnaire) GetResponseReadPrivilegeInfoByQuestionnaireID(ctx context.
return &responseReadPrivilegeInfo, nil
}

func (*Questionnaire) CheckQuestionnairePublished(ctx context.Context, questionnaireID int) (bool, error) {
db, err := getTx(ctx)
if err != nil {
return false, fmt.Errorf("failed to get tx: %w", err)
}

var questionnaire Questionnaires
err = db.
Where("id = ?", questionnaireID).
Select("is_published").
First(&questionnaire).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, ErrRecordNotFound
}
if err != nil {
return false, fmt.Errorf("failed to get the questionnaires: %w", err)
}

return questionnaire.IsPublished, nil
}

func setQuestionnairesOrder(query *gorm.DB, sort string) (*gorm.DB, error) {
switch sort {
case "created_at":
Expand Down
Loading
Loading