From 7a7fc48454ef2a65a63278301d0f77ed6d8bf756 Mon Sep 17 00:00:00 2001 From: kaitoyama <45167401+kaitoyama@users.noreply.github.com> Date: Tue, 14 May 2024 15:25:42 +0900 Subject: [PATCH] until deletequestionnaire --- controller/adapter.go | 4 +- controller/questionnaire.go | 120 +++++++++++++++++++++++++++++++++++- controller/utils.go | 2 +- handler/questionnaire.go | 39 +++++++++++- model/questionnaires.go | 2 +- model/targetGroups.go | 2 +- 6 files changed, 161 insertions(+), 8 deletions(-) diff --git a/controller/adapter.go b/controller/adapter.go index 05cdfc70..1b6bbc3a 100644 --- a/controller/adapter.go +++ b/controller/adapter.go @@ -144,7 +144,7 @@ func convertRespondents(respondents []model.Respondents) []string { return res } -func questionnaire2QuestionnaireDetail(questionnaires model.Questionnaires, adminUsers []string, adminGroups []string, targetUsers []string, targetGroups []string) openapi.QuestionnaireDetail { +func questionnaire2QuestionnaireDetail(questionnaires model.Questionnaires, adminUsers []string, adminGroups []string, targetUsers []string, targetGroups []string, respondents []string) openapi.QuestionnaireDetail { res := openapi.QuestionnaireDetail{ Admins: createUsersAndGroups(adminUsers, adminGroups), CreatedAt: questionnaires.CreatedAt, @@ -155,7 +155,7 @@ func questionnaire2QuestionnaireDetail(questionnaires model.Questionnaires, admi ModifiedAt: questionnaires.ModifiedAt, QuestionnaireId: questionnaires.ID, Questions: convertQuestions(questionnaires.Questions), - Respondents: convertRespondents(questionnaires.Respondents), + Respondents: respondents, ResponseDueDateTime: &questionnaires.ResTimeLimit.Time, ResponseViewableBy: convertResSharedTo(questionnaires.ResSharedTo), Targets: createUsersAndGroups(targetUsers, targetGroups), diff --git a/controller/questionnaire.go b/controller/questionnaire.go index 43835490..22cef948 100644 --- a/controller/questionnaire.go +++ b/controller/questionnaire.go @@ -2,6 +2,7 @@ package controller import ( "context" + "errors" "fmt" "net/http" "strings" @@ -184,15 +185,130 @@ func (q Questionnaire) PostQuestionnaire(c echo.Context, userID string, params o c.Logger().Errorf("failed to create a questionnaire: %+v", err) return openapi.QuestionnaireDetail{}, echo.NewHTTPError(http.StatusInternalServerError, "failed to create a questionnaire") } - questionnaireInfo, _, _, _, err := q.GetQuestionnaireInfo(c.Request().Context(), questionnaireID) + questionnaireInfo, targets, targetGroups, admins, adminGroups, respondents, err := q.GetQuestionnaireInfo(c.Request().Context(), questionnaireID) if err != nil { c.Logger().Errorf("failed to get questionnaire info: %+v", err) return openapi.QuestionnaireDetail{}, echo.NewHTTPError(http.StatusInternalServerError, "failed to get questionnaire info") } - questionnaireDetail := questionnaire2QuestionnaireDetail(*questionnaireInfo, params.Admins.Users, params.Admins.Groups, params.Targets.Users, params.Targets.Groups) + questionnaireDetail := questionnaire2QuestionnaireDetail(*questionnaireInfo, admins, adminGroups, targets, targetGroups, respondents) return questionnaireDetail, nil } +func (q Questionnaire) GetQuestionnaire(ctx echo.Context, questionnaireID int) (openapi.QuestionnaireDetail, error) { + questionnaireInfo, targets, targetGroups, admins, adminGroups, respondents, err := q.GetQuestionnaireInfo(ctx.Request().Context(), questionnaireID) + if err != nil { + return openapi.QuestionnaireDetail{}, err + } + questionnaireDetail := questionnaire2QuestionnaireDetail(*questionnaireInfo, admins, adminGroups, targets, targetGroups, respondents) + return questionnaireDetail, nil +} + +func (q Questionnaire) EditQuestionnaire(c echo.Context, questionnaireID int, params openapi.EditQuestionnaireJSONRequestBody) error { + 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) + if err != nil && !errors.Is(err, model.ErrNoRecordUpdated) { + c.Logger().Errorf("failed to update questionnaire: %+v", err) + return err + } + err = q.DeleteTargets(ctx, questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete targets: %+v", err) + return err + } + err = q.DeleteTargetGroups(ctx, questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete target groups: %+v", err) + return err + } + allTargetUsers, err := rollOutUsersAndGroups(params.Targets.Users, params.Targets.Groups) + if err != nil { + c.Logger().Errorf("failed to roll out users and groups: %+v", err) + return err + } + err = q.InsertTargets(ctx, questionnaireID, allTargetUsers) + if err != nil { + c.Logger().Errorf("failed to insert targets: %+v", err) + return err + } + err = q.InsertTargetGroups(ctx, questionnaireID, params.Targets.Groups) + if err != nil { + c.Logger().Errorf("failed to insert target groups: %+v", err) + return err + } + err = q.DeleteAdministrators(ctx, questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete administrators: %+v", err) + return err + } + err = q.DeleteAdministratorGroups(ctx, questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete administrator groups: %+v", err) + return err + } + allAdminUsers, err := rollOutUsersAndGroups(params.Admins.Users, params.Admins.Groups) + if err != nil { + c.Logger().Errorf("failed to roll out administrators: %+v", err) + return err + } + err = q.InsertAdministrators(ctx, questionnaireID, allAdminUsers) + if err != nil { + c.Logger().Errorf("failed to insert administrators: %+v", err) + return err + } + err = q.InsertAdministratorGroups(ctx, questionnaireID, params.Admins.Groups) + if err != nil { + c.Logger().Errorf("failed to insert administrator groups: %+v", err) + return err + } + + return nil + }) + if err != nil { + c.Logger().Errorf("failed to update a questionnaire: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to update a questionnaire") + } + + return nil +} + +func (q Questionnaire) DeleteQuestionnaire(c echo.Context, questionnaireID int) error { + err := q.ITransaction.Do(c.Request().Context(), nil, func(ctx context.Context) error { + err := q.IQuestionnaire.DeleteQuestionnaire(c.Request().Context(), questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete questionnaire: %+v", err) + return err + } + + err = q.DeleteTargets(c.Request().Context(), questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete targets: %+v", err) + return err + } + + err = q.DeleteAdministrators(c.Request().Context(), questionnaireID) + if err != nil { + c.Logger().Errorf("failed to delete administrators: %+v", err) + return err + } + + return nil + }) + if err != nil { + var httpError *echo.HTTPError + if errors.As(err, &httpError) { + return httpError + } + + c.Logger().Errorf("failed to delete questionnaire: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to delete a questionnaire") + } + return nil +} func createQuestionnaireMessage(questionnaireID int, title string, description string, administrators []string, resTimeLimit null.Time, targets []string) string { var resTimeLimitText string diff --git a/controller/utils.go b/controller/utils.go index 121281b0..fe9df181 100644 --- a/controller/utils.go +++ b/controller/utils.go @@ -5,7 +5,7 @@ import ( "slices" mapset "github.com/deckarep/golang-set/v2" - "github.com/gofrs/uuid" + "github.com/google/uuid" "github.com/traPtitech/anke-to/model" "github.com/traPtitech/anke-to/traq" ) diff --git a/handler/questionnaire.go b/handler/questionnaire.go index 35630b62..bcb52285 100644 --- a/handler/questionnaire.go +++ b/handler/questionnaire.go @@ -37,6 +37,18 @@ func (h Handler) PostQuestionnaire(ctx echo.Context) error { } res := openapi.QuestionnaireDetail{} + q := controller.NewQuestionnaire() + userID, err := getUserID(ctx) + if err != nil { + ctx.Logger().Errorf("failed to get userID: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get userID: %w", err)) + } + + res, err = q.PostQuestionnaire(ctx, userID, params) + if err != nil { + ctx.Logger().Errorf("failed to post questionnaire: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to post questionnaire: %w", err)) + } return ctx.JSON(200, res) } @@ -44,17 +56,42 @@ func (h Handler) PostQuestionnaire(ctx echo.Context) error { // (GET /questionnaires/{questionnaireID}) func (h Handler) GetQuestionnaire(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error { res := openapi.QuestionnaireDetail{} - + q := controller.NewQuestionnaire() + res, err := q.GetQuestionnaire(ctx, questionnaireID) + if err != nil { + ctx.Logger().Errorf("failed to get questionnaire: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire: %w", err)) + } return ctx.JSON(200, res) } // (PATCH /questionnaires/{questionnaireID}) func (h Handler) EditQuestionnaire(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error { + params := openapi.EditQuestionnaireJSONRequestBody{} + if err := ctx.Bind(¶ms); err != nil { + ctx.Logger().Errorf("failed to bind request body: %+v", err) + return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err)) + } + + q := controller.NewQuestionnaire() + 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 ctx.NoContent(200) } // (DELETE /questionnaires/{questionnaireID}) func (h Handler) DeleteQuestionnaire(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error { + q := controller.NewQuestionnaire() + err := q.DeleteQuestionnaire(ctx, questionnaireID) + if err != nil { + ctx.Logger().Errorf("failed to delete questionnaire: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete questionnaire: %w", err)) + } + return ctx.NoContent(200) } diff --git a/model/questionnaires.go b/model/questionnaires.go index e7f30005..8338abbf 100644 --- a/model/questionnaires.go +++ b/model/questionnaires.go @@ -15,7 +15,7 @@ type IQuestionnaire interface { 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) - GetQuestionnaireInfo(ctx context.Context, questionnaireID int) (*Questionnaires, []string, []string, []string, error) + GetQuestionnaireInfo(ctx context.Context, questionnaireID int) (*Questionnaires, []string, []string, []string, []string, []string, error) GetTargettedQuestionnaires(ctx context.Context, userID string, answered string, sort string) ([]TargettedQuestionnaire, error) GetQuestionnaireLimit(ctx context.Context, questionnaireID int) (null.Time, error) GetQuestionnaireLimitByResponseID(ctx context.Context, responseID int) (null.Time, error) diff --git a/model/targetGroups.go b/model/targetGroups.go index dbb58abd..7217c148 100644 --- a/model/targetGroups.go +++ b/model/targetGroups.go @@ -8,5 +8,5 @@ import ( type ITargetGroup interface { InsertTargetGroups(ctx context.Context, questionnaireID int, groupID []string) error GetTargetGroups(ctx context.Context, questionnaireIDs []int) ([]TargetGroups, error) - DeleteTargetGroups(ctx context.Context, questionnaireIDs []int) error + DeleteTargetGroups(ctx context.Context, questionnaireIDs int) error }