Skip to content

Commit

Permalink
Finish filters callback
Browse files Browse the repository at this point in the history
  • Loading branch information
caioeverest committed Feb 4, 2024
1 parent 02b96fb commit 69cd88f
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 132 deletions.
55 changes: 55 additions & 0 deletions pkg/event/api.go
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)
}
2 changes: 2 additions & 0 deletions pkg/event/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import "go.uber.org/fx"
func Module() fx.Option {
return fx.Module("event",
fx.Provide(NewEventService),
fx.Provide(NewEventAPI),
fx.Invoke(Router),
)
}
9 changes: 9 additions & 0 deletions pkg/event/router.go
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)
}
8 changes: 6 additions & 2 deletions pkg/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package event
import (
"context"
"errors"
"fmt"
"log/slog"
"strings"
"time"

"github.com/lib/pq"
Expand Down Expand Up @@ -64,7 +66,7 @@ func (e *EventService) Get(
Limit(limit)

if lo.IsNotEmpty(name) {
base.Where("name like ?", name)
base.Where(fmt.Sprintf("title like '%%%s%%'", name))
}
if lo.IsNotEmpty(city) {
base.Where("venues.city = ?", city)
Expand All @@ -73,7 +75,9 @@ func (e *EventService) Get(
base.Where("tags.tag in ?", tags)
}
if len(typeOf) > 0 {
base.Where("? in ANY(typeOf)", typeOf)
base.Where(fmt.Sprintf("type_of <@ array[%s]", strings.Join(lo.Map(typeOf, func(i EventTypeOf, _ int) string {
return fmt.Sprintf("'%s'::eventtypeof", i.String())
}), ",")))
}
if available {
base.Where("begin_date <= ?", now).Where("end_date > ?", now)
Expand Down
29 changes: 15 additions & 14 deletions pkg/lending/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package lending
import (
"log/slog"
"net/http"
"strings"

"github.com/labstack/echo/v4"
gossr "github.com/natewong1313/go-react-ssr"
"github.com/samber/lo"

"github.com/marcopollivier/techagenda/lib/ssr"
"github.com/marcopollivier/techagenda/pkg/event"
Expand All @@ -14,25 +16,16 @@ import (
"github.com/marcopollivier/techagenda/pkg/venue"
)

type QueryParams struct {
Name string `query:"name"`
City string `query:"city"`
Tags []string `query:"tags"`
TypeOf []event.EventTypeOf `query:"type_of"`
Available bool `query:"available"`
Page int `query:"page"`
Limit int `query:"limit"`
}

func NewLendingHandler(server *echo.Echo, eventService event.Service, tagService tag.Service, venueService venue.Service, engine *ssr.Engine) {
server.Static("/assets", "./ui/public/")
server.Static("/favicon.ico", "./ui/public/favicon.ico")

server.GET("/v2", func(c echo.Context) (err error) {
var (
ctx = c.Request().Context()
qp QueryParams
mainTag = ""
ctx = c.Request().Context()
qp event.QueryParams
mainTag = ""
typeOfSlice []string
)
if err = c.Bind(&qp); err != nil {
slog.ErrorContext(ctx, "Error parsing query params", "error", err.Error())
Expand All @@ -42,9 +35,17 @@ func NewLendingHandler(server *echo.Echo, eventService event.Service, tagService
mainTag = qp.Tags[0]
}

if len(qp.TypeOf) > 0 {
typeOfSlice = strings.Split(qp.TypeOf, ",")
}
typeOf := lo.Map(typeOfSlice, func(i string, _ int) event.EventTypeOf {
o, _ := event.ParseEventTypeOf(i)
return o
})

tags, _ := tagService.GetAllTags(ctx)
cities, _ := venueService.GetAllCities(ctx)
events, _ := eventService.Get(ctx, qp.Name, qp.City, qp.Tags, qp.TypeOf, qp.Available, qp.Page, qp.Limit)
events, _ := eventService.Get(ctx, qp.Name, qp.City, qp.Tags, typeOf, qp.Available, qp.Page, qp.Limit)

page := engine.RenderRoute(gossr.RenderConfig{
File: "pages/Lending.tsx",
Expand Down
91 changes: 91 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@headlessui/react": "^1.7.18",
"@heroicons/react": "^2.1.1",
"axios": "^1.6.7",
"moment": "^2.30.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
7 changes: 2 additions & 5 deletions ui/src/components/EventList.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { useState } from 'react';
import { Event } from '../props/generated';
import EventCard from '../organisms/EventCard';
import { Filters } from '../organisms/FilterButton';

interface EventListProps {
events: Event[]
}

export default function EventList({ events }: EventListProps) {

const [eventsState, _] = useState(events);

return (
<div>
<div className="mx-auto py-8 lg:max-w-none">
<div className="space-y-12 lg:grid lg:grid-cols-3 lg:gap-8 lg:space-y-0">
{eventsState !== null ? eventsState.map((event) => (
{events !== null ? events.map((event) => (
<EventCard event={event} />
)) : null}
</div>
Expand Down
Loading

0 comments on commit 69cd88f

Please sign in to comment.