Skip to content

Commit

Permalink
until deletequestionnaire
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoyama committed May 14, 2024
1 parent 76acd76 commit 7a7fc48
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 8 deletions.
4 changes: 2 additions & 2 deletions controller/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
120 changes: 118 additions & 2 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -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)

Check failure on line 188 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / Lint

q.GetQuestionnaireInfo undefined (type Questionnaire has no field or method GetQuestionnaireInfo) (typecheck)

Check failure on line 188 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L188

q.GetQuestionnaireInfo undefined (type Questionnaire has no field or method GetQuestionnaireInfo) (typecheck)
Raw output
controller/questionnaire.go:188:87: q.GetQuestionnaireInfo undefined (type Questionnaire has no field or method GetQuestionnaireInfo) (typecheck)
	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)

Check failure on line 198 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L198

q.GetQuestionnaireInfo undefined (type Questionnaire has no field or method GetQuestionnaireInfo) (typecheck)
Raw output
controller/questionnaire.go:198:87: q.GetQuestionnaireInfo undefined (type Questionnaire has no field or method GetQuestionnaireInfo) (typecheck)
	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)

Check failure on line 213 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L213

q.UpdateQuestionnaire undefined (type Questionnaire has no field or method UpdateQuestionnaire) (typecheck)
Raw output
controller/questionnaire.go:213:12: q.UpdateQuestionnaire undefined (type Questionnaire has no field or method UpdateQuestionnaire) (typecheck)
		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)

Check failure on line 218 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L218

q.DeleteTargets undefined (type Questionnaire has no field or method DeleteTargets) (typecheck)
Raw output
controller/questionnaire.go:218:11: q.DeleteTargets undefined (type Questionnaire has no field or method DeleteTargets) (typecheck)
		err = q.DeleteTargets(ctx, questionnaireID)
		        ^
if err != nil {
c.Logger().Errorf("failed to delete targets: %+v", err)
return err
}
err = q.DeleteTargetGroups(ctx, questionnaireID)

Check failure on line 223 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L223

q.DeleteTargetGroups undefined (type Questionnaire has no field or method DeleteTargetGroups) (typecheck)
Raw output
controller/questionnaire.go:223:11: q.DeleteTargetGroups undefined (type Questionnaire has no field or method DeleteTargetGroups) (typecheck)
		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)

Check failure on line 233 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L233

q.InsertTargets undefined (type Questionnaire has no field or method InsertTargets) (typecheck)
Raw output
controller/questionnaire.go:233:11: q.InsertTargets undefined (type Questionnaire has no field or method InsertTargets) (typecheck)
		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)

Check failure on line 238 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L238

q.InsertTargetGroups undefined (type Questionnaire has no field or method InsertTargetGroups) (typecheck)
Raw output
controller/questionnaire.go:238:11: q.InsertTargetGroups undefined (type Questionnaire has no field or method InsertTargetGroups) (typecheck)
		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)

Check failure on line 243 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L243

q.DeleteAdministrators undefined (type Questionnaire has no field or method DeleteAdministrators) (typecheck)
Raw output
controller/questionnaire.go:243:11: q.DeleteAdministrators undefined (type Questionnaire has no field or method DeleteAdministrators) (typecheck)
		err = q.DeleteAdministrators(ctx, questionnaireID)
		        ^
if err != nil {
c.Logger().Errorf("failed to delete administrators: %+v", err)
return err
}
err = q.DeleteAdministratorGroups(ctx, questionnaireID)

Check failure on line 248 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L248

q.DeleteAdministratorGroups undefined (type Questionnaire has no field or method DeleteAdministratorGroups) (typecheck)
Raw output
controller/questionnaire.go:248:11: q.DeleteAdministratorGroups undefined (type Questionnaire has no field or method DeleteAdministratorGroups) (typecheck)
		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)

Check failure on line 258 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L258

q.InsertAdministrators undefined (type Questionnaire has no field or method InsertAdministrators) (typecheck)
Raw output
controller/questionnaire.go:258:11: q.InsertAdministrators undefined (type Questionnaire has no field or method InsertAdministrators) (typecheck)
		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)

Check failure on line 263 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L263

q.InsertAdministratorGroups undefined (type Questionnaire has no field or method InsertAdministratorGroups) (typecheck)
Raw output
controller/questionnaire.go:263:11: q.InsertAdministratorGroups undefined (type Questionnaire has no field or method InsertAdministratorGroups) (typecheck)
		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)

Check failure on line 287 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L287

q.DeleteTargets undefined (type Questionnaire has no field or method DeleteTargets) (typecheck)
Raw output
controller/questionnaire.go:287:11: q.DeleteTargets undefined (type Questionnaire has no field or method DeleteTargets) (typecheck)
		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)

Check failure on line 293 in controller/questionnaire.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] controller/questionnaire.go#L293

q.DeleteAdministrators undefined (type Questionnaire has no field or method DeleteAdministrators) (typecheck)
Raw output
controller/questionnaire.go:293:11: q.DeleteAdministrators undefined (type Questionnaire has no field or method DeleteAdministrators) (typecheck)
		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
Expand Down
2 changes: 1 addition & 1 deletion controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"slices"

Check failure on line 5 in controller/utils.go

View workflow job for this annotation

GitHub Actions / Build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/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"
)
Expand Down
39 changes: 38 additions & 1 deletion handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,61 @@ 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)
}

// (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(&params); 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)
}

Expand Down
2 changes: 1 addition & 1 deletion model/questionnaires.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion model/targetGroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7a7fc48

Please sign in to comment.