3
0

Remove obsolete handlers/request for Field

This commit is contained in:
Denis Arh
2019-01-15 21:01:39 +01:00
parent 13482c4de0
commit 2feaf0f487
2 changed files with 0 additions and 180 deletions
-67
View File
@@ -1,67 +0,0 @@
package handlers
/*
Hello! This file is auto-generated from `docs/src/spec.json`.
For development:
In order to update the generated files, edit this file under the location,
add your struct fields, imports, API definitions and whatever you want, and:
1. run [spec](https://github.com/titpetric/spec) in the same folder,
2. run `./_gen.php` in this folder.
You may edit `field.go`, `field.util.go` or `field_test.go` to
implement your API calls, helper functions and tests. The file `field.go`
is only generated the first time, and will not be overwritten if it exists.
*/
import (
"context"
"github.com/go-chi/chi"
"net/http"
"github.com/titpetric/factory/resputil"
"github.com/crusttech/crust/crm/rest/request"
)
// Internal API interface
type FieldAPI interface {
List(context.Context, *request.FieldList) (interface{}, error)
Type(context.Context, *request.FieldType) (interface{}, error)
}
// HTTP API interface
type Field struct {
List func(http.ResponseWriter, *http.Request)
Type func(http.ResponseWriter, *http.Request)
}
func NewField(fh FieldAPI) *Field {
return &Field{
List: func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
params := request.NewFieldList()
resputil.JSON(w, params.Fill(r), func() (interface{}, error) {
return fh.List(r.Context(), params)
})
},
Type: func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
params := request.NewFieldType()
resputil.JSON(w, params.Fill(r), func() (interface{}, error) {
return fh.Type(r.Context(), params)
})
},
}
}
func (fh *Field) MountRoutes(r chi.Router, middlewares ...func(http.Handler) http.Handler) {
r.Group(func(r chi.Router) {
r.Use(middlewares...)
r.Route("/field", func(r chi.Router) {
r.Get("/", fh.List)
r.Get("/{typeID}", fh.Type)
})
})
}
-113
View File
@@ -1,113 +0,0 @@
package request
/*
Hello! This file is auto-generated from `docs/src/spec.json`.
For development:
In order to update the generated files, edit this file under the location,
add your struct fields, imports, API definitions and whatever you want, and:
1. run [spec](https://github.com/titpetric/spec) in the same folder,
2. run `./_gen.php` in this folder.
You may edit `field.go`, `field.util.go` or `field_test.go` to
implement your API calls, helper functions and tests. The file `field.go`
is only generated the first time, and will not be overwritten if it exists.
*/
import (
"encoding/json"
"io"
"mime/multipart"
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/pkg/errors"
)
var _ = chi.URLParam
var _ = multipart.FileHeader{}
// Field list request parameters
type FieldList struct {
}
func NewFieldList() *FieldList {
return &FieldList{}
}
func (f *FieldList) Fill(r *http.Request) (err error) {
if strings.ToLower(r.Header.Get("content-type")) == "application/json" {
err = json.NewDecoder(r.Body).Decode(f)
switch {
case err == io.EOF:
err = nil
case err != nil:
return errors.Wrap(err, "error parsing http request body")
}
}
if err = r.ParseForm(); err != nil {
return err
}
get := map[string]string{}
post := map[string]string{}
urlQuery := r.URL.Query()
for name, param := range urlQuery {
get[name] = string(param[0])
}
postVars := r.Form
for name, param := range postVars {
post[name] = string(param[0])
}
return err
}
var _ RequestFiller = NewFieldList()
// Field type request parameters
type FieldType struct {
TypeID string
}
func NewFieldType() *FieldType {
return &FieldType{}
}
func (f *FieldType) Fill(r *http.Request) (err error) {
if strings.ToLower(r.Header.Get("content-type")) == "application/json" {
err = json.NewDecoder(r.Body).Decode(f)
switch {
case err == io.EOF:
err = nil
case err != nil:
return errors.Wrap(err, "error parsing http request body")
}
}
if err = r.ParseForm(); err != nil {
return err
}
get := map[string]string{}
post := map[string]string{}
urlQuery := r.URL.Query()
for name, param := range urlQuery {
get[name] = string(param[0])
}
postVars := r.Form
for name, param := range postVars {
post[name] = string(param[0])
}
f.TypeID = chi.URLParam(r, "typeID")
return err
}
var _ RequestFiller = NewFieldType()