Skip to content

feat: export to csv #8

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/devmegablaster/bashform/internal/styles"
"github.com/spf13/cobra"
)

func (c *CLI) exportResponses() *cobra.Command {
formCmd := &cobra.Command{
Use: "export [code]",
Short: "Exports responses for a form using form code to CSV",
Args: cobra.ExactArgs(1),
Aliases: []string{"e"},
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
formCSV, err := c.formSvc.GetResponsesCSV(args[0], c.user)
if err != nil {
c.logger.Error("Error getting form", "error", err)
cmd.Println(styles.Error.Render(err.Error()))
}

c.logger.Info("Form exported to CSV")

cmd.Println(formCSV)
return nil
},
}

return formCmd
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (c *CLI) Init() {
c.AddCommand(c.fillForm())
c.AddCommand(c.createForm())
c.AddCommand(c.getForms())
c.AddCommand(c.exportResponses())
}

func (c *CLI) Run() error {
Expand Down
8 changes: 8 additions & 0 deletions internal/models/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ func (r *ResponseRequest) ToResponse(formID uuid.UUID, userID uuid.UUID) Respons
Answers: r.Answers,
}
}

func (r *Response) ToCSV() []string {
csv := []string{}
for _, answer := range r.Answers {
csv = append(csv, answer.Value)
}
return csv
}
10 changes: 10 additions & 0 deletions internal/repository/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ func (r *FormRepository) GetWithResponses(userID, formID string) (*models.Form,

return &form, nil
}

func (r *FormRepository) GetWithResponsesUsingCode(userID, code string) (*models.Form, error) {
var form models.Form

if err := r.db.DB.Preload("Responses.Answers").Preload("Questions").First(&form, "code = ? AND user_id = ?", code, userID).Error; err != nil {
return nil, err
}

return &form, nil
}
22 changes: 22 additions & 0 deletions internal/services/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log/slog"
"strings"

"github.com/devmegablaster/bashform/internal/config"
"github.com/devmegablaster/bashform/internal/database"
Expand Down Expand Up @@ -114,3 +115,24 @@ func (f *FormService) GetWithResponses(formID string, user *models.User) (*model

return formWithResponses, nil
}

func (f *FormService) GetResponsesCSV(formCode string, user *models.User) (string, error) {
form, err := f.fr.GetWithResponsesUsingCode(user.ID.String(), formCode)
if err != nil {
return "", err
}

rows := []string{}
questions := []string{}
for _, question := range form.Questions {
questions = append(questions, question.Text)
}

rows = append(rows, strings.Join(questions, ","))

for _, response := range form.Responses {
rows = append(rows, strings.Join(response.ToCSV(), ","))
}

return strings.Join(rows, "\n"), nil
}