Skip to content

Commit

Permalink
enable nodes to be added to collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Jul 3, 2024
1 parent 8482672 commit 0ca2414
Show file tree
Hide file tree
Showing 13 changed files with 1,635 additions and 242 deletions.
65 changes: 64 additions & 1 deletion api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,17 @@ paths:
"404": { $ref: "#/components/responses/NotFound" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { $ref: "#/components/responses/CollectionUpdateOK" }
delete:
operationId: CollectionDelete
description: Delete a collection owned by the authenticated account.
tags: [collections]
parameters: [$ref: "#/components/parameters/CollectionIDParam"]
responses:
default: { $ref: "#/components/responses/InternalServerError" }
"404": { $ref: "#/components/responses/NotFound" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { description: OK }

/v1/collections/{collection_id}/posts/{post_id}:
put:
operationId: CollectionAddPost
Expand Down Expand Up @@ -876,6 +887,36 @@ paths:
"404": { $ref: "#/components/responses/NotFound" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { $ref: "#/components/responses/CollectionRemovePostOK" }
/v1/collections/{collection_id}/nodes/{node_id}:
put:
operationId: CollectionAddNode
description: |
Add a node to a collection. The collection must be owned by the account
making the request. The node can be any published node or any node
not published but owned by the collection owner.
tags: [collections]
parameters:
- $ref: "#/components/parameters/CollectionIDParam"
- $ref: "#/components/parameters/NodeIDParam"
responses:
default: { $ref: "#/components/responses/InternalServerError" }
"404": { $ref: "#/components/responses/NotFound" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { $ref: "#/components/responses/CollectionAddNodeOK" }
delete:
operationId: CollectionRemoveNode
description: |
Remove a node from a collection. The collection must be owned by the
account making the request.
tags: [collections]
parameters:
- $ref: "#/components/parameters/CollectionIDParam"
- $ref: "#/components/parameters/NodeIDParam"
responses:
default: { $ref: "#/components/responses/InternalServerError" }
"404": { $ref: "#/components/responses/NotFound" }
"401": { $ref: "#/components/responses/Unauthorised" }
"200": { $ref: "#/components/responses/CollectionRemoveNodeOK" }

#
# 888
Expand Down Expand Up @@ -1178,6 +1219,14 @@ components:
schema:
$ref: "#/components/schemas/Identifier"

NodeIDParam:
description: Unique node ID.
name: node_id
in: path
required: true
schema:
$ref: "#/components/schemas/Identifier"

OAuthProvider:
description: The identifier for an OAuth2 provider such as "twitter".
name: oauth_provider
Expand Down Expand Up @@ -1708,6 +1757,20 @@ components:
schema:
$ref: "#/components/schemas/Collection"

CollectionAddNodeOK:
description: Collection content added.
content:
application/json:
schema:
$ref: "#/components/schemas/Collection"

CollectionRemoveNodeOK:
description: Collection content added.
content:
application/json:
schema:
$ref: "#/components/schemas/Collection"

NodeCreateOK:
description: Node created.
content:
Expand Down Expand Up @@ -3165,7 +3228,7 @@ components:

DatagraphNodeKind:
type: string
enum: [thread, reply, node, link]
enum: [post, node, profile]

DatagraphRecommendations:
required: [recomentations]
Expand Down
13 changes: 13 additions & 0 deletions app/resources/collection/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rs/xid"

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/internal/ent"
)
Expand Down Expand Up @@ -59,3 +60,15 @@ func WithPostRemove(id post.ID) Option {
c.RemovePostIDs(xid.ID(id))
}
}

func WithNodeAdd(id datagraph.NodeID) Option {
return func(c *ent.CollectionMutation) {
c.AddNodeIDs(xid.ID(id))
}
}

func WithNodeRemove(id datagraph.NodeID) Option {
return func(c *ent.CollectionMutation) {
c.RemoveNodeIDs(xid.ID(id))
}
}
10 changes: 7 additions & 3 deletions app/resources/reply/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ func (*Reply) GetResourceName() string { return "post" }
func (r *Reply) GetID() xid.ID { return xid.ID(r.ID) }
func (r *Reply) GetKind() datagraph.Kind { return datagraph.KindPost }
func (r *Reply) GetName() string {
if xid.ID(r.RootPostID).IsZero() {
return r.RootThreadTitle
}

return fmt.Sprintf("reply to: %s", r.RootThreadTitle)
}

func (r *Reply) GetSlug() string {
if len(r.RootThreadMark) > 0 {
return r.RootThreadMark
if xid.ID(r.RootPostID).IsZero() {
return r.ID.String()
}
return r.ID.String()
return r.RootThreadMark
}
func (r *Reply) GetText() string { return r.Content.HTML() }
func (r *Reply) GetDesc() string { return r.Content.Short() }
Expand Down
53 changes: 49 additions & 4 deletions app/services/collection/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ import (

"github.com/Southclaws/storyden/app/resources/account"
"github.com/Southclaws/storyden/app/resources/collection"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/resources/rbac"
"github.com/Southclaws/storyden/app/services/authentication/session"
)

type Service interface {
Update(ctx context.Context, cid collection.CollectionID, partial Partial) (*collection.Collection, error)
Add(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error)
Remove(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error)
Delete(ctx context.Context, cid collection.CollectionID) error

PostAdd(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error)
PostRemove(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error)

NodeAdd(ctx context.Context, cid collection.CollectionID, pid datagraph.NodeID) (*collection.Collection, error)
NodeRemove(ctx context.Context, cid collection.CollectionID, pid datagraph.NodeID) (*collection.Collection, error)
}

type Partial struct {
Expand Down Expand Up @@ -74,7 +80,20 @@ func (s *service) Update(ctx context.Context, cid collection.CollectionID, parti
return col, nil
}

func (s *service) Add(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error) {
func (s *service) Delete(ctx context.Context, cid collection.CollectionID) error {
if err := s.authorise(ctx, cid); err != nil {
return err
}

err := s.collection_repo.Delete(ctx, cid)
if err != nil {
return fault.Wrap(err, fctx.With(ctx))
}

return nil
}

func (s *service) PostAdd(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error) {
if err := s.authorise(ctx, cid); err != nil {
return nil, err
}
Expand All @@ -87,7 +106,7 @@ func (s *service) Add(ctx context.Context, cid collection.CollectionID, pid post
return col, nil
}

func (s *service) Remove(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error) {
func (s *service) PostRemove(ctx context.Context, cid collection.CollectionID, pid post.ID) (*collection.Collection, error) {
if err := s.authorise(ctx, cid); err != nil {
return nil, err
}
Expand All @@ -100,6 +119,32 @@ func (s *service) Remove(ctx context.Context, cid collection.CollectionID, pid p
return col, nil
}

func (s *service) NodeAdd(ctx context.Context, cid collection.CollectionID, id datagraph.NodeID) (*collection.Collection, error) {
if err := s.authorise(ctx, cid); err != nil {
return nil, err
}

col, err := s.collection_repo.Update(ctx, cid, collection.WithNodeAdd(id))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

return col, nil
}

func (s *service) NodeRemove(ctx context.Context, cid collection.CollectionID, id datagraph.NodeID) (*collection.Collection, error) {
if err := s.authorise(ctx, cid); err != nil {
return nil, err
}

col, err := s.collection_repo.Update(ctx, cid, collection.WithNodeRemove(id))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

return col, nil
}

func (s *service) authorise(ctx context.Context, cid collection.CollectionID) error {
aid, err := session.GetAccountID(ctx)
if err != nil {
Expand Down
40 changes: 38 additions & 2 deletions app/transports/openapi/bindings/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Southclaws/opt"

"github.com/Southclaws/storyden/app/resources/collection"
"github.com/Southclaws/storyden/app/resources/datagraph"
"github.com/Southclaws/storyden/app/resources/post"
"github.com/Southclaws/storyden/app/services/authentication/session"
collection_svc "github.com/Southclaws/storyden/app/services/collection"
Expand Down Expand Up @@ -88,8 +89,17 @@ func (i *Collections) CollectionUpdate(ctx context.Context, request openapi.Coll
}, nil
}

func (i *Collections) CollectionDelete(ctx context.Context, request openapi.CollectionDeleteRequestObject) (openapi.CollectionDeleteResponseObject, error) {
err := i.collection_svc.Delete(ctx, collection.CollectionID(deserialiseID(request.CollectionId)))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

return openapi.CollectionDelete200Response{}, nil
}

func (i *Collections) CollectionAddPost(ctx context.Context, request openapi.CollectionAddPostRequestObject) (openapi.CollectionAddPostResponseObject, error) {
c, err := i.collection_svc.Add(ctx,
c, err := i.collection_svc.PostAdd(ctx,
collection.CollectionID(deserialiseID(request.CollectionId)),
post.ID(deserialiseID(request.PostId)))
if err != nil {
Expand All @@ -102,7 +112,7 @@ func (i *Collections) CollectionAddPost(ctx context.Context, request openapi.Col
}

func (i *Collections) CollectionRemovePost(ctx context.Context, request openapi.CollectionRemovePostRequestObject) (openapi.CollectionRemovePostResponseObject, error) {
c, err := i.collection_svc.Remove(ctx,
c, err := i.collection_svc.PostRemove(ctx,
collection.CollectionID(deserialiseID(request.CollectionId)),
post.ID(deserialiseID(request.PostId)))
if err != nil {
Expand All @@ -114,6 +124,32 @@ func (i *Collections) CollectionRemovePost(ctx context.Context, request openapi.
}, nil
}

func (i *Collections) CollectionAddNode(ctx context.Context, request openapi.CollectionAddNodeRequestObject) (openapi.CollectionAddNodeResponseObject, error) {
c, err := i.collection_svc.NodeAdd(ctx,
collection.CollectionID(deserialiseID(request.CollectionId)),
datagraph.NodeID(deserialiseID(request.NodeId)))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

return openapi.CollectionAddNode200JSONResponse{
CollectionAddNodeOKJSONResponse: openapi.CollectionAddNodeOKJSONResponse(serialiseCollection(c)),
}, nil
}

func (i *Collections) CollectionRemoveNode(ctx context.Context, request openapi.CollectionRemoveNodeRequestObject) (openapi.CollectionRemoveNodeResponseObject, error) {
c, err := i.collection_svc.NodeRemove(ctx,
collection.CollectionID(deserialiseID(request.CollectionId)),
datagraph.NodeID(deserialiseID(request.NodeId)))
if err != nil {
return nil, fault.Wrap(err, fctx.With(ctx))
}

return openapi.CollectionRemoveNode200JSONResponse{
CollectionRemoveNodeOKJSONResponse: openapi.CollectionRemoveNodeOKJSONResponse(serialiseCollection(c)),
}, nil
}

func serialiseCollection(in *collection.Collection) openapi.Collection {
return openapi.Collection{
Id: in.ID.String(),
Expand Down
Loading

0 comments on commit 0ca2414

Please sign in to comment.