Skip to content

Commit

Permalink
fix: added apimodels package
Browse files Browse the repository at this point in the history
  • Loading branch information
steph-feng committed Feb 24, 2024
1 parent 4af84a4 commit 7c1d97e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
22 changes: 18 additions & 4 deletions store/event.go → apimodels/event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package store
package apimodels

import (
"context"
Expand All @@ -19,12 +19,21 @@ type Event struct {
CreatedAt time.Time `db:"created_at" json:"createdAt"`
}

type CreateEventModel struct {
Name string `db:"name" json:"name"`
Description string `db:"description" json:"description"`
StartDate time.Time `db:"start_date" json:"startDate"`
EndDate time.Time `db:"end_date" json:"endDate"`
VolunteersRequired int `db:"volunteers_required" json:"volunteersRequired"`
Location string `db:"location" json:"location"`
}

type EventStore interface {
GetAll(ctx context.Context) ([]Event, error)
GetOne(ctx context.Context, id int) (*Event, error)
GetOneByStartDate(ctx context.Context, startDate time.Time) (*Event, error)
GetOneByName(ctx context.Context, name string) (*Event, error) // ??
Create(ctx context.Context, event Event) (*Event, error)
Create(ctx context.Context, event CreateEventModel) (*Event, error)
Update(ctx context.Context, event Event) (*Event, error)
Delete(ctx context.Context, id int) error
}
Expand Down Expand Up @@ -75,12 +84,17 @@ func (s *pgEventStore) GetOneByStartDate(ctx context.Context, date time.Time) (*
return &e, nil
}

func (s *pgEventStore) Create(ctx context.Context, event Event) (*Event, error) {
func (s *pgEventStore) Create(ctx context.Context, newEvent CreateEventModel) (*Event, error) {
id := 0
createdAt := time.Time{}

query := "INSERT INTO events (name, description, start_date, end_date, volunteers_required, location) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, created_at"
err := s.db.QueryRowContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired, event.Location).Scan(&event.ID, &event.CreatedAt)
err := s.db.QueryRowContext(ctx, query, newEvent.Name, newEvent.Description, newEvent.StartDate, newEvent.EndDate, newEvent.VolunteersRequired, newEvent.Location).Scan(&id, &createdAt)
if err != nil {
return nil, fmt.Errorf("unable to create and store an event, error %w", err)
}

event := Event{id, newEvent.Name, newEvent.Description, newEvent.StartDate, newEvent.EndDate, newEvent.VolunteersRequired, newEvent.Location, createdAt}
return &event, nil
}

Expand Down
3 changes: 2 additions & 1 deletion db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ CREATE TABLE IF NOT EXISTS events (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
location TEXT,
start_date TIMESTAMP NOT NULL,
end_date TIMESTAMP NOT NULL,
volunteers_required INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

--- Users
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/labstack/echo v3.3.10+incompatible
require (
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/labstack/gommon v0.4.1 // indirect
github.com/lib/pq v1.10.9
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8c
github.com/labstack/gommon v0.4.1 h1:gqEff0p/hTENGMABzezPoPSRtIh1Cvw0ueMOe0/dfOk=
github.com/labstack/gommon v0.4.1/go.mod h1:TyTrpPqxR5KMk8LKVtLmfMjeQ5FEkBYdxLYPw/WfrOM=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
37 changes: 31 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
package main

import (
"blood-for-life-backend/store"
"blood-for-life-backend/apimodels"
"fmt"
"net/http"
"os"

"github.com/jmoiron/sqlx"
"github.com/labstack/echo"
_ "github.com/lib/pq"
)

func main() {
e := echo.New()

db, err := sqlx.Connect() // configuration needed
db, err := sqlx.Connect("postgres", "user=postgres dbname=ubc_blood_for_life sslmode=disable password=5025 host=localhost")

if err != nil {
panic(err)
fmt.Println(err.Error())
}

eventStore := store.NewPGEventStore(db)
defer db.Close()

if err := db.Ping(); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Successfully Connected")
}

loadSchema(db)

eventStore := apimodels.NewPGEventStore(db)
bind(e, eventStore)

e.Logger.Fatal(e.Start(":1323"))

}

func bind(e *echo.Echo, eventStore store.EventStore) {
func loadSchema(db *sqlx.DB) {
file, err := os.ReadFile("./db/schema.sql")
if err != nil {
fmt.Println(err.Error())
}

_, err = db.Exec(string(file))
if err != nil {
fmt.Println(err.Error())
}
}

func bind(e *echo.Echo, eventStore apimodels.EventStore) {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})

e.POST("/api/create", func(c echo.Context) error {
event := new(store.Event)
event := new(apimodels.CreateEventModel)

// parse request body
bindErr := c.Bind(event)
Expand Down

0 comments on commit 7c1d97e

Please sign in to comment.