Skip to content

Commit

Permalink
add export endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
totegamma committed May 5, 2024
1 parent 9a84543 commit 8893327
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func main() {
apiV1.GET("/message/:id/associations", associationHandler.GetFiltered)
apiV1.GET("/message/:id/associationcounts", associationHandler.GetCounts)
apiV1.GET("/message/:id/associations/mine", associationHandler.GetOwnByTarget, auth.Restrict(auth.ISKNOWN))
apiV1.GET("/messages/mine", messageHandler.GetOwn, auth.Restrict(auth.ISKNOWN))

// association
apiV1.POST("/association", associationHandler.Post, auth.Restrict(auth.ISKNOWN))
Expand Down
18 changes: 18 additions & 0 deletions x/message/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var tracer = otel.Tracer("message")
// Handler is the interface for handling HTTP requests
type Handler interface {
Get(c echo.Context) error
GetOwn(c echo.Context) error
Post(c echo.Context) error
Delete(c echo.Context) error
}
Expand Down Expand Up @@ -62,6 +63,23 @@ func (h handler) Get(c echo.Context) error {
})
}

// GetOwn returns an message by Owner
func (h handler) GetOwn(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "HandlerGetOwn")
defer span.End()

requester, ok := c.Get(core.RequesterIdCtxKey).(string)
if !ok {
return c.JSON(http.StatusForbidden, echo.Map{"status": "error", "message": "requester not found"})
}

messages, err := h.service.GetByOwner(ctx, requester)
if err != nil {
return c.JSON(http.StatusNotFound, echo.Map{"error": err.Error()})
}
return c.JSON(http.StatusOK, echo.Map{"status": "ok", "content": messages})
}

// Post creates a new message
// returns the created message
func (h handler) Post(c echo.Context) error {
Expand Down
11 changes: 11 additions & 0 deletions x/message/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Repository interface {
Create(ctx context.Context, message core.Message) (core.Message, error)
Get(ctx context.Context, key string) (core.Message, error)
GetByOwner(ctx context.Context, key string) ([]core.Message, error)
GetWithAssociations(ctx context.Context, key string) (core.Message, error)
GetWithOwnAssociations(ctx context.Context, key string, ccid string) (core.Message, error)
Delete(ctx context.Context, key string) (core.Message, error)
Expand Down Expand Up @@ -82,6 +83,16 @@ func (r *repository) Get(ctx context.Context, key string) (core.Message, error)
return message, err
}

// GetByOwner returns a message by Owner
func (r *repository) GetByOwner(ctx context.Context, key string) ([]core.Message, error) {
ctx, span := tracer.Start(ctx, "RepositoryGetByOwner")
defer span.End()

var messages []core.Message
err := r.db.WithContext(ctx).Where("author = ?", key).Find(&messages).Error
return messages, err
}

// GetWithOwnAssociations returns a message by ID with associations
func (r *repository) GetWithOwnAssociations(ctx context.Context, key string, ccid string) (core.Message, error) {
ctx, span := tracer.Start(ctx, "RepositoryGet")
Expand Down
9 changes: 9 additions & 0 deletions x/message/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Provides methods for message CRUD
type Service interface {
Get(ctx context.Context, id string, requester string) (core.Message, error)
GetByOwner(ctx context.Context, owner string) ([]core.Message, error)
GetWithOwnAssociations(ctx context.Context, id string, requester string) (core.Message, error)
PostMessage(ctx context.Context, objectStr string, signature string, streams []string) (core.Message, error)
Delete(ctx context.Context, id string) (core.Message, error)
Expand Down Expand Up @@ -70,6 +71,14 @@ func (s *service) Get(ctx context.Context, id string, requester string) (core.Me
return message, nil
}

// GetByOwner returns messages by owner
func (s *service) GetByOwner(ctx context.Context, owner string) ([]core.Message, error) {
ctx, span := tracer.Start(ctx, "ServiceGetByOwner")
defer span.End()

return s.repo.GetByOwner(ctx, owner)
}

// GetWithOwnAssociations returns a message by ID with associations
func (s *service) GetWithOwnAssociations(ctx context.Context, id string, requester string) (core.Message, error) {
ctx, span := tracer.Start(ctx, "ServiceGetWithOwnAssociations")
Expand Down

0 comments on commit 8893327

Please sign in to comment.