Compare commits
No commits in common. "35bc4ff3ccf9c47f138e33aba61b2ceb4d89c162" and "edbd581ccb2aeb9fb275d9337d44d8408cac4a43" have entirely different histories.
35bc4ff3cc
...
edbd581ccb
8 changed files with 9 additions and 423 deletions
controllers
models
routes
services
sql
|
@ -3,6 +3,7 @@ package controllers
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.gnous.eu/Rick/calendrier/models"
|
"git.gnous.eu/Rick/calendrier/models"
|
||||||
"git.gnous.eu/Rick/calendrier/services"
|
"git.gnous.eu/Rick/calendrier/services"
|
||||||
|
@ -77,13 +78,14 @@ func GetCalendarEvents(c *fiber.Ctx) error {
|
||||||
func PostCalendar(c *fiber.Ctx) error {
|
func PostCalendar(c *fiber.Ctx) error {
|
||||||
tmp := new(models.Calendar)
|
tmp := new(models.Calendar)
|
||||||
err := c.BodyParser(tmp)
|
err := c.BodyParser(tmp)
|
||||||
name := GetName(c)
|
|
||||||
|
fmt.Println(tmp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
err = services.CreateCalendar(tmp, name)
|
err = services.CreateCalendar(tmp)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
||||||
|
|
|
@ -1,164 +0,0 @@
|
||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.gnous.eu/Rick/calendrier/models"
|
|
||||||
"git.gnous.eu/Rick/calendrier/services"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// @Summary Retourne tous les évènemenst
|
|
||||||
// @Tag event
|
|
||||||
// @Param name query string false "Nom de l'évènement"
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 {array} models.Event
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /events [get]
|
|
||||||
func GetEvents(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Retourne les informations sur un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 models.Event
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id} [get]
|
|
||||||
func GetEvent(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Créer un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Accept json
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event [post]
|
|
||||||
func PostEvent(c *fiber.Ctx) error {
|
|
||||||
// TODO faire la création du lien avec l'utilisateur
|
|
||||||
// TODO ignorer l'id
|
|
||||||
tmp := new(models.Event)
|
|
||||||
err := c.BodyParser(tmp)
|
|
||||||
name := GetName(c)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
|
||||||
}
|
|
||||||
|
|
||||||
err = services.CreateEvent(tmp, name)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
|
|
||||||
} else {
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Modifie les informations sur un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Accept json
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id} [patch]
|
|
||||||
func PatchEvent(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Retourne les informations sur un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id} [delete]
|
|
||||||
func DeleteEvent(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Retourne les informations de toutes les éditions d'un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Param from query int false "Date minimum de l'édition."
|
|
||||||
// @Param to query int false "Date maximum de l'édition."
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 {array} models.Edition
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou édition introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/editions [get]
|
|
||||||
func GetEditions(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Retourne les informations sur une édition d'un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200 models.Edition
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou édition introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/edition/{id} [get]
|
|
||||||
func GetEdition(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Créer une édition
|
|
||||||
// @Tag event
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/edition [post]
|
|
||||||
func PostEdition(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Met à jour une édition
|
|
||||||
// @Tag event
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou édition introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/edition/{id} [patch]
|
|
||||||
func PatchEdition(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Supprime une édition
|
|
||||||
// @Tag event
|
|
||||||
// @Produce json
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou édition introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/edition/{id} [delete]
|
|
||||||
func DeleteEdition(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Ajoute un utilisateur dans un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou utilisateur introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/user/{id} [post]
|
|
||||||
func PostEventUser(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Supprime un utilisateur dans un évènement
|
|
||||||
// @Tag event
|
|
||||||
// @Success 200
|
|
||||||
// @Failure 401 "Token mal formaté"
|
|
||||||
// @Failure 404 "Évènement ou utilisateur introuvable"
|
|
||||||
// @Failure 500 "Erreur dans la base de données"
|
|
||||||
// @Router /event/{id}/user/{id} [delete]
|
|
||||||
func DeleteEventUser(c *fiber.Ctx) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetName(c *fiber.Ctx) string {
|
|
||||||
t := c.Locals("user").(*jwt.Token)
|
|
||||||
claims := t.Claims.(jwt.MapClaims)
|
|
||||||
name := claims["name"].(string)
|
|
||||||
return name
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
package models
|
|
||||||
|
|
||||||
import "github.com/uptrace/bun"
|
|
||||||
|
|
||||||
type Event struct {
|
|
||||||
bun.BaseModel `bun:"table:event"`
|
|
||||||
Id int `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Site string `json:"site"`
|
|
||||||
IsPublic bool `json:"is_public"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Edition struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
DateFrom int `json:"from"`
|
|
||||||
DateTo int `json:"to"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
location string `json:"location"`
|
|
||||||
}
|
|
|
@ -23,8 +23,6 @@ func SetupApi(app *fiber.App) {
|
||||||
|
|
||||||
api.Post("/calendar", controllers.PostCalendar)
|
api.Post("/calendar", controllers.PostCalendar)
|
||||||
|
|
||||||
api.Post("/event", controllers.PostEvent)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
api.Put("/calendar/:id<int>/visibility", controllers.PutVisibilityCalendar)
|
api.Put("/calendar/:id<int>/visibility", controllers.PutVisibilityCalendar)
|
||||||
api.Delete("/calendar/:id<int>", controllers.DeleteCalendar)
|
api.Delete("/calendar/:id<int>", controllers.DeleteCalendar)
|
||||||
|
|
|
@ -27,22 +27,11 @@ func GetCalendarById(id int) (models.Calendar, error) {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// name est le nom du créateur du calendrier
|
func CreateCalendar(c *models.Calendar) error {
|
||||||
func CreateCalendar(c *models.Calendar, name string) error {
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
db := get()
|
db := get()
|
||||||
user, err := GetUserByName(name)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = db.NewInsert().Model(c).Exec(ctx)
|
_, err := db.NewInsert().Model(c).Exec(ctx)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := map[string]interface{}{"calendar_id": c.Id, "user_id": user.Id, "can_write": true, "is_owner": true}
|
|
||||||
_, err = db.NewInsert().Model(&tmp).Table("user_in_calendar").Exec(ctx)
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,207 +0,0 @@
|
||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"git.gnous.eu/Rick/calendrier/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
// name est le nom du créateur du calendrier
|
|
||||||
func CreateEvent(e *models.Event, name string) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
db := get()
|
|
||||||
user, _ := GetUserByName(name)
|
|
||||||
|
|
||||||
_, err := db.NewInsert().Model(e).Exec(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := map[string]interface{}{"event_id": e.Id, "user_id": user.Id, "can_write": true, "is_owner": true}
|
|
||||||
_, err = db.NewInsert().Model(&tmp).Table("user_in_event").Exec(ctx)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func GetEventById(id int) (models.Event, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
res := new(models.Event)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = conn.QueryRow(ctx, "select * from event where id = $1", id).Scan(&res.Id, &res.DateFrom, &res.DateTo, &res.Name, &res.Site, &res.Creator)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return *res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetEventsByCalendar(calendar_id int) ([]models.Event, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
var res []models.Event
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err = conn.Query(ctx, "select events* from event, event_in_calendar where calendar_id = $1 and even_id = id", user_id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := new(models.Event)
|
|
||||||
for rows.Next() {
|
|
||||||
err = rows.Scan(&tmp.Id, &tmp.DateFrom, &tmp.DateTo, &tmp.Name, &tmp.Site, &tmp.Creator)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if res != nil {
|
|
||||||
res = append(res, *tmp)
|
|
||||||
} else {
|
|
||||||
res = []models.Event{*tmp}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetEventsByCreator(user_id int) ([]models.Event, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
var res []models.Event
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err = conn.Query(ctx, "select * from event where creator = $1", user_id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := new(models.Event)
|
|
||||||
for rows.Next() {
|
|
||||||
err = rows.Scan(&tmp.Id, &tmp.DateFrom, &tmp.DateTo, &tmp.Name, &tmp.Site, &tmp.Creator)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if res != nil {
|
|
||||||
res = append(res, *tmp)
|
|
||||||
} else {
|
|
||||||
res = []models.Event{*tmp}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetEventsByFromDate(from_date int) ([]models.Event, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
var res []models.Event
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
rows, err = conn.Query(ctx, "select * from event where date_from >= $1", from_date)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp := new(models.Event)
|
|
||||||
for rows.Next() {
|
|
||||||
err = rows.Scan(&tmp.Id, &tmp.DateFrom, &tmp.DateTo, &tmp.Name, &tmp.Site, &tmp.Creator)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return res, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if res != nil {
|
|
||||||
res = append(res, *tmp)
|
|
||||||
} else {
|
|
||||||
res = []models.Event{*tmp}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateEvent(e *models.Event) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tx, err := conn.Begin(ctx)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = tx.Exec(ctx, "insert into calendar (name, url, creator, is_public) values ($1, $2, $3, $4)", c.Name, c.Url, c.Creator, c.IsPublic)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tx.Commit(ctx)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteEvent(id int) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
conn, err := get()
|
|
||||||
defer close(conn)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tx, err := conn.Begin(ctx)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := tx.Exec(ctx, "delete from event where id = $1", id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := tx.Exec(ctx, "delete from event_in_calendar where event_id = $1", id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tx.Commit(ctx)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
*/
|
|
|
@ -5,7 +5,7 @@ CREATE DATABASE r_calendar ENCODING 'UTF-8';
|
||||||
CREATE TABLE IF NOT EXISTS c_user (
|
CREATE TABLE IF NOT EXISTS c_user (
|
||||||
id SERIAL PRIMARY KEY NOT NULL,
|
id SERIAL PRIMARY KEY NOT NULL,
|
||||||
name TEXT NOT NULL UNIQUE,
|
name TEXT NOT NULL UNIQUE,
|
||||||
email TEXT,
|
email TEXT UNIQUE,
|
||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS event (
|
||||||
id SERIAL PRIMARY KEY NOT NULL,
|
id SERIAL PRIMARY KEY NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
site TEXT,
|
site TEXT,
|
||||||
is_public BOOLEAN DEFAULT TRUE
|
is_private BOOLEAN DEFAULT TRUE
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS edition (
|
CREATE TABLE IF NOT EXISTS edition (
|
||||||
|
@ -46,8 +46,8 @@ CREATE TABLE IF NOT EXISTS user_in_calendar (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS user_in_event (
|
CREATE TABLE IF NOT EXISTS user_in_event (
|
||||||
event_id INT REFERENCES event(id) NOT NULL,
|
|
||||||
user_id INT REFERENCES c_user(id) NOT NULL,
|
user_id INT REFERENCES c_user(id) NOT NULL,
|
||||||
|
event_id INT REFERENCES event(id) NOT NULL,
|
||||||
can_write BOOLEAN DEFAULT FALSE,
|
can_write BOOLEAN DEFAULT FALSE,
|
||||||
is_owner BOOLEAN DEFAULT FALSE,
|
is_owner BOOLEAN DEFAULT FALSE,
|
||||||
PRIMARY KEY (event_id, user_id)
|
PRIMARY KEY (event_id, user_id)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue