add link user calendar at creation

This commit is contained in:
rick 2024-02-25 01:10:45 +01:00
parent edbd581ccb
commit 98e3a452a5
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
4 changed files with 25 additions and 6 deletions

View file

@ -3,7 +3,6 @@ package controllers
import (
"database/sql"
"errors"
"fmt"
"git.gnous.eu/Rick/calendrier/models"
"git.gnous.eu/Rick/calendrier/services"
@ -78,14 +77,13 @@ func GetCalendarEvents(c *fiber.Ctx) error {
func PostCalendar(c *fiber.Ctx) error {
tmp := new(models.Calendar)
err := c.BodyParser(tmp)
fmt.Println(tmp)
name := GetName(c)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})
}
err = services.CreateCalendar(tmp)
err = services.CreateCalendar(tmp, name)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"err": err.Error()})

13
controllers/utils.go Normal file
View file

@ -0,0 +1,13 @@
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
}

View file

@ -27,11 +27,19 @@ func GetCalendarById(id int) (models.Calendar, error) {
return res, err
}
func CreateCalendar(c *models.Calendar) error {
// name est le nom du créateur du calendrier
func CreateCalendar(c *models.Calendar, name string) error {
ctx := context.Background()
db := get()
user, _ := GetUserByName(name)
_, 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
}

View file

@ -5,7 +5,7 @@ CREATE DATABASE r_calendar ENCODING 'UTF-8';
CREATE TABLE IF NOT EXISTS c_user (
id SERIAL PRIMARY KEY NOT NULL,
name TEXT NOT NULL UNIQUE,
email TEXT UNIQUE,
email TEXT,
password TEXT NOT NULL
);