-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: implement GetQuestionnaireResult, GetMyResponses, GetResponse and DeleteResponse #1265
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2e96d61
feat: implement GetQuestionnaireResult
Eraxyso af155ec
feat: implement GetMyResponses(wip)
Eraxyso 1b700da
feat: implement GetResponse
Eraxyso b092327
feat: implement DeleteResponse
Eraxyso 63892f4
feat: implement GetMyResponses(complete)
Eraxyso e21b78d
fix: fix some errors, optimize some implementations and add existence…
Eraxyso bd06106
style: add missing space
Eraxyso 54e993d
style: format code
Eraxyso 1878011
style: remove unused import
Eraxyso 7cba992
fix: specify model of db request in functions respondents/GetMyRespon…
Eraxyso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package controller | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/traPtitech/anke-to/model" | ||
"github.com/traPtitech/anke-to/openapi" | ||
) | ||
|
||
// Response Responseの構造体 | ||
type Response struct { | ||
model.IQuestionnaire | ||
model.IRespondent | ||
model.IResponse | ||
model.ITarget | ||
} | ||
|
||
func NewResponse() *Response { | ||
return &Response{} | ||
} | ||
|
||
func (r Response) GetMyResponses(ctx echo.Context, params openapi.GetMyResponsesParams, userID string) (openapi.ResponsesWithQuestionnaireInfo, error) { | ||
res := openapi.ResponsesWithQuestionnaireInfo{} | ||
|
||
sort := string(*params.Sort) | ||
responsesID := []int{} | ||
responsesID, err := r.IRespondent.GetMyResponseIDs(ctx.Request().Context(), sort, userID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to get my responses ID: %+v", err) | ||
return openapi.ResponsesWithQuestionnaireInfo{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire responses: %w", err)) | ||
} | ||
|
||
for _, responseID := range responsesID { | ||
responseDetail, err := r.IRespondent.GetRespondentDetail(ctx.Request().Context(), responseID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to get respondent detail: %+v", err) | ||
return openapi.ResponsesWithQuestionnaireInfo{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get respondent detail: %w", err)) | ||
} | ||
|
||
questionnaire, _, _, _, _, _, err := r.IQuestionnaire.GetQuestionnaireInfo(ctx.Request().Context(), responseDetail.QuestionnaireID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to get questionnaire info: %+v", err) | ||
return openapi.ResponsesWithQuestionnaireInfo{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire info: %w", err)) | ||
} | ||
|
||
isTargetingMe, err := r.ITarget.IsTargetingMe(ctx.Request().Context(), responseDetail.QuestionnaireID, userID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to get target info: %+v", err) | ||
return openapi.ResponsesWithQuestionnaireInfo{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get target info: %w", err)) | ||
} | ||
|
||
questionnaireInfo := struct { | ||
CreatedAt time.Time `json:"created_at"` | ||
IsTargetingMe bool `json:"is_targeting_me"` | ||
ModifiedAt time.Time `json:"modified_at"` | ||
ResponseDueDateTime *time.Time `json:"response_due_date_time,omitempty"` | ||
Title string `json:"title"` | ||
}{ | ||
CreatedAt: questionnaire.CreatedAt, | ||
IsTargetingMe: isTargetingMe, | ||
ModifiedAt: questionnaire.ModifiedAt, | ||
ResponseDueDateTime: &questionnaire.ResTimeLimit.Time, | ||
Title: questionnaire.Title, | ||
} | ||
|
||
response, err := respondentDetail2Response(ctx, responseDetail) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to convert respondent detail into response: %+v", err) | ||
return openapi.ResponsesWithQuestionnaireInfo{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to convert respondent detail into response: %w", err)) | ||
} | ||
|
||
tmp := struct { | ||
Body []openapi.ResponseBody `json:"body"` | ||
IsDraft bool `json:"is_draft"` | ||
ModifiedAt time.Time `json:"modified_at"` | ||
QuestionnaireId int `json:"questionnaire_id"` | ||
QuestionnaireInfo *struct { | ||
CreatedAt time.Time `json:"created_at"` | ||
IsTargetingMe bool `json:"is_targeting_me"` | ||
ModifiedAt time.Time `json:"modified_at"` | ||
ResponseDueDateTime *time.Time `json:"response_due_date_time,omitempty"` | ||
Title string `json:"title"` | ||
} `json:"questionnaire_info,omitempty"` | ||
Respondent openapi.TraqId `json:"respondent"` | ||
ResponseId int `json:"response_id"` | ||
SubmittedAt time.Time `json:"submitted_at"` | ||
}{ | ||
Body: response.Body, | ||
IsDraft: response.IsDraft, | ||
ModifiedAt: response.ModifiedAt, | ||
QuestionnaireId: response.QuestionnaireId, | ||
QuestionnaireInfo: &questionnaireInfo, | ||
Respondent: userID, | ||
ResponseId: response.ResponseId, | ||
SubmittedAt: response.SubmittedAt, | ||
} | ||
res = append(res, tmp) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (r Response) GetResponse(ctx echo.Context, responseID openapi.ResponseIDInPath) (openapi.Response, error) { | ||
responseDetail, err := r.IRespondent.GetRespondentDetail(ctx.Request().Context(), responseID) | ||
if err != nil { | ||
if errors.Is(err, model.ErrRecordNotFound) { | ||
ctx.Logger().Errorf("failed to find response by response ID: %+v", err) | ||
return openapi.Response{}, echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("failed to find response by response ID: %w", err)) | ||
} | ||
ctx.Logger().Errorf("failed to get respondent detail: %+v", err) | ||
return openapi.Response{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get respondent detail: %w", err)) | ||
} | ||
|
||
res, err := respondentDetail2Response(ctx, responseDetail) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to convert respondent detail into response: %+v", err) | ||
return openapi.Response{}, echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to convert respondent detail into response: %w", err)) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (r Response) DeleteResponse(ctx echo.Context, responseID openapi.ResponseIDInPath, userID string) error { | ||
limit, err := r.IQuestionnaire.GetQuestionnaireLimitByResponseID(ctx.Request().Context(), responseID) | ||
if err != nil { | ||
if errors.Is(err, model.ErrRecordNotFound) { | ||
ctx.Logger().Errorf("failed to find response by response ID: %+v", err) | ||
return echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("failed to find response by response ID: %w", err)) | ||
} | ||
ctx.Logger().Errorf("failed to get questionnaire limit by response ID: %+v", err) | ||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaire limit by response ID: %w", err)) | ||
} | ||
if limit.Valid && limit.Time.Before(time.Now()) { | ||
ctx.Logger().Errorf("unable delete the expired response") | ||
return echo.NewHTTPError(http.StatusMethodNotAllowed, fmt.Errorf("unable delete the expired response")) | ||
} | ||
|
||
err = r.IRespondent.DeleteRespondent(ctx.Request().Context(), responseID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to delete respondent: %+v", err) | ||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete respondent: %w", err)) | ||
} | ||
|
||
err = r.IResponse.DeleteResponse(ctx.Request().Context(), responseID) | ||
if err != nil { | ||
ctx.Logger().Errorf("failed to delete response: %+v", err) | ||
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete response: %w", err)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI 現状openapi側の書き方があんまりよくないせいでopenapi由来のtype.go使えてないので後で修正します