Skip to content

Commit

Permalink
fix for wire
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoyama committed Dec 17, 2024
1 parent 76c285f commit f16d7a3
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 53 deletions.
24 changes: 22 additions & 2 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,28 @@ type Questionnaire struct {
Response
}

func NewQuestionnaire() *Questionnaire {
return &Questionnaire{}
func NewQuestionnaire(
questionnaire model.IQuestionnaire,
target model.ITarget,
administrator model.IAdministrator,
question model.IQuestion,
option model.IOption,
scaleLabel model.IScaleLabel,
validation model.IValidation,
transaction model.ITransaction,
webhook traq.IWebhook,
) *Questionnaire {
return &Questionnaire{
IQuestionnaire: questionnaire,
ITarget: target,
IAdministrator: administrator,
IQuestion: question,
IOption: option,
IScaleLabel: scaleLabel,
IValidation: validation,
ITransaction: transaction,
IWebhook: webhook,
}
}

const MaxTitleLength = 50
Expand Down
20 changes: 18 additions & 2 deletions controller/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ type Response struct {
model.IScaleLabel
}

func NewResponse() *Response {
return &Response{}
func NewResponse(
questionnaire model.IQuestionnaire,
respondent model.IRespondent,
response model.IResponse,
target model.ITarget,
question model.IQuestion,
validation model.IValidation,
scaleLabel model.IScaleLabel,
) *Response {
return &Response{
IQuestionnaire: questionnaire,
IRespondent: respondent,
IResponse: response,
ITarget: target,
IQuestion: question,
IValidation: validation,
IScaleLabel: scaleLabel,
}
}

func (r Response) GetMyResponses(ctx echo.Context, params openapi.GetMyResponsesParams, userID string) (openapi.ResponsesWithQuestionnaireInfo, error) {
Expand Down
16 changes: 15 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package handler

type Handler struct{}
import "github.com/traPtitech/anke-to/controller"

type Handler struct {
Questionnaire *controller.Questionnaire
Response *controller.Response
}

func NewHandler(questionnaire *controller.Questionnaire,
response *controller.Response,
) *Handler {
return &Handler{
Questionnaire: questionnaire,
Response: response,
}
}
28 changes: 9 additions & 19 deletions handler/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/anke-to/controller"
"github.com/traPtitech/anke-to/model"
"github.com/traPtitech/anke-to/openapi"
)

// (GET /questionnaires)
func (h Handler) GetQuestionnaires(ctx echo.Context, params openapi.GetQuestionnairesParams) error {
res := openapi.QuestionnaireList{}
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.GetQuestionnaires(ctx, userID, params)
res, err = h.Questionnaire.GetQuestionnaires(ctx, userID, params)
if err != nil {
ctx.Logger().Errorf("failed to get questionnaires: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get questionnaires: %w", err))
Expand Down Expand Up @@ -50,14 +48,13 @@ 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)
res, err = h.Questionnaire.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))
Expand All @@ -69,8 +66,7 @@ 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)
res, err := h.Questionnaire.GetQuestionnaire(ctx, questionnaireID)
if err != nil {
if errors.Is(err, model.ErrRecordNotFound) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("questionnaire not found: %w", err))
Expand All @@ -89,8 +85,7 @@ func (h Handler) EditQuestionnaire(ctx echo.Context, questionnaireID openapi.Que
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err))
}

q := controller.NewQuestionnaire()
err := q.EditQuestionnaire(ctx, questionnaireID, params)
err := h.Questionnaire.EditQuestionnaire(ctx, questionnaireID, params)
if err != nil {
ctx.Logger().Errorf("failed to edit questionnaire: %+v", err)
return err
Expand All @@ -101,8 +96,7 @@ func (h Handler) EditQuestionnaire(ctx echo.Context, questionnaireID openapi.Que

// (DELETE /questionnaires/{questionnaireID})
func (h Handler) DeleteQuestionnaire(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error {
q := controller.NewQuestionnaire()
err := q.DeleteQuestionnaire(ctx, questionnaireID)
err := h.Questionnaire.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))
Expand All @@ -114,8 +108,7 @@ func (h Handler) DeleteQuestionnaire(ctx echo.Context, questionnaireID openapi.Q
// (GET /questionnaires/{questionnaireID}/myRemindStatus)
func (h Handler) GetQuestionnaireMyRemindStatus(ctx echo.Context, questionnaireID openapi.QuestionnaireIDInPath) error {
res := openapi.QuestionnaireIsRemindEnabled{}
q := controller.NewQuestionnaire()
status, err := q.GetQuestionnaireMyRemindStatus(ctx, questionnaireID)
status, err := h.Questionnaire.GetQuestionnaireMyRemindStatus(ctx, questionnaireID)
if err != nil {
ctx.Logger().Errorf("failed to get questionnaire my remind status: %+v", err)
return err
Expand All @@ -133,8 +126,7 @@ func (h Handler) EditQuestionnaireMyRemindStatus(ctx echo.Context, questionnaire
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to bind request body: %w", err))
}

q := controller.NewQuestionnaire()
err := q.EditQuestionnaireMyRemindStatus(ctx, questionnaireID, params.IsRemindEnabled)
err := h.Questionnaire.EditQuestionnaireMyRemindStatus(ctx, questionnaireID, params.IsRemindEnabled)
if err != nil {
ctx.Logger().Errorf("failed to edit questionnaire my remind status: %+v", err)
return err
Expand All @@ -149,8 +141,7 @@ func (h Handler) GetQuestionnaireResponses(ctx echo.Context, questionnaireID ope
ctx.Logger().Errorf("failed to get userID: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get userID: %w", err))
}
q := controller.NewQuestionnaire()
res, err := q.GetQuestionnaireResponses(ctx, questionnaireID, params, userID)
res, err := h.Questionnaire.GetQuestionnaireResponses(ctx, questionnaireID, params, userID)
if err != nil {
ctx.Logger().Errorf("failed to get questionnaire responses: %+v", err)
return err
Expand Down Expand Up @@ -185,8 +176,7 @@ func (h Handler) PostQuestionnaireResponse(ctx echo.Context, questionnaireID ope
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to validate request body: %w", err))
}

q := controller.NewQuestionnaire()
res, err = q.PostQuestionnaireResponse(ctx, questionnaireID, params, userID)
res, err = h.Questionnaire.PostQuestionnaireResponse(ctx, questionnaireID, params, userID)
if err != nil {
ctx.Logger().Errorf("failed to post questionnaire response: %+v", err)
return err
Expand Down
15 changes: 5 additions & 10 deletions handler/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/anke-to/controller"
"github.com/traPtitech/anke-to/openapi"
)

Expand All @@ -18,8 +17,7 @@ func (h Handler) GetMyResponses(ctx echo.Context, params openapi.GetMyResponsesP
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get userID: %w", err))
}

r := controller.NewResponse()
res, err = r.GetMyResponses(ctx, params, userID)
res, err = h.Response.GetMyResponses(ctx, params, userID)
if err != nil {
ctx.Logger().Errorf("failed to get my responses: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get my responses: %w", err))
Expand All @@ -35,8 +33,7 @@ func (h Handler) DeleteResponse(ctx echo.Context, responseID openapi.ResponseIDI
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to get userID: %w", err))
}

r := controller.NewResponse()
err = r.DeleteResponse(ctx, responseID, userID)
err = h.Response.DeleteResponse(ctx, responseID, userID)
if err != nil {
ctx.Logger().Errorf("failed to delete response: %+v", err)
return err
Expand All @@ -49,8 +46,7 @@ func (h Handler) DeleteResponse(ctx echo.Context, responseID openapi.ResponseIDI
func (h Handler) GetResponse(ctx echo.Context, responseID openapi.ResponseIDInPath) error {
res := openapi.Response{}

r := controller.NewResponse()
res, err := r.GetResponse(ctx, responseID)
res, err := h.Response.GetResponse(ctx, responseID)
if err != nil {
ctx.Logger().Errorf("failed to get response: %+v", err)
return err
Expand Down Expand Up @@ -78,11 +74,10 @@ func (h Handler) EditResponse(ctx echo.Context, responseID openapi.ResponseIDInP
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("failed to validate request body: %w", err))
}

r := controller.NewResponse()
err = r.EditResponse(ctx, responseID, req)
err = h.Response.EditResponse(ctx, responseID, req)
if err != nil {
ctx.Logger().Errorf("failed to edit response: %+v", err)
return err
return err
}

return ctx.NoContent(200)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/labstack/echo/v4/middleware"
oapiMiddleware "github.com/oapi-codegen/echo-middleware"
"github.com/traPtitech/anke-to/controller"
"github.com/traPtitech/anke-to/handler"
"github.com/traPtitech/anke-to/model"
"github.com/traPtitech/anke-to/openapi"

Expand Down Expand Up @@ -83,7 +82,8 @@ func main() {
mws.AddRouteConfig("/responses/:responseID", http.MethodPatch, api.RespondentAuthenticate)
mws.AddRouteConfig("/responses/:responseID", http.MethodDelete, api.RespondentAuthenticate)

openapi.RegisterHandlers(e, handler.Handler{})
handlerApi := InjectHandler()
openapi.RegisterHandlers(e, handlerApi)

e.Use(mws.ApplyMiddlewares)
e.Logger.Fatal(e.Start(port))
Expand Down
2 changes: 1 addition & 1 deletion model/administratorGroups_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type AdministratorGroup struct{}

// NewAdministratorGroup AdministratorGroupRepositoryのコンストラクタ
func NewAdministratorGroup() *AdministratorGroup {
return &AdministratorGroup{}
return new(AdministratorGroup)
}

type AdministratorGroups struct {
Expand Down
37 changes: 35 additions & 2 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var (
webhookBind = wire.Bind(new(traq.IWebhook), new(*traq.Webhook))
)

func InjectAPIServer() *handler.Middleware {
func InjectHandler() *handler.Handler {
wire.Build(
handler.NewMiddleware,
handler.NewHandler,
controller.NewResponse,
controller.NewQuestionnaire,
model.NewAdministrator,
Expand Down Expand Up @@ -57,3 +57,36 @@ func InjectAPIServer() *handler.Middleware {

return nil
}

func InjectAPIServer() *handler.Middleware {
wire.Build(
// handler.NewHandler,
handler.NewMiddleware,
// controller.NewResponse,
// controller.NewQuestionnaire,
// model.NewAdministrator,
// model.NewOption,
// model.NewQuestionnaire,
// model.NewQuestion,
// model.NewRespondent,
// model.NewResponse,
// model.NewScaleLabel,
// model.NewTarget,
// model.NewValidation,
// model.NewTransaction,
// traq.NewWebhook,
// administratorBind,
// optionBind,
// questionnaireBind,
// questionBind,
// respondentBind,
// responseBind,
// scaleLabelBind,
// targetBind,
// validationBind,
// transactionBind,
// webhookBind,
)

return nil
}
30 changes: 16 additions & 14 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f16d7a3

Please sign in to comment.