Skip to content

Commit

Permalink
Merge pull request #1286 from traPtitech/fix/openapi-insert_questions…
Browse files Browse the repository at this point in the history
…_into_db

fix: add question operations in questionnaire handling
  • Loading branch information
kaitoyama authored Dec 19, 2024
2 parents 55c5ce3 + 9da5cad commit 95ebb76
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions controller/questionnaire.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -176,6 +177,25 @@ func (q Questionnaire) PostQuestionnaire(c echo.Context, userID string, params o
c.Logger().Errorf("failed to insert administrator groups: %+v", err)
return err
}
for questoinNum, question := range params.Questions {
b, err := question.MarshalJSON()
if err != nil {
c.Logger().Errorf("failed to marshal new question: %+v", err)
return err
}
var questionParsed map[string]interface{}
err = json.Unmarshal([]byte(b), &questionParsed)
if err != nil {
c.Logger().Errorf("failed to unmarshal new question: %+v", err)
return err
}
questionType := questionParsed["question_type"].(string)
_, err = q.InsertQuestion(ctx, questionnaireID, 1, questoinNum+1, questionType, question.Body, question.IsRequired)
if err != nil {
c.Logger().Errorf("failed to insert question: %+v", err)
return err
}
}

message := createQuestionnaireMessage(
questionnaireID,
Expand Down Expand Up @@ -394,6 +414,33 @@ func (q Questionnaire) EditQuestionnaire(c echo.Context, questionnaireID int, pa
c.Logger().Errorf("failed to insert administrator groups: %+v", err)
return err
}
for questoinNum, question := range params.Questions {
b, err := question.MarshalJSON()
if err != nil {
c.Logger().Errorf("failed to marshal new question: %+v", err)
return err
}
var questionParsed map[string]interface{}
err = json.Unmarshal([]byte(b), &questionParsed)
if err != nil {
c.Logger().Errorf("failed to unmarshal new question: %+v", err)
return err
}
questionType := questionParsed["question_type"].(string)
if question.QuestionId == nil {
_, err = q.InsertQuestion(ctx, questionnaireID, 1, questoinNum+1, questionType, question.Body, question.IsRequired)
if err != nil {
c.Logger().Errorf("failed to insert question: %+v", err)
return err
}
} else {
err = q.UpdateQuestion(ctx, questionnaireID, 1, questoinNum+1, questionType, question.Body, question.IsRequired, *question.QuestionId)
if err != nil {
c.Logger().Errorf("failed to update question: %+v", err)
return err
}
}
}

err = Jq.DeleteReminder(questionnaireID)
if err != nil {
Expand Down Expand Up @@ -531,6 +578,19 @@ func (q Questionnaire) DeleteQuestionnaire(c echo.Context, questionnaireID int)
return err
}

questions, err := q.GetQuestions(c.Request().Context(), questionnaireID)
if err != nil {
c.Logger().Errorf("failed to get questions: %+v", err)
return err
}
for _, question := range questions {
err = q.DeleteQuestion(c.Request().Context(), question.ID)
if err != nil {
c.Logger().Errorf("failed to delete administrators: %+v", err)
return err
}
}

err = Jq.DeleteReminder(questionnaireID)
if err != nil {
c.Logger().Errorf("failed to delete reminder: %+v", err)
Expand Down

0 comments on commit 95ebb76

Please sign in to comment.