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

Add crud variants #12

Merged
merged 1 commit into from
Oct 6, 2017
Merged
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
36 changes: 35 additions & 1 deletion pkg/handler/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/checkr/flagr/swagger_gen/restapi/operations/distribution"
"github.com/checkr/flagr/swagger_gen/restapi/operations/flag"
"github.com/checkr/flagr/swagger_gen/restapi/operations/segment"
"github.com/checkr/flagr/swagger_gen/restapi/operations/variant"
"github.com/go-openapi/runtime/middleware"
)

Expand All @@ -33,6 +34,10 @@ type CRUD interface {
// Distributions
FindDistributions(distribution.FindDistributionsParams) middleware.Responder
PutDistributions(distribution.PutDistributionsParams) middleware.Responder

// Variants
CreateVariant(variant.CreateVariantParams) middleware.Responder
FindVariants(variant.FindVariantsParams) middleware.Responder
}

// NewCRUD creates a new CRUD instance
Expand Down Expand Up @@ -121,7 +126,7 @@ func (c *crud) CreateSegment(params segment.CreateSegmentParams) middleware.Resp

err := s.Create(repo.GetDB())
if err != nil {
return segment.NewFindSegmentsDefault(500)
return segment.NewCreateSegmentDefault(500)
}

resp := segment.NewCreateSegmentOK()
Expand Down Expand Up @@ -218,3 +223,32 @@ func (c *crud) FindDistributions(params distribution.FindDistributionsParams) mi
resp.SetPayload(e2r.MapDistributions(ds))
return resp
}

func (c *crud) CreateVariant(params variant.CreateVariantParams) middleware.Responder {
v := &entity.Variant{}
v.FlagID = uint(params.FlagID)
v.Key = util.SafeString(params.Body.Key)

err := v.Create(repo.GetDB())
if err != nil {
return variant.NewCreateVariantDefault(500)
}

resp := variant.NewCreateVariantOK()
resp.SetPayload(e2r.MapVariant(v))
return resp
}

func (c *crud) FindVariants(params variant.FindVariantsParams) middleware.Responder {
vs := []entity.Variant{}

q := entity.NewVariantQuerySet(repo.GetDB())
err := q.FlagIDEq(uint(params.FlagID)).OrderAscByID().All(&vs)
if err != nil {
return variant.NewFindVariantsDefault(500)
}

resp := variant.NewFindVariantsOK()
resp.SetPayload(e2r.MapVariants(vs))
return resp
}
5 changes: 5 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/checkr/flagr/swagger_gen/restapi/operations/evaluation"
"github.com/checkr/flagr/swagger_gen/restapi/operations/flag"
"github.com/checkr/flagr/swagger_gen/restapi/operations/segment"
"github.com/checkr/flagr/swagger_gen/restapi/operations/variant"
)

// Setup initialize all the hanlder functions
Expand All @@ -31,6 +32,10 @@ func Setup(api *operations.FlagrAPI) {
api.DistributionFindDistributionsHandler = distribution.FindDistributionsHandlerFunc(c.FindDistributions)
api.DistributionPutDistributionsHandler = distribution.PutDistributionsHandlerFunc(c.PutDistributions)

// variants
api.VariantCreateVariantHandler = variant.CreateVariantHandlerFunc(c.CreateVariant)
api.VariantFindVariantsHandler = variant.FindVariantsHandlerFunc(c.FindVariants)

// evaluation
e := NewEval()
api.EvaluationPostEvaluationHandler = evaluation.PostEvaluationHandlerFunc(e.PostEvaluation)
Expand Down
18 changes: 18 additions & 0 deletions pkg/mapper/entity_restapi/e2r/e2r.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,21 @@ func MapDistributions(e []entity.Distribution) []*models.Distribution {
}
return ret
}

// MapVariant maps variant
func MapVariant(e *entity.Variant) *models.Variant {
r := &models.Variant{
ID: int64(e.ID),
Key: util.StringPtr(e.Key),
}
return r
}

// MapVariants maps variant
func MapVariants(e []entity.Variant) []*models.Variant {
ret := make([]*models.Variant, len(e), len(e))
for i, v := range e {
ret[i] = MapVariant(&v)
}
return ret
}
35 changes: 35 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ paths:
description: generic error response
schema:
$ref: "#/definitions/error"
post:
tags:
- variant
operationId: createVariant
parameters:
- in: path
name: flagID
description: numeric ID of the flag
required: true
type: integer
format: int32
minimum: 1
- in: body
name: body
description: create a variant
required: true
schema:
$ref: "#/definitions/createVariantRequest"
responses:
200:
description: variant just created
schema:
$ref: "#/definitions/variant"
default:
description: generic error response
schema:
$ref: "#/definitions/error"
/flags/{flagID}/segments:
get:
tags:
Expand Down Expand Up @@ -376,6 +403,14 @@ definitions:
format: int32
minimum: 0
maximum: 100
createVariantRequest:
type: object
required:
- key
properties:
key:
type: string
minLength: 1
createConstraintRequest:
type: object
required:
Expand Down
73 changes: 73 additions & 0 deletions swagger_gen/models/create_variant_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions swagger_gen/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions swagger_gen/restapi/operations/flagr_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions swagger_gen/restapi/operations/variant/create_variant.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading