-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
737 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ db: | |
website: | ||
academicYears_s: 86400 | ||
events_s: 3600 | ||
boards_s: 86400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS member ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
username VARCHAR(255) | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS member; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS board ( | ||
id SERIAL PRIMARY KEY, | ||
member INTEGER NOT NULL REFERENCES member (id), | ||
academic_year INTEGER NOT NULL REFERENCES academic_year (id), | ||
role VARCHAR(255) NOT NULL, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS board; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS organizer ( | ||
id SERIAL PRIMARY KEY, | ||
event INTEGER NOT NULL REFERENCES event (id), | ||
board INTEGER NOT NULL REFERENCES board (id) | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS organizer; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
-- CRUD | ||
|
||
-- name: AcademicYearGetAll :many | ||
SELECT * FROM academic_year | ||
ORDER BY start_year DESC; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- name: BoardGetAll :many | ||
SELECT * FROM board b | ||
INNER JOIN member m ON b.member = m.id | ||
INNER JOIN academic_year a_y ON b.academic_year = a_y.id; | ||
|
||
-- name: BoardCreate :one | ||
INSERT INTO board (member, academic_year, role) | ||
VALUES ($1, $2, $3) | ||
RETURNING id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
-- CRUD | ||
|
||
-- name: EventGetAll :many | ||
SELECT * FROM event; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- name: MemberGetAll :many | ||
SELECT * FROM member; | ||
|
||
-- name: MemberCreate :one | ||
INSERT INTO member (name, username) | ||
VALUES ($1, $2) | ||
RETURNING id; | ||
|
||
-- name: MemberUpdate :exec | ||
UPDATE member | ||
SET name = $1, username = $2 | ||
WHERE id = $3; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- name: OrganizerGetAllByEvent :many | ||
SELECT * FROM organizer | ||
WHERE event = $1; | ||
|
||
-- name: OrganizerCreate :one | ||
INSERT INTO organizer (event, board) | ||
VALUES ($1, $2) | ||
RETURNING id; | ||
|
||
-- name: OrganizerDelete :exec | ||
DELETE FROM organizer | ||
WHERE id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ZeusWPI/events/internal/pkg/db/sqlc" | ||
"github.com/ZeusWPI/events/internal/pkg/model" | ||
"github.com/ZeusWPI/events/pkg/db" | ||
"github.com/ZeusWPI/events/pkg/util" | ||
) | ||
|
||
// Board provides all model.Board related database operations | ||
type Board interface { | ||
GetAll() ([]*model.Board, error) | ||
Save(*model.Board) error | ||
} | ||
|
||
type boardRepo struct { | ||
db db.DB | ||
|
||
year AcademicYear | ||
member Member | ||
} | ||
|
||
// Interface compliance | ||
var _ Board = (*boardRepo)(nil) | ||
|
||
// GetAll returns all boards | ||
func (r *boardRepo) GetAll() ([]*model.Board, error) { | ||
boards, err := r.db.Queries().BoardGetAll(context.Background()) | ||
if err != nil { | ||
return nil, fmt.Errorf("Unable to get all boards | %v", err) | ||
} | ||
|
||
return util.SliceMap(boards, func(b sqlc.BoardGetAllRow) *model.Board { | ||
username := "" | ||
if b.Username.Valid { | ||
username = b.Username.String | ||
} | ||
|
||
return &model.Board{ | ||
ID: int(b.ID), | ||
Member: model.Member{ | ||
ID: int(b.ID_2), | ||
Name: b.Name, | ||
Username: username, | ||
}, | ||
AcademicYear: model.AcademicYear{ | ||
ID: int(b.ID_3), | ||
StartYear: int(b.StartYear), | ||
EndYear: int(b.EndYear), | ||
}, | ||
Role: b.Role, | ||
} | ||
}), nil | ||
} | ||
|
||
// Save creates a new board | ||
func (r *boardRepo) Save(b *model.Board) error { | ||
if b.ID != 0 { | ||
// Already in database | ||
return nil | ||
} | ||
|
||
return r.db.WithRollback(context.Background(), func(q *sqlc.Queries) error { | ||
if b.Member.ID == 0 { | ||
err := r.member.Save(&b.Member) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if b.AcademicYear.ID == 0 { | ||
err := r.year.Save(&b.AcademicYear) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
id, err := q.BoardCreate(context.Background(), sqlc.BoardCreateParams{ | ||
Member: int32(b.Member.ID), | ||
AcademicYear: int32(b.AcademicYear.ID), | ||
Role: b.Role, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Unable to save board %+v | %v", *b, err) | ||
} | ||
|
||
b.ID = int(id) | ||
|
||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.