Skip to content

Commit e076afe

Browse files
committed
feature: workshops. Almost completed currently debugging
1 parent da80702 commit e076afe

8 files changed

Lines changed: 889 additions & 0 deletions

File tree

apps/api/internal/api/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/swamphacks/core/apps/api/internal/domains/redeemables"
2525
"github.com/swamphacks/core/apps/api/internal/domains/teams"
2626
"github.com/swamphacks/core/apps/api/internal/domains/users"
27+
"github.com/swamphacks/core/apps/api/internal/domains/workshops"
2728
"github.com/swamphacks/core/apps/api/internal/emailutils"
2829
"github.com/swamphacks/core/apps/api/internal/logger"
2930
"github.com/swamphacks/core/apps/api/internal/storage"
@@ -116,6 +117,7 @@ func Run() {
116117
redeemablesRepo := repository.NewRedeemablesRepository(db)
117118
batRunsRepo := repository.NewBatRunsRepository(db)
118119
eventInterestsRepo := repository.NewEventInterestsRepository(db)
120+
workshopRepo := repository.NewWorkshopsRepository(db)
119121

120122
mw := mw.NewMiddleware(userRepo, db, logger, config)
121123

@@ -149,6 +151,10 @@ func Run() {
149151
redeemablesHandler := redeemables.NewHandler(redeemablesService, config, logger)
150152
redeemables.RegisterRoutes(redeemablesHandler, huma.NewGroup(api, "/redeemables"), mw)
151153

154+
workshopService := workshops.NewService(workshopRepo, logger)
155+
workshopHandler := workshops.NewHandler(workshopService, logger)
156+
workshops.RegisterRoutes(workshopHandler, huma.NewGroup(api, "/workshops"), mw)
157+
152158
huma.Register(api, huma.Operation{
153159
OperationID: "ping",
154160
Method: http.MethodGet,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
4+
-- TABLES
5+
create table workshops
6+
(
7+
id uuid default gen_random_uuid() not null primary key,
8+
title text not null,
9+
description text,
10+
start_time timestamptz not null,
11+
end_time timestamptz not null,
12+
curr_attendees integer default 0 not null,
13+
location text,
14+
presenter text,
15+
created_at timestamptz default now() not null,
16+
updated_at timestamptz default now() not null
17+
);
18+
19+
create table workshop_registrations
20+
(
21+
user_id uuid not null,
22+
workshop_id uuid not null,
23+
created_at timestamptz default now() not null,
24+
25+
primary key (user_id, workshop_id),
26+
27+
foreign key (workshop_id)
28+
references workshops(id)
29+
on delete cascade
30+
);
31+
32+
-- TRIGGERS
33+
create trigger set_updated_at_workshops
34+
before update
35+
on workshops
36+
for each row
37+
execute procedure update_modified_column();
38+
39+
-- +goose StatementEnd
40+
-- +goose Down
41+
drop trigger if exists set_updated_at_workshops on workshops;
42+
43+
drop table if exists workshop_registrations;
44+
45+
drop table if exists workshops;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- name: GetWorkshop :one
2+
SELECT w.* FROM workshops w
3+
WHERE w.id = @workshop_id;
4+
5+
-- name: GetAllWorkshops :many
6+
SELECT w.* FROM workshops w
7+
WHERE w.start_time > CURRENT_TIMESTAMP
8+
ORDER BY w.start_time ASC;
9+
10+
-- name: DeleteWorkshop :exec
11+
DELETE FROM workshops
12+
WHERE id = @workshop_id;
13+
14+
-- name: CreateWorkshop :one
15+
INSERT INTO workshops (title, description, start_time, end_time, location, presenter)
16+
VALUES (@title, @description, @start_time, @end_time, @location, @presenter)
17+
RETURNING *;
18+
19+
-- name: RegisterUserForWorkshop :exec
20+
INSERT INTO workshop_registrations (user_id, workshop_id)
21+
VALUES (@user_id, @workshop_id);
22+
23+
-- name: UnregisterUserForWorkshop :exec
24+
DELETE FROM workshop_registrations
25+
WHERE user_id = @user_id AND workshop_id = @workshop_id;
26+
27+
-- name: IsUserRegistered :one
28+
SELECT EXISTS (
29+
SELECT 1
30+
FROM workshop_registrations
31+
WHERE user_id = @user_id AND workshop_id = @workshop_id
32+
);
33+
34+
-- name: GetWorkshopRegistrations :many
35+
SELECT *
36+
FROM workshop_registrations
37+
WHERE workshop_id = @workshop_id;
38+
39+
-- name: IncrementWorkshopAttendees :exec
40+
UPDATE workshops
41+
SET curr_attendees = curr_attendees + 1
42+
WHERE id = $1;
43+
44+
-- name: DecrementWorkshopAttendees :exec
45+
UPDATE workshops
46+
SET curr_attendees = GREATEST(curr_attendees - 1, 0)
47+
WHERE id = $1;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
7+
"github.com/google/uuid"
8+
"github.com/swamphacks/core/apps/api/internal/database"
9+
"github.com/swamphacks/core/apps/api/internal/database/sqlc"
10+
)
11+
12+
type WorkshopsRepository struct {
13+
db *database.DB
14+
}
15+
16+
func NewWorkshopsRepository(db *database.DB) *WorkshopsRepository{
17+
return &WorkshopsRepository{
18+
db: db,
19+
}
20+
}
21+
22+
func (r *WorkshopsRepository) GetWorkshop(ctx context.Context, workshopID uuid.UUID) (*sqlc.Workshop, error) {
23+
workshop, err := r.db.Query.GetWorkshop(ctx, workshopID)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
return &workshop, nil
29+
}
30+
31+
func (r *WorkshopsRepository) GetAllWorkshops(ctx context.Context) ([]sqlc.Workshop, error){
32+
workshop, err := r.db.Query.GetAllWorkshops(ctx)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return workshop, nil
38+
}
39+
40+
func (r *WorkshopsRepository) DeleteWorkshop(ctx context.Context, workshopID uuid.UUID) error {
41+
err := r.db.Query.DeleteWorkshop(ctx, workshopID)
42+
if err != nil {
43+
return err
44+
}
45+
46+
return nil
47+
}
48+
49+
func (r *WorkshopsRepository) UnregisterUserForWorkshop(ctx context.Context, params sqlc.UnregisterUserForWorkshopParams) error {
50+
51+
err := r.db.Query.UnregisterUserForWorkshop(ctx, params)
52+
if err != nil {
53+
return err
54+
}
55+
56+
return nil
57+
}
58+
59+
func (r *WorkshopsRepository) RegisterUserForWorkshop(ctx context.Context, params sqlc.RegisterUserForWorkshopParams) error {
60+
61+
err := r.db.Query.RegisterUserForWorkshop(ctx, params)
62+
if err != nil {
63+
return err
64+
}
65+
66+
return nil
67+
}
68+
69+
func (r *WorkshopsRepository) DecrementWorkshopAttendees(ctx context.Context, workshopID uuid.UUID) error {
70+
71+
err := r.db.Query.DecrementWorkshopAttendees(ctx, workshopID)
72+
if err != nil {
73+
return err
74+
}
75+
76+
return nil
77+
}
78+
79+
func (r *WorkshopsRepository) CreateWorkshop(ctx context.Context, params sqlc.CreateWorkshopParams) (*sqlc.Workshop, error){
80+
workshop, err := r.db.Query.CreateWorkshop(ctx, params)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
return &workshop, nil
86+
}
87+
88+
func (r *WorkshopsRepository) IncrementWorkshopAttendees(ctx context.Context, workshopID uuid.UUID) error {
89+
err := r.db.Query.IncrementWorkshopAttendees(ctx, workshopID)
90+
if err != nil {
91+
return err
92+
}
93+
94+
return nil
95+
}

apps/api/internal/database/sqlc/models.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)