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

Remove relationship create/delete handlers #174

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
91 changes: 0 additions & 91 deletions internal/api/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,6 @@ func (r *Router) buildRelationships(subjResource types.Resource, items []createR
return out, nil
}

func (r *Router) relationshipsCreate(c echo.Context) error {
resourceIDStr := c.Param("id")

ctx, span := tracer.Start(c.Request().Context(), "api.relationshipsCreate", trace.WithAttributes(attribute.String("id", resourceIDStr)))
defer span.End()

resourceID, err := gidx.Parse(resourceIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing resource ID").SetInternal(err)
}

var reqBody createRelationshipsRequest

err = c.Bind(&reqBody)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing request body").SetInternal(err)
}

resource, err := r.engine.NewResourceFromID(resourceID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error creating relationships").SetInternal(err)
}

rels, err := r.buildRelationships(resource, reqBody.Relationships)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error creating relationships").SetInternal(err)
}

_, err = r.engine.CreateRelationships(ctx, rels)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error creating relationships").SetInternal(err)
}

resp := createRelationshipsResponse{
Success: true,
}

return c.JSON(http.StatusCreated, resp)
}

func (r *Router) relationshipListFrom(c echo.Context) error {
resourceIDStr := c.Param("id")

Expand Down Expand Up @@ -159,54 +119,3 @@ func (r *Router) relationshipListTo(c echo.Context) error {

return c.JSON(http.StatusOK, out)
}

func (r *Router) relationshipDelete(c echo.Context) error {
resourceIDStr := c.Param("id")

ctx, span := tracer.Start(c.Request().Context(), "api.relationshipsDelete", trace.WithAttributes(attribute.String("id", resourceIDStr)))
defer span.End()

resourceID, err := gidx.Parse(resourceIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing resource ID").SetInternal(err)
}

var reqBody deleteRelationshipRequest

err = c.Bind(&reqBody)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error parsing request body").SetInternal(err)
}

resource, err := r.engine.NewResourceFromID(resourceID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error deleting relationship").SetInternal(err)
}

relatedResourceID, err := gidx.Parse(reqBody.SubjectID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error deleting relationship").SetInternal(err)
}

relatedResource, err := r.engine.NewResourceFromID(relatedResourceID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error deleting relationship").SetInternal(err)
}

relationship := types.Relationship{
Resource: resource,
Relation: reqBody.Relation,
Subject: relatedResource,
}

_, err = r.engine.DeleteRelationships(ctx, relationship)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error deleting relationship").SetInternal(err)
}

resp := deleteRelationshipsResponse{
Success: true,
}

return c.JSON(http.StatusOK, resp)
}
2 changes: 0 additions & 2 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ func (r *Router) Routes(rg *echo.Group) {

v1.POST("/resources/:id/roles", r.roleCreate)
v1.GET("/resources/:id/roles", r.rolesList)
v1.POST("/resources/:id/relationships", r.relationshipsCreate)
v1.DELETE("/resources/:id/relationships", r.relationshipDelete)
v1.GET("/resources/:id/relationships", r.relationshipListFrom)
v1.GET("/relationships/from/:id", r.relationshipListFrom)
v1.GET("/relationships/to/:id", r.relationshipListTo)
Expand Down
Loading