From 07699aef77ef7b49fa6aa036fcfd24c326b1dfa5 Mon Sep 17 00:00:00 2001 From: Karen Zhao Date: Fri, 29 Dec 2023 22:46:54 +0000 Subject: [PATCH 1/5] feat: Partially implemented EventStore --- store/event.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 store/event.go diff --git a/store/event.go b/store/event.go new file mode 100644 index 0000000..d698f1d --- /dev/null +++ b/store/event.go @@ -0,0 +1,83 @@ +package store +import ( + "context" + "fmt" + "strconv" + "time" + "github.com/jmoiron/sqlx" +) + +type Event struct { + ID int `db:"id" json:"id"` + 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"` + CreatedAt time.Time `db:"created_at" json:"createdAt"` +} + +type EventStore interface { + GetAll(ctx context.Context) ([]Event, error) + GetOne(ctx context.Context, id int) (*Event, error) + GetOneByStartDate(ctx context.Context, startDate string) (*Event, error) + GetOneByName(ctx context.Context, name string) (*Event, error) // ?? + Create(ctx context.Context, event Event) (*Event, error) + Update(ctx context.Context, event Event) (*Event, error) + Delete(ctx context.Context, id int) error +} + +type pgEventStore struct { + db *sqlx.DB +} + +func NewPGEventStore(db *sqlx.DB) EventStore { + return &pgEventStore{db} +} + +func (s *pgUserStore) GetAll(ctx context.Context) ([]Event, error) { + var e []Event + err := s.db.SelectContext(ctx, &e, "SELECT * FROM events") + if err != nil { + return nil, fmt.Errorf("Unable to retrieve events from database, error %w", err) + } + return e, nil +} + +func (s *pgUserStore) GetOne(ctx context.Context, id int) (*Event, error) { + var e Event + err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE id = $1", id) + if err != nil { + return nil, fmt.Errorf("Unable to find event with id, error %w", err) + } + return &u, nil +} + + +func (s *pgUserStore) GetOneByName(ctx context.Context, name string) (*Event, error) { + var e Event + err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE LOWER(name) = LOWER($1)", name) + if err != nil { + return nil, fmt.Errorf("Unable to find event with name %s, with error %w", name, err) + } + return &e, nil +} + +// Don't know if I handled date right here +func (s *pgUserStore) GetOneByStartDate(ctx context.Context, date time.Time) (*Event, error) { + var e Event + err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE start_date = $1", date) + if err != nil { + return nil, fmt.Errorf("Unable to find event with start date %s, with error %w", date, err) + } + return &e, nil +} + +func (s *pgUserStore) Create(ctx context.Context, event Event) (*Event, error) { + query := "INSERT INTO events (name, description, start_date, end_date, volunteers_required) VALUES ($1, $2, $3, $4, $5) RETURNING id, created_at" + err = s.db.QueryRowContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired).Scan(&event.ID, &event.CreatedAt) + if err != nil { + return nil, fmt.Errorf("Unable to create and store an event, error %w", err) + } + return &event, nil +} From 658ae49ecf93c49e7989412143925240376fe8fe Mon Sep 17 00:00:00 2001 From: Karen Zhao Date: Fri, 29 Dec 2023 23:04:58 +0000 Subject: [PATCH 2/5] fix: fixed issues within previous commit --- store/event.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/store/event.go b/store/event.go index d698f1d..60353c7 100644 --- a/store/event.go +++ b/store/event.go @@ -2,7 +2,6 @@ package store import ( "context" "fmt" - "strconv" "time" "github.com/jmoiron/sqlx" ) @@ -20,7 +19,7 @@ type Event struct { type EventStore interface { GetAll(ctx context.Context) ([]Event, error) GetOne(ctx context.Context, id int) (*Event, error) - GetOneByStartDate(ctx context.Context, startDate string) (*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) Update(ctx context.Context, event Event) (*Event, error) @@ -35,49 +34,57 @@ func NewPGEventStore(db *sqlx.DB) EventStore { return &pgEventStore{db} } -func (s *pgUserStore) GetAll(ctx context.Context) ([]Event, error) { +func (s *pgEventStore) GetAll(ctx context.Context) ([]Event, error) { var e []Event err := s.db.SelectContext(ctx, &e, "SELECT * FROM events") if err != nil { - return nil, fmt.Errorf("Unable to retrieve events from database, error %w", err) + return nil, fmt.Errorf("unable to retrieve events from database, error %w", err) } return e, nil } -func (s *pgUserStore) GetOne(ctx context.Context, id int) (*Event, error) { +func (s *pgEventStore) GetOne(ctx context.Context, id int) (*Event, error) { var e Event err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE id = $1", id) if err != nil { - return nil, fmt.Errorf("Unable to find event with id, error %w", err) + return nil, fmt.Errorf("unable to find event with id, error %w", err) } - return &u, nil + return &e, nil } -func (s *pgUserStore) GetOneByName(ctx context.Context, name string) (*Event, error) { +func (s *pgEventStore) GetOneByName(ctx context.Context, name string) (*Event, error) { var e Event err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE LOWER(name) = LOWER($1)", name) if err != nil { - return nil, fmt.Errorf("Unable to find event with name %s, with error %w", name, err) + return nil, fmt.Errorf("unable to find event with name %s, with error %w", name, err) } return &e, nil } // Don't know if I handled date right here -func (s *pgUserStore) GetOneByStartDate(ctx context.Context, date time.Time) (*Event, error) { +func (s *pgEventStore) GetOneByStartDate(ctx context.Context, date time.Time) (*Event, error) { var e Event err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE start_date = $1", date) if err != nil { - return nil, fmt.Errorf("Unable to find event with start date %s, with error %w", date, err) + return nil, fmt.Errorf("unable to find event with start date %s, with error %w", date, err) } return &e, nil } -func (s *pgUserStore) Create(ctx context.Context, event Event) (*Event, error) { +func (s *pgEventStore) Create(ctx context.Context, event Event) (*Event, error) { query := "INSERT INTO events (name, description, start_date, end_date, volunteers_required) VALUES ($1, $2, $3, $4, $5) RETURNING id, created_at" - err = s.db.QueryRowContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired).Scan(&event.ID, &event.CreatedAt) + err := s.db.QueryRowContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired).Scan(&event.ID, &event.CreatedAt) if err != nil { - return nil, fmt.Errorf("Unable to create and store an event, error %w", err) + return nil, fmt.Errorf("unable to create and store an event, error %w", err) } return &event, nil } + + +func (s *pgEventStore) Update(ctx context.Context, event Event) (*Event, error) { + return nil, nil +} +func (s *pgEventStore) Delete(ctx context.Context, id int) error { + return nil +} \ No newline at end of file From d9b73ba8f762a10e003fcc95f67a9d042bcbbbef Mon Sep 17 00:00:00 2001 From: Karen Zhao Date: Tue, 2 Jan 2024 02:03:52 +0000 Subject: [PATCH 3/5] feat: implement Update and Delete --- store/event.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/store/event.go b/store/event.go index 60353c7..bb23776 100644 --- a/store/event.go +++ b/store/event.go @@ -81,10 +81,27 @@ func (s *pgEventStore) Create(ctx context.Context, event Event) (*Event, error) return &event, nil } - func (s *pgEventStore) Update(ctx context.Context, event Event) (*Event, error) { - return nil, nil + query := "UPDATE events SET name = $1, description = $2, start_date = $3, end_date = $4, volunteers_required = $5, WHERE id = $6" + + _, err = s.db.ExecContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired, event.ID) + + if err != nil { + return nil, fmt.Errorf("unable to update event, error %w", err) + + } + + return &event, nil } func (s *pgEventStore) Delete(ctx context.Context, id int) error { + query := "DELETE FROM events WHERE id = $1" + + _, err := s.db.ExecContext(ctx, query, id) + + if err != nil { + return fmt.Errorf("unable to delete event, error %w", err) + + } + return nil } \ No newline at end of file From e5727d62cc3bb7c8f64847f676ba6ae239202ee7 Mon Sep 17 00:00:00 2001 From: Karen Zhao Date: Tue, 2 Jan 2024 09:47:49 +0000 Subject: [PATCH 4/5] fix: type assignment error --- store/event.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/event.go b/store/event.go index bb23776..d22e04c 100644 --- a/store/event.go +++ b/store/event.go @@ -84,7 +84,7 @@ func (s *pgEventStore) Create(ctx context.Context, event Event) (*Event, error) func (s *pgEventStore) Update(ctx context.Context, event Event) (*Event, error) { query := "UPDATE events SET name = $1, description = $2, start_date = $3, end_date = $4, volunteers_required = $5, WHERE id = $6" - _, err = s.db.ExecContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired, event.ID) + _, err := s.db.ExecContext(ctx, query, event.Name, event.Description, event.StartDate, event.EndDate, event.VolunteersRequired, event.ID) if err != nil { return nil, fmt.Errorf("unable to update event, error %w", err) From 3284bf8f1e8c32e4a1207d67629d09c16498b6c3 Mon Sep 17 00:00:00 2001 From: Karen Zhao Date: Wed, 3 Jan 2024 17:00:36 +0000 Subject: [PATCH 5/5] fix: converted date formatting to sql timestamp --- store/event.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/store/event.go b/store/event.go index d22e04c..2ccdb9c 100644 --- a/store/event.go +++ b/store/event.go @@ -65,7 +65,8 @@ func (s *pgEventStore) GetOneByName(ctx context.Context, name string) (*Event, e // Don't know if I handled date right here func (s *pgEventStore) GetOneByStartDate(ctx context.Context, date time.Time) (*Event, error) { var e Event - err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE start_date = $1", date) + formattedDate := date.Format("2006-01-02 15:04"); + err := s.db.GetContext(ctx, &e, "SELECT * FROM events WHERE TO_CHAR(start_date, 'YYYY-MM-DD HH24:MI') = $1", formattedDate) if err != nil { return nil, fmt.Errorf("unable to find event with start date %s, with error %w", date, err) }