Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Events #52

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/ory/dockertest/v3 v3.10.0
github.com/sendgrid/rest v2.6.9+incompatible
github.com/sendgrid/sendgrid-go v3.14.0+incompatible
github.com/thanhpk/randstr v1.0.6
github.com/vektah/gqlparser/v2 v2.5.11
golang.org/x/crypto v0.21.0
)
Expand Down Expand Up @@ -63,7 +64,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/thanhpk/randstr v1.0.6 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion graph/event.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extend type Query {
allEvents(filter: AllEventsFilter!): [Event!]! @isAuthenticated
allEvents(filter: AllEventsFilter!): [Event!]!
myEvents(userId: Int!): [Event!]!
}

extend type Mutation {
Expand Down
158 changes: 139 additions & 19 deletions graph/generated.go

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

14 changes: 10 additions & 4 deletions graph/resolver/event.resolvers.go

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

2 changes: 1 addition & 1 deletion graph/resolver/user.resolvers.go

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

52 changes: 43 additions & 9 deletions services/event_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func (service Service) CreateEvent(ctx context.Context, user gmodel.User, input
table.Event.UpdatedByID,
).
MODEL(model.Event{
LocationID: &input.LocationID,
Name: input.Name,
Description: input.Description,
Type: &input.Type,
StartDate: input.StartDate,
EndDate: input.EndDate,
LocationID: &input.LocationID,
Name: input.Name,
Description: input.Description,
Type: &input.Type,
StartDate: input.StartDate,
EndDate: input.EndDate,
LocationInstanceID: &location_instances[0].ID,
CreatedByID: &user.ID,
UpdatedByID: &user.ID,
CreatedByID: &user.ID,
UpdatedByID: &user.ID,
}).RETURNING(table.Event.AllColumns)
var new_event gmodel.EventShallow
if err := qb.QueryContext(ctx, db, &new_event); err != nil {
Expand Down Expand Up @@ -91,7 +91,7 @@ func (service Service) FindAllEvents(ctx context.Context, filter gmodel.AllEvent
INNER_JOIN(table.Location, table.Location.ID.EQ(table.Event.LocationID)).
INNER_JOIN(table.Address, table.Address.ID.EQ(table.Location.AddressID)).
INNER_JOIN(table.Country, table.Country.Code.EQ(table.Address.CountryCode)).
LEFT_JOIN(table.LocationImage, table.LocationImage.LocationID.EQ(table.Location.ID)).
LEFT_JOIN(table.LocationImage, table.LocationImage.LocationID.EQ(table.Location.ID)).
LEFT_JOIN(created_by_user_table, created_by_user_table.ID.EQ(table.Event.CreatedByID)).
LEFT_JOIN(updated_by_user_table, updated_by_user_table.ID.EQ(table.Event.CreatedByID)),
).
Expand Down Expand Up @@ -123,3 +123,37 @@ func (service Service) FindAllEvents(ctx context.Context, filter gmodel.AllEvent
}
return events, nil
}

func (service Service) MyEvents(ctx context.Context, user_id int64) ([]gmodel.Event, error) {
query := table.Event.
SELECT(
table.Event.AllColumns,
table.Participant.AllColumns,
table.Location.AllColumns,
table.Address.AllColumns,
table.Country.Name,
table.LocationImage.AllColumns,
table.User.ID.AS("created_by_user_id"),
table.User.Name.AS("created_by_name"),
table.User.Avatar.AS("created_by_avatar"),
).FROM(
table.Event.
INNER_JOIN(table.Location, table.Location.ID.EQ(table.Event.LocationID)).
INNER_JOIN(table.Address, table.Address.ID.EQ(table.Location.AddressID)).
INNER_JOIN(table.Country, table.Country.Code.EQ(table.Address.CountryCode)).
LEFT_JOIN(table.Participant, table.Participant.EventID.EQ(table.Event.ID)).
LEFT_JOIN(table.LocationImage, table.LocationImage.LocationID.EQ(table.Location.ID)).
LEFT_JOIN(table.User, table.User.ID.EQ(table.Event.CreatedByID)),
).WHERE(
table.Participant.UserID.EQ(postgres.Int64(user_id)),
).ORDER_BY(
table.Event.StartDate.ASC(),
)

db := service.DbOrTxQueryable()
var events []gmodel.Event
if err := query.QueryContext(ctx, db, &events); err != nil {
return nil, err
}
return events, nil
}
16 changes: 8 additions & 8 deletions setup/server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const GRAPH_ENDPOINT string = "/graphql"

func NewServer(db_conn *sql.DB, router *chi.Mux) *types.ServerBase {
server := types.ServerBase{
DB: db_conn,
Router: router,
StructValidator: validator.New(),
DB: db_conn,
Router: router,
StructValidator: validator.New(),
MigrationDirectory: "./database/migrations",
}

Expand All @@ -50,11 +50,11 @@ func NewServer(db_conn *sql.DB, router *chi.Mux) *types.ServerBase {
sendgrid_client := sendgrid.NewSendClient(tokens.SendGrid.ApiKey)

service := services.Service{
DB: server.DB,
DB: server.DB,
StructValidator: server.StructValidator,
Tokens: &tokens,
Cloudinary: cloudinary,
Sendgrid: sendgrid_client,
Tokens: &tokens,
Cloudinary: cloudinary,
Sendgrid: sendgrid_client,
}

// TODO: only show in dev environment
Expand All @@ -64,7 +64,7 @@ func NewServer(db_conn *sql.DB, router *chi.Mux) *types.ServerBase {
c := graph.Config{}
c.Resolvers = &gresolver.Resolver{
AppContext: server,
Service: service,
Service: service,
}
c.Directives.IsAuthenticated = service.IsAuthenticatedDirective
graphql_handler := handler.NewDefaultServer(graph.NewExecutableSchema(c))
Expand Down
Loading
Loading