diff --git a/README.md b/README.md index d2063aa..5a80a20 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,30 @@ ## Structure du projet * .env : fichier de configuration + * controllers : actions possibles par l'API REST + * docs : doc swagger (swagger -i) + * routes : configuration des routes * views : fichiers html dynamiques * dao : fichiers pour faire des requetes en BDD * models : structures utilisées pour manipuler le contenu de la base de données ## Lancement +### Backend + Il faut mettre en place une base de données [MongoDB](https://www.mongodb.com/). Il est nécessaire d'avoir [une application Discord](https://discord.com/developers/) et [une clé d'API Steam](https://steamcommunity.com/dev). Configurez ensuite le fichier `example.env` et renommez le en `.env`. Lancez le serveur avec `go run main.go`. + +### Documentation + +Installer swager avec Go et lancer le. Vous pouvez démarrer le serveur et aller +sur le lien [de la documentation](localhost:8080/swagger) pour pouvoir la lire en ligne. + +``` +go install github.com/swaggo/swag +swag init +``` diff --git a/controllers/group.go b/controllers/group.go new file mode 100644 index 0000000..a162a5c --- /dev/null +++ b/controllers/group.go @@ -0,0 +1,169 @@ +package controllers + +import ( + "cds/dao" + "fmt" + + "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/mongo" +) + +type Groupe struct { + Name int `json:"name"` + Max int `json:"max"` + Leader int `json:"leader"` + //Players []Account `json:"players"` + //Events []Event `json:events` +} + +/* +type Event struct { + Id int `json:"id"` + Start int `json:"start"` + End int `json:"end"` +} +*/ + +// @Summary Renvoie les informations sur un groupe +// @Tags groups +// @Produce json +// @Success 200 {json} Groupe +// @Failure 400 +// @Failure 404 +// @Router /groups/{id} [get] +func GetInfos(c *fiber.Ctx) error { + token := checkCookie(c) + if token == "" { + return c.SendStatus(fiber.StatusForbidden) + } else { + id := c.Params("id") + group, err := dao.GetGroupById(id) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprint(err), + }) + } else { + return c.Status(fiber.StatusOK).JSON(group) + } + } +} + +// @Summary Renvoie les events d'un groupe +// @Tags groups +// @Produce json +// @Success 200 {json} Groupe +// @Failure 400 +// @Failure 404 +// @Router /groups/{id}/events [get] +func GetEvents(c *fiber.Ctx) error { + return nil +} + +// @Summary Créer un groupe +// @Tags groups +// @Accept json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/create [put] +func CreateGroup(c *fiber.Ctx) error { + return nil +} + +// @Summary Rejoindre un groupe +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/join [put] +func JoinGroup(c *fiber.Ctx) error { + return nil +} + +// @Summary Quitte un groupe +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/leave [put] +func LeaveGroupe(c *fiber.Ctx) error { + return nil +} + +// @Summary Change le chef d'un groupe +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/switch [put] +func ChangeLeader(c *fiber.Ctx) error { + return nil +} + +// @Summary Prépare un event +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/events/plan [put] +func CreateEvent(c *fiber.Ctx) error { + return nil +} + +// @Summary Change un event +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/events/change [put] +func ChangeEvent(c *fiber.Ctx) error { + return nil +} + +// @Summary Ferme un groupe et distribue les points CDS +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/end [put] +func EndGroup(c *fiber.Ctx) error { + return nil +} + +// @Summary Supprime un groupe sans distribuer de points +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/cancel [delete] +func DeleteGroup(c *fiber.Ctx) error { + return nil +} + +// @Summary Supprime un event +// @Tags groups +// @Accept json +// @Produce json +// @Success 200 +// @Failure 400 +// @Failure 404 +// @Router /groups/events/cancel [delete] +func DeleteEvent(c *fiber.Ctx) error { + return nil +} diff --git a/controllers/user.go b/controllers/user.go new file mode 100644 index 0000000..564a2a3 --- /dev/null +++ b/controllers/user.go @@ -0,0 +1,105 @@ +package controllers + +import ( + "cds/dao" + "fmt" + + "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/mongo" +) + +func GetUsers(c *fiber.Ctx) error { + token := checkCookie(c) + if token == "" { + return c.SendStatus(fiber.StatusForbidden) + } else { + id := c.Params("id") + group, err := dao.GetGroupById(id) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprint(err), + }) + } else { + return c.Status(fiber.StatusOK).JSON(group) + } + } +} + +// @Summary Renvoie les informations sur le compte se trouvant sur le cookie. +// @Produce json +// @Success 200 {object} models.User +// @Failure 403 +// @Failure 404 +// @Failure 500 +// @Router /users/current [get] +func GetCurrentUser(c *fiber.Ctx) error { + token := c.Cookies("token", "") + if token == "" { + return c.SendStatus(fiber.StatusForbidden) + } else { + user, err := dao.GetById(token) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprint(err), + }) + } else { + return c.Status(fiber.StatusOK).JSON(user) + } + } +} + +// @Summary Renvoie les informations sur le compte +// @Produce json +// @Success 200 {object} models.User +// @Failure 403 +// @Failure 404 +// @Failure 500 +// @Router /users/{id} [get] +func GetUser(c *fiber.Ctx) error { + token := checkCookie(c) + if token == "" { + return c.SendStatus(fiber.StatusForbidden) + } else { + id := c.Params("id") + user, err := dao.GetById(id) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprint(err), + }) + } else { + return c.Status(fiber.StatusOK).JSON(user) + } + } +} + +// @Summary Renvoie la liste des groupes d'un utilisateur +// @Produce json +// @Success 200 {array} primitive.ObjectID "Liste des ids des groupes" +// @Failure 403 +// @Failure 404 +// @Failure 500 +// @Router /users/{id}/groups [get] +func GetGroupsUser(c *fiber.Ctx) error { + token := checkCookie(c) + if token == "" { + return c.SendStatus(fiber.StatusForbidden) + } else { + id := c.Params("id") + groups, err := dao.GetGroupByMember(id) + if err == mongo.ErrNoDocuments { + return c.SendStatus(fiber.StatusNotFound) + } else if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": fmt.Sprint(err), + }) + } else { + return c.Status(fiber.StatusOK).JSON(groups) + } + } +} diff --git a/controllers/utils.go b/controllers/utils.go new file mode 100644 index 0000000..36e645d --- /dev/null +++ b/controllers/utils.go @@ -0,0 +1,7 @@ +package controllers + +import "github.com/gofiber/fiber/v2" + +func checkCookie(c *fiber.Ctx) string { + return c.Cookies("token", "") +} diff --git a/main.go b/main.go index e360c74..fe895ea 100644 --- a/main.go +++ b/main.go @@ -4,14 +4,14 @@ import ( "cds/controllers" "cds/dao" "cds/models" + "cds/routes" "fmt" "os" - "time" _ "cds/docs" "github.com/gofiber/fiber/v2" - "github.com/gofiber/template/django" + "github.com/gofiber/swagger" "github.com/joho/godotenv" "github.com/markbates/goth" "github.com/markbates/goth/providers/discord" @@ -23,12 +23,11 @@ import ( //const DISCORD_API = "https://discord.com/api/v10/" func setupRoutes(app *fiber.App) { - app.Get("/", index) - - //api := app.Group("/api") - //routes.AccountRoute(api.Group("/accounts")) - //routes.GroupeRoute(api.Group("/groups")) - //app.Get("/swagger/*", swagger.HandlerDefault) + //app.Get("/", index) + api := app.Group("/api") + routes.GroupRoute(api.Group("/groups")) + routes.UserRoute(api.Group("/users")) + app.Get("/swagger/*", swagger.HandlerDefault) app.Get("/login/:provider", goth_fiber.BeginAuthHandler) app.Get("/auth/:provider", controllers.Auth) @@ -98,7 +97,7 @@ func main() { panic(err) } - app := fiber.New(fiber.Config{Views: django.New("./views", ".django")}) + app := fiber.New() goth.UseProviders( discord.New( os.Getenv("DISCORD_ID"), @@ -109,6 +108,8 @@ func main() { ) setupRoutes(app) + app.Static("/", "./dist") + app.Static("/*", "./dist") err = app.Listen(":8080") if err != nil { diff --git a/routes/accounts.go b/routes/accounts.go new file mode 100644 index 0000000..26c3ba4 --- /dev/null +++ b/routes/accounts.go @@ -0,0 +1,13 @@ +package routes + +import ( + "cds/controllers" + + "github.com/gofiber/fiber/v2" +) + +func UserRoute(route fiber.Router) { + route.Get("/current", controllers.GetCurrentUser) + route.Get("/:id", controllers.GetUser) + route.Get("/:id/groups", controllers.GetGroupsUser) +} diff --git a/routes/groupe.go b/routes/groupe.go new file mode 100644 index 0000000..f2cac2b --- /dev/null +++ b/routes/groupe.go @@ -0,0 +1,21 @@ +package routes + +import ( + "cds/controllers" + + "github.com/gofiber/fiber/v2" +) + +func GroupRoute(route fiber.Router) { + route.Get("/:id", controllers.GetInfos) + route.Get("/:id/events", controllers.GetEvents) + route.Put("/create", controllers.CreateGroup) + route.Put("/join", controllers.JoinGroup) + route.Put("/leave", controllers.LeaveGroupe) + route.Put("/switch", controllers.ChangeLeader) + route.Put("/events/plan", controllers.CreateEvent) + route.Put("/events/change", controllers.ChangeEvent) + route.Put("/end", controllers.EndGroup) + route.Delete("/cancel", controllers.DeleteGroup) + route.Delete("/events/cancel", controllers.DeleteEvent) +}