From 15fc1cc06a6f4d2f953acd76025f2899966d3f89 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 15 Oct 2022 12:37:44 +0200 Subject: [PATCH] add games and groups, use django instead of html --- dao/games.go | 38 ++++++++++++++++++++++++++ dao/groups.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 28 +++++++++++++++++-- models/game.go | 23 ++++++++++++++++ models/group.go | 23 ++++++++++++++++ views/group.django | 17 ++++++++++++ views/index.django | 44 ++++++++++++++++++++++++++++++ views/index.html | 37 ------------------------- 8 files changed, 238 insertions(+), 39 deletions(-) create mode 100644 dao/games.go create mode 100644 dao/groups.go create mode 100644 models/game.go create mode 100644 models/group.go create mode 100644 views/group.django create mode 100644 views/index.django delete mode 100644 views/index.html diff --git a/dao/games.go b/dao/games.go new file mode 100644 index 0000000..011c031 --- /dev/null +++ b/dao/games.go @@ -0,0 +1,38 @@ +package dao + +import ( + "cds/models" + "context" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +func GetGameByAppId(id uint) (game *models.Game, err error) { + db, _ := get() + defer disconnect(db.Client()) + coll := db.Collection("games") + + var result models.Game + err = coll.FindOne(context.TODO(), bson.D{{"appid", id}}).Decode(&result) + if err != nil { + return nil, err + } else { + return &result, nil + } +} + +func GetGameById(id string) (game *models.Game, err error) { + db, _ := get() + defer disconnect(db.Client()) + coll := db.Collection("games") + objectId, err := primitive.ObjectIDFromHex(id) + + var result models.Game + err = coll.FindOne(context.TODO(), bson.D{{"_id", objectId}}).Decode(&result) + if err != nil { + return nil, err + } else { + return &result, nil + } +} diff --git a/dao/groups.go b/dao/groups.go new file mode 100644 index 0000000..477a96e --- /dev/null +++ b/dao/groups.go @@ -0,0 +1,67 @@ +package dao + +import ( + "cds/models" + "context" + + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo" +) + +func GetGroupById(id string) (group *models.Group, err error) { + db, _ := get() + objectId, err := primitive.ObjectIDFromHex(id) + defer disconnect(db.Client()) + coll := db.Collection("groups") + + var result models.Group + err = coll.FindOne(context.TODO(), bson.D{{"_id", objectId}}).Decode(&result) + if err != nil { + return nil, err + } else { + return &result, nil + } +} + +func GetGroupByMember(id string) (group []models.Group, err error) { + db, _ := get() + objectId, err := primitive.ObjectIDFromHex(id) + defer disconnect(db.Client()) + coll := db.Collection("groups") + + cursor, err := coll.Find(context.TODO(), bson.D{{"members", objectId}}) + defer cursor.Close(context.TODO()) + if err != nil { + return nil, err + } + + err = cursor.All(context.TODO(), &group) + + if err != nil { + return nil, err + } else { + return group, nil + } +} + +func CheckMemberInGroup(idUser string, idGroup string) (ret bool, err error) { + db, _ := get() + userObject, err := primitive.ObjectIDFromHex(idUser) + groupObject, err := primitive.ObjectIDFromHex(idGroup) + defer disconnect(db.Client()) + coll := db.Collection("groups") + + err = coll.FindOne(context.TODO(), bson.D{ + {"_id", groupObject}, + {"members", userObject}, + }).Err() + + if err == mongo.ErrNoDocuments { + return false, nil + } else if err != nil { + return false, err + } else { + return true, nil + } +} diff --git a/main.go b/main.go index 7f34c41..f7c93ba 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( _ "cds/docs" "github.com/gofiber/fiber/v2" - "github.com/gofiber/template/html" + "github.com/gofiber/template/django" "github.com/joho/godotenv" "github.com/markbates/goth" "github.com/markbates/goth/providers/discord" @@ -31,6 +31,7 @@ func setupRoutes(app *fiber.App) { app.Get("/login/:provider", goth_fiber.BeginAuthHandler) app.Get("/auth/:provider", auth) + app.Get("/groups/:id", groups) } func checkDiscord(c *fiber.Ctx) error { @@ -153,11 +154,13 @@ func index(c *fiber.Ctx) error { token := c.Cookies("token", "") var discord string = "" var steam string = "" + var listGroups []models.Group if token != "" { user, _ := dao.GetById(fmt.Sprint(token)) if user != nil { steam = user.SteamId discord = user.DiscordName + listGroups, _ = dao.GetGroupByMember(fmt.Sprint(token)) } } @@ -167,6 +170,27 @@ func index(c *fiber.Ctx) error { "urlSteam": "/login/steam", "discordName": discord, "steamName": steam, + "listGroups": listGroups, + }) +} + +func groups(c *fiber.Ctx) error { + groupId := c.Params("id") + group, err := dao.GetGroupById(groupId) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + } + + token := c.Cookies("token", "") + joined, _ := dao.CheckMemberInGroup(fmt.Sprint(token), groupId) + game, _ := dao.GetGameById(group.Game.Hex()) + + return c.Render("group", fiber.Map{ + "name": group.Name, + "game": game.Name, + "joined": joined, }) } @@ -184,7 +208,7 @@ func main() { panic(err) } - app := fiber.New(fiber.Config{Views: html.New("./views", ".html")}) + app := fiber.New(fiber.Config{Views: django.New("./views", ".django")}) goth.UseProviders( discord.New( os.Getenv("DISCORD_ID"), diff --git a/models/game.go b/models/game.go new file mode 100644 index 0000000..64ee347 --- /dev/null +++ b/models/game.go @@ -0,0 +1,23 @@ +package models + +import "go.mongodb.org/mongo-driver/bson/primitive" + +type Game struct { + Id primitive.ObjectID `bson:"_id", omitempty` + AppId uint `bson:"appid"` + HasAchievements bool `bson:"hasAchievements"` + IsCoop bool `bson:"isCoop"` + IsMulti bool `bson:"isMulti"` + Name string `bson:"name"` +} + +func NewGame() *Game { + return &Game{primitive.NewObjectID(), 0, false, false, false, ""} +} + +func NewGameName(appid uint, name string) *Game { + ret := NewGame() + ret.AppId = appid + ret.Name = name + return ret +} diff --git a/models/group.go b/models/group.go new file mode 100644 index 0000000..7e46301 --- /dev/null +++ b/models/group.go @@ -0,0 +1,23 @@ +package models + +import "go.mongodb.org/mongo-driver/bson/primitive" + +type Group struct { + Id primitive.ObjectID `bson:"_id", omitempty` + Name string `bson:"name"` + Desc string `bson:"desc"` + NbMax int `bson:"nbMax"` + Members []primitive.ObjectID `bson:"members"` + Game primitive.ObjectID `bson:"game"` +} + +func NewGroup() *Group { + return &Group{primitive.NewObjectID(), "", "", 0, []primitive.ObjectID{}, primitive.NewObjectID()} +} + +func NewGroupNameDesc(name string, desc string) *Group { + ret := NewGroup() + ret.Name = name + ret.Desc = desc + return ret +} diff --git a/views/group.django b/views/group.django new file mode 100644 index 0000000..56c82d8 --- /dev/null +++ b/views/group.django @@ -0,0 +1,17 @@ + + + + + +

Groupe {{ name }}

+

Pour le jeu {{ game }}

+ + {% if joined %} +

Vous êtes déjà dedans !

+ {% else %} +

Le rejoindre

+ {% endif %} + +

Events prévus

+ + diff --git a/views/index.django b/views/index.django new file mode 100644 index 0000000..02e8080 --- /dev/null +++ b/views/index.django @@ -0,0 +1,44 @@ + + + + + +

Chasseurs De Succès

+

Site pour mieux réunir les chasseurs de succès sur les jeux multijoueurs

+
+ + {% if error %} +
+

Erreur rencontrée: {{error}}

+
+ {% endif %} + + {% if connected %} +
+

Bienvenue !!

+

Connexions actives :

+ {% if discordName %} +

Discord : {{ discordName }}

+ {% else %} + SE CONNECTER VIA DISCORD ICI + {% endif %} + + {% if steamName %} +

Steam : {{ steamName }}

+ {% else %} + SE CONNECTER VIA STEAM ICI + {% endif %} + +

Vos groupes actuels:

+ +
+ {% else %} + SE CONNECTER VIA DISCORD ICI + SE CONNECTER VIA STEAM ICI + {% endif %} + + diff --git a/views/index.html b/views/index.html deleted file mode 100644 index bbb596f..0000000 --- a/views/index.html +++ /dev/null @@ -1,37 +0,0 @@ - - - - - -

Chasseurs De Succès

-

Site pour mieux réunir les chasseurs de succès sur les jeux multijoueurs

-
- - {{ if .error }} -
-

Erreur rencontrée: {{.error}}

-
- {{ end }} - - {{if .connected}} -
-

Bienvenue !!

-

Connexions actives :

- {{ if .discordName }} -

Discord : {{ .discordName }}

- {{ else }} - SE CONNECTER VIA DISCORD ICI - {{ end }} - - {{ if .steamName }} -

Steam : {{ .steamName }}

- {{ else }} - SE CONNECTER VIA STEAM ICI - {{ end }} -
- {{else}} - SE CONNECTER VIA DISCORD ICI - SE CONNECTER VIA STEAM ICI - {{end}} - -