Skip to content

Commit 07af8ab

Browse files
committed
add teams, being m2m relationships, update README
1 parent 01dc804 commit 07af8ab

30 files changed

+902
-138
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# API
22
PORT=8080
33
TOKEN_KEY=
4-
#DEV_MODE=true
4+
DEV_MODE=true
55

66
# DB
77
#DB_DISABLED=true

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
# skillbased.io
1+
# skillbased
22
[![Go Report Card](https://goreportcard.com/badge/github.com/adamdevigili/skillbased.io)](https://goreportcard.com/report/github.com/adamdevigili/skillbased.io)
33

4-
[skillbased](http://skillbased.xyz) is a service that is aimed to provide players of casual, "pick-up" type sports to quickly and easily create balanced teams for their activity, and save those teams to create elevated levels of competition.
4+
[Skillbased](http://skillbased.xyz) is a service that is aimed to provide players of casual, "pick-up" type sports and activities to quickly and easily create balanced teams, and save those teams to create elevated levels of competition. Skillbased is designed to be generic,
5+
allowing an unlimited number of custom `Sports` and `Skills` to be added and modified.
56

67
## Development
78

89
Rename the `.env` file to `.env.local`. The default values should allow the API and postgres to start, however to
9-
enable Auth0 on the frontend, you'll need to supply your own credentials to their corresponding environment variables
10+
enable Auth0 on the frontend, you'll need to supply your own credentials to their corresponding environment variables.
11+
12+
The `DEV_MODE` variable is set to `true` by default, and disables SSL for Postgres.
1013

1114
Build and run the API, frontend, and a postgres database locally with: `docker-compose -f docker-compose.local.yml up --build`
1215

1316
## Tech
1417
### Stack
15-
- Frontend: [ReactJS](https://reactjs.org/)
16-
- Backend: [Go](https://golang.org/) ([echo](https://echo.labstack.com/))
18+
- Frontend: [NextJS w/ Auth0](https://nextjs.org/)
19+
- API: [Go](https://golang.org/)
1720
- Database: [PostgreSQL](https://www.postgresql.org/)
1821
- Runtime: [Docker](https://www.docker.com/)
1922
- Infrastructure: [DigitalOcean](https://www.digitalocean.com/)
2023

21-
2224
### Other tools
2325
- API designer: [insomnia.rest](https://insomnia.rest/)
26+
- Diagraming: [lucid.app](https://lucid.app/)

api/.env

Lines changed: 0 additions & 13 deletions
This file was deleted.

api/go.mod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/badoux/checkmail v1.2.1
77
github.com/dgrijalva/jwt-go v3.2.0+incompatible
88
github.com/google/uuid v1.3.0
9-
github.com/jinzhu/gorm v1.9.16
109
github.com/joho/godotenv v1.4.0
1110
github.com/kelseyhightower/envconfig v1.4.0
1211
github.com/labstack/echo/v4 v4.6.1
@@ -20,7 +19,17 @@ require (
2019
require (
2120
github.com/davecgh/go-spew v1.1.1 // indirect
2221
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
22+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
23+
github.com/jackc/pgconn v1.10.0 // indirect
24+
github.com/jackc/pgio v1.0.0 // indirect
25+
github.com/jackc/pgpassfile v1.0.0 // indirect
26+
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
27+
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
28+
github.com/jackc/pgtype v1.8.1 // indirect
29+
github.com/jackc/pgx/v4 v4.13.0 // indirect
30+
github.com/jinzhu/gorm v1.9.16 // indirect
2331
github.com/jinzhu/inflection v1.0.0 // indirect
32+
github.com/jinzhu/now v1.1.2 // indirect
2433
github.com/mattn/go-colorable v0.1.8 // indirect
2534
github.com/mattn/go-isatty v0.0.14 // indirect
2635
github.com/stretchr/testify v1.7.0 // indirect
@@ -31,4 +40,6 @@ require (
3140
golang.org/x/text v0.3.7 // indirect
3241
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
3342
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
43+
gorm.io/driver/postgres v1.2.2 // indirect
44+
gorm.io/gorm v1.22.3 // indirect
3445
)

api/go.sum

Lines changed: 144 additions & 0 deletions
Large diffs are not rendered by default.

api/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/adamdevigili/skillbased/api/pkg/db"
77
"github.com/adamdevigili/skillbased/api/pkg/server"
8-
dotenv "github.com/joho/godotenv"
98
"github.com/kelseyhightower/envconfig"
109
"github.com/labstack/echo/v4"
1110
"github.com/labstack/gommon/log"
@@ -21,9 +20,6 @@ func main() {
2120
// Create new Echo server
2221
e := echo.New()
2322

24-
// Load env vars for configuration
25-
dotenv.Load(".env")
26-
2723
var dbConfig db.Config
2824
err := envconfig.Process("db", &dbConfig)
2925
if err != nil {

api/pkg/db/init.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import (
44
"fmt"
55

66
"github.com/adamdevigili/skillbased/api/pkg/models"
7-
"github.com/jinzhu/gorm"
8-
_ "github.com/jinzhu/gorm/dialects/postgres"
97
"github.com/labstack/gommon/log"
10-
_ "github.com/lib/pq"
11-
)
12-
13-
const (
14-
dbConnRetryLimit = 5
8+
"gorm.io/driver/postgres"
9+
"gorm.io/gorm"
10+
// _ "github.com/lib/pq"
1511
)
1612

1713
// Environment variables to configure target DB. All are required. Will be looked for with the "PG_" prefix
@@ -54,7 +50,7 @@ func InitDB(dbConfig Config) *gorm.DB {
5450

5551
// Connect to the default "postgres" database first
5652
log.Infof("Attempting initial connection to database", defaultConnStr)
57-
db, err := gorm.Open("postgres", defaultConnStr)
53+
db, err := gorm.Open(postgres.Open(defaultConnStr), &gorm.Config{})
5854
if err != nil {
5955
log.Fatalf("Unable to connect to database with default settings: %v", err)
6056
}
@@ -64,7 +60,7 @@ func InitDB(dbConfig Config) *gorm.DB {
6460
db.Exec(fmt.Sprintf("CREATE DATABASE %s;", dbConfig.Database))
6561

6662
// Connect to the "skillbased" database
67-
db, err = gorm.Open("postgres", connStr)
63+
db, err = gorm.Open(postgres.Open(connStr), &gorm.Config{})
6864
if err != nil {
6965
log.Fatalf("Unable to connect to main database: %v", err)
7066
}
@@ -84,16 +80,24 @@ func InitDB(dbConfig Config) *gorm.DB {
8480
}
8581

8682
func initTables(db *gorm.DB) {
83+
log.Info("initializing tables")
84+
seedPlayers := generateSeedPlayers()
85+
initPlayersTable(db, seedPlayers)
86+
seedTeams := generateSeedTeams(seedPlayers, db)
8787
initSportsTable(db)
88+
initTeamsTable(db, seedTeams)
8889
deleteAllSeedPlayers(db)
89-
initPlayersTable(db)
90+
9091
}
9192

92-
func initPlayersTable(db *gorm.DB) {
93+
func initPlayersTable(db *gorm.DB, seedPlayers []*models.Player) {
9394
log.Info("Populating players database with initial values..")
9495

95-
db.AutoMigrate(&models.Player{})
96-
for _, p := range generateSeedPlayers() {
96+
if err := db.AutoMigrate(&models.Player{}); err != nil {
97+
log.Fatalf("Unable to migrate schema: %v", err)
98+
}
99+
100+
for _, p := range seedPlayers {
97101
if err := InsertPlayer(db, p); err != nil {
98102
log.Warn(err)
99103
}
@@ -108,10 +112,27 @@ func deleteAllSeedPlayers(db *gorm.DB) {
108112
func initSportsTable(db *gorm.DB) {
109113
log.Info("Populating sports database with initial values..")
110114

111-
db.AutoMigrate(&models.Sport{})
115+
if err := db.AutoMigrate(&models.Sport{}); err != nil {
116+
log.Fatalf("Unable to migrate schema: %v", err)
117+
}
118+
112119
for _, s := range initialSports {
113120
if err := InsertSport(db, &s); err != nil {
114121
log.Warn(err)
115122
}
116123
}
117124
}
125+
126+
func initTeamsTable(db *gorm.DB, seedTeams []*models.Team) {
127+
log.Info("Initializing Teams table..")
128+
129+
if err := db.AutoMigrate(&models.Team{}); err != nil {
130+
log.Fatalf("Unable to migrate schema: %v", err)
131+
}
132+
133+
for _, t := range seedTeams {
134+
if err := InsertTeam(db, t); err != nil {
135+
log.Warn(err)
136+
}
137+
}
138+
}

api/pkg/db/player.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package db
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/adamdevigili/skillbased/api/pkg/models"
7-
"github.com/jinzhu/gorm"
8+
"gorm.io/gorm"
89
)
910

1011
func InsertPlayer(db *gorm.DB, player *models.Player) error {
@@ -13,7 +14,7 @@ func InsertPlayer(db *gorm.DB, player *models.Player) error {
1314

1415
func GetPlayer(db *gorm.DB, id string) (*models.Player, error) {
1516
player := &models.Player{}
16-
if db.Where("id = ?", id).First(player).RecordNotFound() {
17+
if err := db.Where("id = ?", id).First(player).Error; errors.Is(err, gorm.ErrRecordNotFound) {
1718
return nil, fmt.Errorf("player not found")
1819
}
1920

@@ -39,7 +40,7 @@ func UpdatePlayer(db *gorm.DB, player *models.Player) (*models.Player, error) {
3940
}
4041

4142
func DeletePlayer(db *gorm.DB, id string) error {
42-
if db.Where("id = ?", id).First(&models.Player{}).RecordNotFound() {
43+
if err := db.Where("id = ?", id).First(&models.Player{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
4344
return fmt.Errorf("player not found")
4445
} else {
4546
fmt.Println("ya")

api/pkg/db/seed.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import (
44
"fmt"
55

66
"github.com/adamdevigili/skillbased/api/pkg/models"
7+
"github.com/labstack/gommon/log"
78
"github.com/pioz/faker"
89
"github.com/segmentio/ksuid"
10+
"gorm.io/gorm"
911
)
1012

1113
var (
1214
ultimateFrisbee = models.Sport{
1315
Base: models.Base{
14-
Name: "Ultimate Frisbee",
15-
ID: ksuid.New().String(),
16+
Name: "Ultimate Frisbee",
17+
ID: ksuid.New().String(),
18+
IsSeed: true,
1619
},
1720
SkillWeights: models.SkillWeightMap{
1821
"handling": 0.9,
@@ -26,8 +29,9 @@ var (
2629

2730
football = models.Sport{
2831
Base: models.Base{
29-
Name: "Football",
30-
ID: ksuid.New().String(),
32+
Name: "Football",
33+
ID: ksuid.New().String(),
34+
IsSeed: true,
3135
},
3236
SkillWeights: models.SkillWeightMap{
3337
"strength": 0.7,
@@ -41,8 +45,9 @@ var (
4145

4246
basketball = models.Sport{
4347
Base: models.Base{
44-
Name: "Basketball",
45-
ID: ksuid.New().String(),
48+
Name: "Basketball",
49+
ID: ksuid.New().String(),
50+
IsSeed: true,
4651
},
4752
SkillWeights: models.SkillWeightMap{
4853
"shooting": 0.9,
@@ -74,11 +79,11 @@ func generateSeedPlayers() []*models.Player {
7479
FirstName: fn,
7580
LastName: ln,
7681
Base: models.Base{
77-
Name: fmt.Sprintf("%s %s", fn, ln),
78-
ID: ksuid.New().String(),
82+
Name: fmt.Sprintf("%s %s", fn, ln),
83+
ID: ksuid.New().String(),
84+
IsSeed: true,
7985
},
8086
PowerScores: make(map[string]int),
81-
IsSeed: true,
8287
}
8388

8489
for _, s := range models.SkillsList {
@@ -90,3 +95,33 @@ func generateSeedPlayers() []*models.Player {
9095

9196
return players
9297
}
98+
99+
func generateSeedTeams(seedPlayers []*models.Player, db *gorm.DB) []*models.Team {
100+
teamSize := 5
101+
numTeams := len(seedPlayers) / teamSize
102+
teams := make([]*models.Team, numTeams)
103+
104+
log.Infof("%+v", teams, teamSize, numTeams, len(seedPlayers))
105+
currPlayer := 0
106+
for i := range teams {
107+
t := &models.Team{
108+
Base: models.Base{
109+
Name: faker.ColorName(),
110+
ID: ksuid.New().String(),
111+
IsSeed: true,
112+
},
113+
}
114+
115+
for i := currPlayer; i < currPlayer+i; i++ {
116+
// t.Players = append(t.Players, seedPlayers[i])
117+
db.Model(t).Association("Players").Append()
118+
db.Model(seedPlayers[i]).Association("Teams").Append(seedPlayers[i])
119+
}
120+
121+
log.Infof("generated team: %+v", t)
122+
123+
teams[i] = t
124+
}
125+
126+
return teams
127+
}

api/pkg/db/sport.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package db
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/adamdevigili/skillbased/api/pkg/models"
7-
"github.com/jinzhu/gorm"
8+
"gorm.io/gorm"
89
)
910

1011
func InsertSport(db *gorm.DB, sport *models.Sport) error {
@@ -13,7 +14,7 @@ func InsertSport(db *gorm.DB, sport *models.Sport) error {
1314

1415
func GetSport(db *gorm.DB, id string) (*models.Sport, error) {
1516
sport := &models.Sport{}
16-
if db.Where("id = ?", id).First(sport).RecordNotFound() {
17+
if err := db.Where("id = ?", id).First(sport).Error; errors.Is(err, gorm.ErrRecordNotFound) {
1718
return nil, fmt.Errorf("sport not found")
1819
}
1920

@@ -39,7 +40,7 @@ func UpdateSport(db *gorm.DB, sport *models.Sport) (*models.Sport, error) {
3940
}
4041

4142
func DeleteSport(db *gorm.DB, id string) error {
42-
if db.Where("id = ?", id).First(&models.Sport{}).RecordNotFound() {
43+
if err := db.Where("id = ?", id).First(&models.Sport{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
4344
return fmt.Errorf("sport not found")
4445
} else {
4546
fmt.Println("ya")

0 commit comments

Comments
 (0)