add link user calendar at creation
This commit is contained in:
parent
edbd581ccb
commit
98e3a452a5
4 changed files with 25 additions and 6 deletions
|
@ -3,7 +3,6 @@ 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"
|
||||||
|
@ -78,14 +77,13 @@ 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)
|
err = services.CreateCalendar(tmp, name)
|
||||||
|
|
||||||
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()})
|
||||||
|
|
13
controllers/utils.go
Normal file
13
controllers/utils.go
Normal 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
|
||||||
|
}
|
|
@ -27,11 +27,19 @@ func GetCalendarById(id int) (models.Calendar, error) {
|
||||||
return res, err
|
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()
|
ctx := context.Background()
|
||||||
db := get()
|
db := get()
|
||||||
|
user, _ := GetUserByName(name)
|
||||||
|
|
||||||
_, 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 UNIQUE,
|
email TEXT,
|
||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue