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

[OSE-301] Implement Create, Get, List, Delete endpoints for issuance templates #231

Merged
merged 8 commits into from
Jan 3, 2023
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
73 changes: 62 additions & 11 deletions pkg/server/router/issuance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"net/http"

"github.com/pkg/errors"
"github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/internal/util"
"github.com/tbd54566975/ssi-service/pkg/server/framework"
svcframework "github.com/tbd54566975/ssi-service/pkg/service/framework"
"github.com/tbd54566975/ssi-service/pkg/service/issuing"
)

type IssuanceRouter struct {
service issuing.Service
}

func NewIssuanceRouter(svc framework.Service) (*IssuanceRouter, error) {
func NewIssuanceRouter(svc svcframework.Service) (*IssuanceRouter, error) {
service, ok := svc.(*issuing.Service)
if !ok {
return nil, errors.New("could not cast to issuing service type")
Expand All @@ -28,17 +30,34 @@ func NewIssuanceRouter(svc framework.Service) (*IssuanceRouter, error) {
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} issuing.GetIssuanceTemplateResponse
// @Success 200 {object} issuing.IssuanceTemplate
// @Failure 400 {string} string "Bad request"
// @Router /v1/issuancetemplates/{id} [get]
func (ir IssuanceRouter) GetIssuanceTemplate(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
return nil
func (ir IssuanceRouter) GetIssuanceTemplate(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
id := framework.GetParam(ctx, IDParam)
if id == nil {
return framework.NewRequestError(
util.LoggingNewError("cannot get issuance template without an ID"), http.StatusBadRequest)
}

issuanceTemplate, err := ir.service.GetIssuanceTemplate(&issuing.GetIssuanceTemplateRequest{ID: *id})
if err != nil {
return framework.NewRequestError(
util.LoggingErrorMsg(err, "getting issuance template"), http.StatusInternalServerError)
}
return framework.Respond(ctx, w, issuanceTemplate.IssuanceTemplate, http.StatusOK)
}

type CreateIssuanceTemplateRequest struct {
issuing.IssuanceTemplate
}

func (r CreateIssuanceTemplateRequest) ToServiceRequest() *issuing.CreateIssuanceTemplateRequest {
return &issuing.CreateIssuanceTemplateRequest{
IssuanceTemplate: r.IssuanceTemplate,
}
}

// CreateIssuanceTemplate godoc
// @Summary Create issuance template
// @Description Create issuance template
Expand All @@ -51,7 +70,20 @@ type CreateIssuanceTemplateRequest struct {
// @Failure 500 {string} string "Internal server error"
// @Router /v1/issuancetemplates [put]
func (ir IssuanceRouter) CreateIssuanceTemplate(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
var request CreateIssuanceTemplateRequest
errMsg := "Invalid Issuance Template Request"
if err := framework.Decode(r, &request); err != nil {
return framework.NewRequestError(
util.LoggingErrorMsg(err, errMsg), http.StatusBadRequest)
}

template, err := ir.service.CreateIssuanceTemplate(request.ToServiceRequest())
if err != nil {
return framework.NewRequestError(
util.LoggingErrorMsg(err, "creating issuance template"), http.StatusInternalServerError)
}

return framework.Respond(ctx, w, template, http.StatusCreated)
}

// DeleteIssuanceTemplate godoc
Expand All @@ -65,8 +97,19 @@ func (ir IssuanceRouter) CreateIssuanceTemplate(ctx context.Context, w http.Resp
// @Failure 400 {string} string "Bad request"
// @Failure 500 {string} string "Internal server error"
// @Router /v1/issuancetemplates/{id} [delete]
func (ir IssuanceRouter) DeleteIssuanceTemplate(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
func (ir IssuanceRouter) DeleteIssuanceTemplate(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
id := framework.GetParam(ctx, IDParam)
if id == nil {
return framework.NewRequestError(
util.LoggingNewError("cannot delete an issuance template without an ID parameter"), http.StatusBadRequest)
}

if err := ir.service.DeleteIssuanceTemplate(&issuing.DeleteIssuanceTemplateRequest{ID: *id}); err != nil {
return framework.NewRequestError(
util.LoggingErrorMsgf(err, "could not delete issuance template with id: %s", *id), http.StatusInternalServerError)
}

return framework.Respond(ctx, w, nil, http.StatusOK)
}

type ListIssuanceTemplatesResponse struct {
Expand All @@ -75,14 +118,22 @@ type ListIssuanceTemplatesResponse struct {

// ListIssuanceTemplates godoc
// @Summary Lists issuance templates
// @Description Lists all issuangce templates stored in this service.
// @Description Lists all issuance templates stored in this service.
// @Tags IssuingAPI
// @Accept json
// @Produce json
// @Success 200 {object} ListIssuanceTemplatesResponse
// @Failure 400 {string} string "Bad request"
// @Failure 500 {string} string "Internal server error"
// @Router /v1/manifests [get]
func (ir IssuanceRouter) ListIssuanceTemplates(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return nil
func (ir IssuanceRouter) ListIssuanceTemplates(ctx context.Context, w http.ResponseWriter, _ *http.Request) error {
gotManifests, err := ir.service.ListIssuanceTemplates(&issuing.ListIssuanceTemplatesRequest{})

if err != nil {
return framework.NewRequestError(
util.LoggingErrorMsg(err, "could not get templates"), http.StatusBadRequest)
}

resp := ListIssuanceTemplatesResponse{IssuanceTemplates: gotManifests.IssuanceTemplates}
return framework.Respond(ctx, w, resp, http.StatusOK)
}
Loading