Skip to content
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

fix: add question operations in questionnaire handling #1286

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading