Skip to content

Commit

Permalink
create event model and sent it to front on request
Browse files Browse the repository at this point in the history
  • Loading branch information
caioeverest committed Jan 27, 2024
1 parent bed6d81 commit a779536
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 35 deletions.
4 changes: 3 additions & 1 deletion pkg/event/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ package event
import "go.uber.org/fx"

func Module() fx.Option {
return fx.Module("event")
return fx.Module("event",
fx.Provide(NewEventService),
)
}
56 changes: 46 additions & 10 deletions pkg/event/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,55 @@ package event
import (
"time"

"gorm.io/gorm"

"github.com/lib/pq"
"github.com/marcopollivier/techagenda/pkg/attendee"
"github.com/marcopollivier/techagenda/pkg/user"
"gorm.io/gorm"
)

//go:generate go-enum --marshal --sql -f model.go

type Event struct {
gorm.Model
Name string
Tags []string
Location string
ScheduleDate time.Time
OwnerUserID uint
ExternalLink string

user.User
Title string `json:"title"`
Banner string `json:"banner"`
Description string `json:"description"`
Href string `json:"href"`
TypeOf pq.StringArray `json:"type_of" gorm:"type:text[]"`
BeginDate time.Time `json:"begin"`
EndDate time.Time `json:"end"`
CfpID uint `json:"cfp_id"`
UserID uint `json:"user_id"`
Attendees []attendee.Attendee `json:"attendees"`

Tags []Tags `json:"tags" gorm:"many2many:events_tags"`
Venues []Venue `json:"venues" gorm:"many2many:events_venues"`
Cfp Cfp `json:"cfp"`
User user.User `json:"user"`
}

// func (Event) TableName() string { return "events" }

type Venue struct {
gorm.Model
Alias string `json:"alias"`
Address string `json:"address"`
City string `json:"city"`
Lat string `json:"lat"`
Long string `json:"long"`
}

type Cfp struct {
gorm.Model
BeginDate time.Time `json:"begin"`
EndDate time.Time `json:"end"`
Href string `json:"href"`
}

type Tags struct {
gorm.Model
Tag string `json:"tag"`
}

// ENUM(online, in_person)
type EventTypeOf int
146 changes: 146 additions & 0 deletions pkg/event/model_enum.go

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

63 changes: 59 additions & 4 deletions pkg/event/service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
package event

import (
"context"
"log/slog"
"time"

"github.com/samber/lo"
"gorm.io/gorm"
)

type Service interface {
Create()
Read()
Update()
Delete()
Get(ctx context.Context, name, city string, tags []string, typeOf []EventTypeOf, available bool, page, limit int) (events []Event, err error)
}

type EventService struct {
db *gorm.DB
}

func NewEventService(db *gorm.DB) Service {
return &EventService{
db: db,
}
}

func (e *EventService) Get(
ctx context.Context,
name, city string,
tags []string,
typeOf []EventTypeOf,
available bool,
page, limit int,
) (events []Event, err error) {
if limit == 0 || limit > 100 {
limit = 50
}
now := time.Now()
base := e.db.Joins("User").
Preload("Attendees").Preload("Tags").Preload("Venues").
Offset(page * limit).
Limit(limit)

if lo.IsNotEmpty(name) {
base.Where("name like ?", name)
}
if lo.IsNotEmpty(city) {
base.Where("venues.city in ?", city)
}
if len(tags) > 0 {
base.Where("tags.tag in ?", tags)
}
if len(typeOf) > 0 {
base.Where("? in ANY(typeOf)", typeOf)
}
if available {
base.Where("begin_date <= ?", now).Where("end_date > ?", now)
}

if err = base.Find(&events).Error; err != nil {
slog.ErrorContext(ctx, "Fail to scan events", "error", err.Error())
}
return
}
11 changes: 7 additions & 4 deletions pkg/lending/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
gossr "github.com/natewong1313/go-react-ssr"

"github.com/marcopollivier/techagenda/lib/ssr"
"github.com/marcopollivier/techagenda/pkg/event"
)

func NewLendingHandler(server *echo.Echo) {
func NewLendingHandler(server *echo.Echo, eventService event.Service) {
engine, err := ssr.New("lending", "pkg/lending/props.go")
if err != nil {
slog.Error("Fail to start SSR engine", "error", err)
Expand All @@ -20,15 +21,17 @@ func NewLendingHandler(server *echo.Echo) {
server.Static("/assets", "./ui/public/")

server.GET("/v2", func(c echo.Context) (err error) {
events, _ := eventService.Get(c.Request().Context(), "", "", []string{}, []event.EventTypeOf{}, false, 0, 50)

page := engine.RenderRoute(gossr.RenderConfig{
File: "pages/Lending.tsx",
Title: "TechAgenda",
MetaTags: map[string]string{
"og:title": "TechAgenda",
"og:title": "Tech Agenda",
"description": "A Tech Agenda é um projeto OpenSource que foi criado pensando em ajudar as pessoas a encontrarem eventos de tecnologia perto delas.",
},
Props: &IndexRouteProps{
InitialCount: 171,
Props: &Props{
Events: events,
},
})
return c.HTML(http.StatusOK, string(page))
Expand Down
4 changes: 0 additions & 4 deletions pkg/lending/props.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ import "github.com/marcopollivier/techagenda/pkg/event"
type Props struct {
Events []event.Event
}

type IndexRouteProps struct {
InitialCount int `json:"initialCount"`
}
8 changes: 5 additions & 3 deletions pkg/user/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import "gorm.io/gorm"

type User struct {
gorm.Model
Email string
Name string
Role Role
Email string
Name string
Role Role
Bio string
Avatar string
}

func (u *User) IsAdmin() bool { return u.Role == RoleAdmin }
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/models_enum.go

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

Loading

0 comments on commit a779536

Please sign in to comment.