From 47fe43d5acee57be0009dad1985acb2bfd156ff4 Mon Sep 17 00:00:00 2001 From: Eraxyso <130852025+Eraxyso@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:05:38 +0000 Subject: [PATCH] fix: add question operations in questionnaire handling --- controller/questionnaire.go | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/controller/questionnaire.go b/controller/questionnaire.go index 2341e42b..89b09db7 100644 --- a/controller/questionnaire.go +++ b/controller/questionnaire.go @@ -2,6 +2,7 @@ package controller import ( "context" + "encoding/json" "errors" "fmt" "net/http" @@ -156,6 +157,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, @@ -357,6 +377,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 + } + } + } return nil }) @@ -483,6 +530,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 + } + } + return nil }) if err != nil {