add games and groups, use django instead of html

This commit is contained in:
rick 2022-10-15 12:37:44 +02:00
parent 517168dc29
commit 15fc1cc06a
Signed by: Rick
GPG key ID: 4A6223D66294EB20
8 changed files with 238 additions and 39 deletions

38
dao/games.go Normal file
View file

@ -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
}
}

67
dao/groups.go Normal file
View file

@ -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
}
}

28
main.go
View file

@ -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"),

23
models/game.go Normal file
View file

@ -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
}

23
models/group.go Normal file
View file

@ -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
}

17
views/group.django Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<head>
</head>
<html>
<body>
<h1>Groupe {{ name }}</h1>
<h2>Pour le jeu {{ game }}</h2>
{% if joined %}
<p>Vous êtes déjà dedans !</p>
{% else %}
<p>Le rejoindre</p>
{% endif %}
<h2>Events prévus</h2>
</body>
</html>

44
views/index.django Normal file
View file

@ -0,0 +1,44 @@
<!DOCTYPE html>
<head>
</head>
<html>
<body>
<h1>Chasseurs De Succès</h1>
<p>Site pour mieux réunir les chasseurs de succès sur les jeux multijoueurs</p>
<hr />
{% if error %}
<div class="error">
<p>Erreur rencontrée: {{error}}</p>
</div>
{% endif %}
{% if connected %}
<div>
<p>Bienvenue !!</p>
<p>Connexions actives : </p>
{% if discordName %}
<p>Discord : {{ discordName }}</p>
{% else %}
<a href="{{ urlDiscord }}">SE CONNECTER VIA DISCORD ICI</a>
{% endif %}
{% if steamName %}
<p>Steam : {{ steamName }}</p>
{% else %}
<a href="{{ urlSteam }}">SE CONNECTER VIA STEAM ICI</a>
{% endif %}
<p>Vos groupes actuels:</p>
<ul>
{% for g in listGroups %}
<li> <a href="/groups/{{ g.Id.Hex() }}">{{ g.Name }}</a> </li>
{% endfor %}
</ul>
</div>
{% else %}
<a href="{{ urlDiscord }}">SE CONNECTER VIA DISCORD ICI</a>
<a href="{{ urlSteam }}">SE CONNECTER VIA STEAM ICI</a>
{% endif %}
</body>
</html>

View file

@ -1,37 +0,0 @@
<!DOCTYPE html>
<head>
</head>
<html>
<body>
<h1>Chasseurs De Succès</h1>
<p>Site pour mieux réunir les chasseurs de succès sur les jeux multijoueurs</p>
<hr />
{{ if .error }}
<div class="error">
<p>Erreur rencontrée: {{.error}}</p>
</div>
{{ end }}
{{if .connected}}
<div>
<p>Bienvenue !!</p>
<p>Connexions actives : </p>
{{ if .discordName }}
<p>Discord : {{ .discordName }}</p>
{{ else }}
<a href="{{ .urlDiscord }}">SE CONNECTER VIA DISCORD ICI</a>
{{ end }}
{{ if .steamName }}
<p>Steam : {{ .steamName }}</p>
{{ else }}
<a href="{{ .urlSteam }}">SE CONNECTER VIA STEAM ICI</a>
{{ end }}
</div>
{{else}}
<a href="{{ .urlDiscord }}">SE CONNECTER VIA DISCORD ICI</a>
<a href="{{ .urlSteam }}">SE CONNECTER VIA STEAM ICI</a>
{{end}}
</body>
</html>