Skip to content

Commit

Permalink
add getCharacterByID API
Browse files Browse the repository at this point in the history
  • Loading branch information
totegamma committed Feb 26, 2024
1 parent 9550586 commit 0841789
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ func main() {

// character
apiV1.PUT("/character", characterHandler.Put, auth.Restrict(auth.ISLOCAL))
apiV1.GET("/characters", characterHandler.Get)
apiV1.GET("/character/:id", characterHandler.Get)
apiV1.GET("/characters", characterHandler.Query)
apiV1.DELETE("/character/:id", characterHandler.Delete, auth.Restrict(auth.ISLOCAL))

// stream
Expand Down
24 changes: 23 additions & 1 deletion x/character/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var tracer = otel.Tracer("character")
// Handler is the interface for handling HTTP requests
type Handler interface {
Get(c echo.Context) error
Query(c echo.Context) error
Put(c echo.Context) error
Delete(c echo.Context) error
}
Expand All @@ -30,11 +31,32 @@ func NewHandler(service Service) Handler {
return &handler{service: service}
}

// Get returns a character by ID
// Get returns a character by id
func (h handler) Get(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "HandlerGet")
defer span.End()

id := c.Param("id")
if id == "" {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Invalid request", "message": "id is required"})
}

character, err := h.service.Get(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.JSON(http.StatusNotFound, echo.Map{"error": "Character not found"})
}
return err
}

return c.JSON(http.StatusOK, echo.Map{"status": "ok", "content": character})
}

// Query returns a character by author and schema
func (h handler) Query(c echo.Context) error {
ctx, span := tracer.Start(c.Request().Context(), "HandlerQuery")
defer span.End()

author := c.QueryParam("author")
schema := c.QueryParam("schema")

Expand Down

0 comments on commit 0841789

Please sign in to comment.