diff --git a/controller/questionnaire.go b/controller/questionnaire.go index 84c216c0..99e0ccaf 100644 --- a/controller/questionnaire.go +++ b/controller/questionnaire.go @@ -2,6 +2,7 @@ package controller import ( "context" + "encoding/json" "errors" "fmt" "net/http" @@ -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, @@ -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 { @@ -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)