-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02b96fb
commit 69cd88f
Showing
13 changed files
with
436 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package event | ||
|
||
import ( | ||
"log/slog" | ||
"strings" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type EventAPI struct { | ||
service Service | ||
} | ||
|
||
func NewEventAPI(service Service) *EventAPI { | ||
return &EventAPI{ | ||
service: service, | ||
} | ||
} | ||
|
||
type QueryParams struct { | ||
Name string `query:"name"` | ||
City string `query:"city"` | ||
Tags []string `query:"tags"` | ||
TypeOf string `query:"type_of"` | ||
Available bool `query:"available"` | ||
Page int `query:"page"` | ||
Limit int `query:"limit"` | ||
} | ||
|
||
func (e *EventAPI) GetEvents(c echo.Context) (err error) { | ||
var ( | ||
ctx = c.Request().Context() | ||
qp QueryParams | ||
events []Event | ||
typeOf []EventTypeOf | ||
typeOfSlice []string | ||
) | ||
|
||
if err = c.Bind(&qp); err != nil { | ||
slog.ErrorContext(ctx, "Error parsing query params", "error", err.Error()) | ||
return err | ||
} | ||
|
||
if len(qp.TypeOf) > 0 { | ||
typeOfSlice = strings.Split(qp.TypeOf, ",") | ||
} | ||
typeOf = lo.Map(typeOfSlice, func(i string, _ int) EventTypeOf { o, _ := ParseEventTypeOf(i); return o }) | ||
if events, err = e.service.Get(ctx, qp.Name, qp.City, qp.Tags, typeOf, qp.Available, qp.Page, qp.Limit); err != nil { | ||
slog.ErrorContext(ctx, "Fail to get events with this query", "error", err.Error(), "query", qp) | ||
return err | ||
} | ||
|
||
return c.JSON(200, events) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package event | ||
|
||
import "github.com/labstack/echo/v4" | ||
|
||
func Router(server *echo.Echo, handler *EventAPI) { | ||
group := server.Group("/api/events") | ||
|
||
group.GET("", handler.GetEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.