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

匿名解答の追加(openapi) #1280

Merged
merged 14 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
11 changes: 9 additions & 2 deletions controller/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func questionnaire2QuestionnaireDetail(questionnaires model.Questionnaires, admi
CreatedAt: questionnaires.CreatedAt,
Description: questionnaires.Description,
// IsAllowingMultipleResponses: questionnaires.IsAllowingMultipleResponses,
// IsAnonymous: questionnaires.IsAnonymous,
// IsPublished: questionnaires.IsPublished,
IsAnonymous: questionnaires.IsAnonymous,
IsPublished: questionnaires.IsPublished,
ModifiedAt: questionnaires.ModifiedAt,
QuestionnaireId: questionnaires.ID,
Questions: convertQuestions(questionnaires.Questions),
Expand Down Expand Up @@ -264,6 +264,12 @@ func respondentDetail2Response(ctx echo.Context, respondentDetail model.Responde
oResponseBodies = append(oResponseBodies, oResponseBody)
}

isAnonymous, err := model.NewQuestionnaire().GetResponseIsAnonymousByQuestionnaireID(ctx.Request().Context(), respondentDetail.QuestionnaireID)
if err != nil {
ctx.Logger().Errorf("failed to get response is anonymous: %+v", err)
return openapi.Response{}, err
}

res := openapi.Response{
Body: oResponseBodies,
IsDraft: respondentDetail.SubmittedAt.Valid,
Expand All @@ -272,6 +278,7 @@ func respondentDetail2Response(ctx echo.Context, respondentDetail model.Responde
Respondent: &respondentDetail.TraqID,
ResponseId: respondentDetail.ResponseID,
SubmittedAt: respondentDetail.SubmittedAt.Time,
IsAnonymous: &isAnonymous,
}

return res, nil
Expand Down
20 changes: 17 additions & 3 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (q Questionnaire) PostQuestionnaire(c echo.Context, userID string, params o
questionnaireID := 0

err := q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error {
questionnaireID, err := q.InsertQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, convertResponseViewableBy(params.ResponseViewableBy), params.IsPublished)
questionnaireID, err := q.InsertQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, convertResponseViewableBy(params.ResponseViewableBy), params.IsPublished, params.IsAnonymous)
if err != nil {
c.Logger().Errorf("failed to insert questionnaire: %+v", err)
return err
Expand Down Expand Up @@ -296,13 +296,24 @@ func (q Questionnaire) GetQuestionnaire(ctx echo.Context, questionnaireID int) (
}

func (q Questionnaire) EditQuestionnaire(c echo.Context, questionnaireID int, params openapi.EditQuestionnaireJSONRequestBody) error {
// unable to change the questionnaire from anoymous to non-anonymous
isAnonymous, err := q.GetResponseIsAnonymousByQuestionnaireID(c.Request().Context(), questionnaireID)
if err != nil {
c.Logger().Errorf("failed to get anonymous info: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get anonymous info")
}
if isAnonymous && !params.IsAnonymous {
c.Logger().Info("unable to change the questionnaire from anoymous to non-anonymous")
return echo.NewHTTPError(http.StatusBadRequest, "unable to change the questionnaire from anoymous to non-anonymous")
}

responseDueDateTime := null.Time{}
if params.ResponseDueDateTime != nil {
responseDueDateTime.Valid = true
responseDueDateTime.Time = *params.ResponseDueDateTime
}
err := q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error {
err := q.UpdateQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, string(params.ResponseViewableBy), questionnaireID, params.IsPublished)
err = q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error {
err := q.UpdateQuestionnaire(ctx, params.Title, params.Description, responseDueDateTime, string(params.ResponseViewableBy), questionnaireID, params.IsPublished, params.IsAnonymous)
if err != nil && !errors.Is(err, model.ErrNoRecordUpdated) {
c.Logger().Errorf("failed to update questionnaire: %+v", err)
return err
Expand Down Expand Up @@ -656,13 +667,16 @@ func (q Questionnaire) PostQuestionnaireResponse(c echo.Context, questionnaireID
}
}

isAnonymous, err := q.GetResponseIsAnonymousByQuestionnaireID(c.Request().Context(), questionnaireID)

res = openapi.Response{
QuestionnaireId: questionnaireID,
ResponseId: resopnseID,
Respondent: &userID,
SubmittedAt: submittedAt,
ModifiedAt: modifiedAt,
IsDraft: params.IsDraft,
IsAnonymous: &isAnonymous,
Body: params.Body,
}

Expand Down
5 changes: 3 additions & 2 deletions controller/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (r Response) GetMyResponses(ctx echo.Context, params openapi.GetMyResponses
Respondent: &userID,
ResponseId: response.ResponseId,
SubmittedAt: response.SubmittedAt,
IsAnonymous: response.IsAnonymous,
}
res = append(res, tmp)
}
Expand Down Expand Up @@ -244,6 +245,6 @@ func (r Response) EditResponse(ctx echo.Context, responseID openapi.ResponseIDIn
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to insert responses: %w", err))
}
}

return nil
}
}
3 changes: 2 additions & 1 deletion docs/db_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
| res_time_limit | timestamp | YES | | _NULL_ | | 回答の締切日時 (締切がない場合は NULL) |
| deleted_at | timestamp | YES | | _NULL_ | | アンケートが削除された日時 (削除されていない場合は NULL) |
| res_shared_to | char(30) | NO | | administrators | | アンケートの結果を, 運営は見られる ("administrators"), 回答済みの人は見られる ("respondents") 誰でも見られる ("public") |
| is_anonymous | boolean | NO | | false | | アンケートが匿名解答かどうか |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが作成された日時 |
| modified_at | timestamp | NO | | CURRENT_TIMESTAMP | | アンケートが更新された日時 |
| is_published | bookean | NO | | false | | アンケートが公開かどうか
| is_published | boolean | NO | | false | | アンケートが公開かどうか |

### respondents

Expand Down
4 changes: 3 additions & 1 deletion docs/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ paths: # TODO 変数の命名を確認する
operationId: editQuestionnaire
tags:
- questionnaire
description: アンケートの情報を変更します。
description: アンケートの情報を変更します。匿名のアンケートを非匿名アンケートに変更することができません。
parameters:
- $ref: "#/components/parameters/questionnaireIDInPath"
requestBody:
Expand All @@ -105,6 +105,8 @@ paths: # TODO 変数の命名を確認する
description: 正常にアンケートを変更できました。
"400":
description: アンケートのIDが無効です
"405":
description: 匿名のアンケートを非匿名アンケートに変更することができません
"500":
description: 正常にアンケートを変更できませんでした
delete:
Expand Down
2 changes: 1 addition & 1 deletion handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (h Handler) EditQuestionnaire(ctx echo.Context, questionnaireID openapi.Que
err := q.EditQuestionnaire(ctx, questionnaireID, params)
if err != nil {
ctx.Logger().Errorf("failed to edit questionnaire: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to edit questionnaire: %w", err))
return err
}

return ctx.NoContent(200)
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, isPublished bool) (int, error)
UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int, isPublished bool) error
InsertQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, isPublished bool, isAnonymous bool) (int, error)
UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int, isPublished bool, isAnonymous bool) error
DeleteQuestionnaire(ctx context.Context, questionnaireID int) error
GetQuestionnaires(ctx context.Context, userID string, sort string, search string, pageNum int, onlyTargetingMe bool, onlyAdministratedByMe 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)
GetResponseIsAnonymousByQuestionnaireID(ctx context.Context, questionnaireID int) (bool, error)
}
31 changes: 29 additions & 2 deletions model/questionnaires_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Questionnaires struct {
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"`
IsAnonymous bool `json:"is_anonymous" gorm:"type:boolean;not null;default:false"`
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"`
Expand Down Expand Up @@ -82,7 +83,7 @@ type ResponseReadPrivilegeInfo struct {
}

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

Expand All @@ -115,7 +118,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, isPublished bool) error {
func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, description string, resTimeLimit null.Time, resSharedTo string, questionnaireID int, isPublished bool, isAnonymous bool) error {
db, err := getTx(ctx)
if err != nil {
return fmt.Errorf("failed to get tx: %w", err)
Expand All @@ -129,6 +132,7 @@ func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, des
ResTimeLimit: resTimeLimit,
ResSharedTo: resSharedTo,
IsPublished: isPublished,
IsAnonymous: isAnonymous,
}
} else {
questionnaire = map[string]interface{}{
Expand All @@ -137,6 +141,7 @@ func (*Questionnaire) UpdateQuestionnaire(ctx context.Context, title string, des
"res_time_limit": gorm.Expr("NULL"),
"res_shared_to": resSharedTo,
"is_published": isPublished,
"is_anonymous": isAnonymous,
}
}

Expand Down Expand Up @@ -476,6 +481,28 @@ func (*Questionnaire) GetResponseReadPrivilegeInfoByQuestionnaireID(ctx context.
return &responseReadPrivilegeInfo, nil
}

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

var isAnonymous bool
err = db.
Table("questionnaires").
Where("questionnaires.id = ?", questionnaireID).
Select("questionnaires.is_anonymous").
Take(&isAnonymous).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return true, ErrRecordNotFound
}
if err != nil {
return true, fmt.Errorf("failed to get is_anonymous: %w", err)
}

return isAnonymous, nil
}

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