Skip to content

Commit

Permalink
feat: allow user to choose stylesheet and starting coordinates (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbaraffe authored Dec 23, 2020
1 parent c3f6d94 commit 0776850
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 56 deletions.
34 changes: 28 additions & 6 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ var doc = `{
}
},
{
"description": "Sheets to extract",
"name": "sheets",
"description": "Reader optional options",
"name": "options",
"in": "body",
"schema": {
"type": "string"
"$ref": "#/definitions/models.ReaderOption"
}
}
],
Expand Down Expand Up @@ -100,11 +100,11 @@ var doc = `{
}
},
{
"description": "Sheets to extract",
"name": "sheets",
"description": "Reader optional options",
"name": "options",
"in": "body",
"schema": {
"type": "string"
"$ref": "#/definitions/models.ReaderOption"
}
}
],
Expand Down Expand Up @@ -383,6 +383,17 @@ var doc = `{
}
}
},
"models.Option": {
"type": "object",
"properties": {
"coordinates": {
"type": "string"
},
"sheetname": {
"type": "string"
}
}
},
"models.PlotArea": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -417,6 +428,17 @@ var doc = `{
}
}
},
"models.ReaderOption": {
"type": "object",
"properties": {
"options": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Option"
}
}
}
},
"models.Series": {
"type": "object",
"properties": {
Expand Down
34 changes: 28 additions & 6 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
}
},
{
"description": "Sheets to extract",
"name": "sheets",
"description": "Reader optional options",
"name": "options",
"in": "body",
"schema": {
"type": "string"
"$ref": "#/definitions/models.ReaderOption"
}
}
],
Expand Down Expand Up @@ -83,11 +83,11 @@
}
},
{
"description": "Sheets to extract",
"name": "sheets",
"description": "Reader optional options",
"name": "options",
"in": "body",
"schema": {
"type": "string"
"$ref": "#/definitions/models.ReaderOption"
}
}
],
Expand Down Expand Up @@ -366,6 +366,17 @@
}
}
},
"models.Option": {
"type": "object",
"properties": {
"coordinates": {
"type": "string"
},
"sheetname": {
"type": "string"
}
}
},
"models.PlotArea": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -400,6 +411,17 @@
}
}
},
"models.ReaderOption": {
"type": "object",
"properties": {
"options": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Option"
}
}
}
},
"models.Series": {
"type": "object",
"properties": {
Expand Down
26 changes: 20 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ definitions:
show_legend_key:
type: boolean
type: object
models.Option:
properties:
coordinates:
type: string
sheetname:
type: string
type: object
models.PlotArea:
properties:
show_bubble_size:
Expand All @@ -165,6 +172,13 @@ definitions:
locked:
type: boolean
type: object
models.ReaderOption:
properties:
options:
items:
$ref: '#/definitions/models.Option'
type: array
type: object
models.Series:
properties:
categories:
Expand Down Expand Up @@ -235,11 +249,11 @@ paths:
required: true
schema:
type: string
- description: Sheets to extract
- description: Reader optional options
in: body
name: sheets
name: options
schema:
type: string
$ref: '#/definitions/models.ReaderOption'
produces:
- application/json
responses:
Expand All @@ -266,11 +280,11 @@ paths:
required: true
schema:
type: string
- description: Sheets to extract
- description: Reader optional options
in: body
name: sheets
name: options
schema:
type: string
$ref: '#/definitions/models.ReaderOption'
produces:
- application/json
responses:
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
30 changes: 13 additions & 17 deletions handlers/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package handlers
import (
"encoding/json"
"net/http"
"strings"

"github.com/Los-Crackitos/Excelante/models"
"github.com/Los-Crackitos/Excelante/services"
)

Expand All @@ -15,29 +15,27 @@ import (
// @Accept mpfd
// @Produce json
// @Param file body string true "The Excel file to convert"
// @Param sheets body string false "Sheets to extract"
// @Param options body models.ReaderOption false "Reader optional options"
// @Success 200 {object} services.Output
// @Failure 400 {string} string
// @Router /read/lines [post]
func ReadExcelFileByLine(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
file, _, _ := r.FormFile("file")
r.ParseForm()
sheets := r.Form.Get("sheets")
options := r.Form.Get("options")

var sheetsToExtract []string
if sheets != "" {
sheetsToExtract = strings.Split(sheets, ",")
}
readerOptions := models.ReaderOption{}
json.Unmarshal([]byte(options), &readerOptions)

output, err := services.ReadLines(file, sheetsToExtract)
output, err := services.ReadLines(file, readerOptions)

if err != nil {
http.Error(w, "An error occurred during file reading", http.StatusBadRequest)

return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(output)
}
Expand All @@ -49,29 +47,27 @@ func ReadExcelFileByLine(w http.ResponseWriter, r *http.Request) {
// @Accept mpfd
// @Produce json
// @Param file body string true "The Excel file to convert"
// @Param sheets body string false "Sheets to extract"
// @Param options body models.ReaderOption false "Reader optional options"
// @Success 200 {object} services.Output
// @Failure 400 {string} string
// @Router /read/columns [post]
func ReadExcelFileByColumn(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
file, _, _ := r.FormFile("file")
r.ParseForm()
sheets := r.Form.Get("sheets")
options := r.Form.Get("options")

var sheetsToExtract []string
if sheets != "" {
sheetsToExtract = strings.Split(sheets, ",")
}
readerOptions := models.ReaderOption{}
json.Unmarshal([]byte(options), &readerOptions)

output, err := services.ReadColumns(file, sheetsToExtract)
output, err := services.ReadColumns(file, readerOptions)

if err != nil {
http.Error(w, "An error occurred during file reading", http.StatusBadRequest)

return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(output)
}
12 changes: 12 additions & 0 deletions models/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

// ReaderOption contains reader optional options
type ReaderOption struct {
Options []Option `json:"options"`
}

// Option represent a reader option object
type Option struct {
SheetName string `json:"sheet_name"`
StartingCoordinates string `json:"starting_coordinates,omitempty"`
}
Loading

0 comments on commit 0776850

Please sign in to comment.