calendrier/services/calendar.go
2024-02-17 13:25:03 +01:00

155 lines
2.5 KiB
Go

package services
import (
"context"
"git.gnous.eu/Rick/calendrier/models"
)
func GetAllCalendar(name string) ([]models.Calendar, error) {
var err error
slice := []models.Calendar{}
db := get()
ctx := context.Background()
err = db.NewSelect().Table("calendar").Where("is_public = TRUE").Where("name like ?", "%"+name+"%").Scan(ctx, &slice)
return slice, err
}
func GetCalendarById(id int) (models.Calendar, error) {
var res models.Calendar
ctx := context.Background()
db := get()
err := db.NewSelect().Table("calendar").Where("id = ?", id).Scan(ctx, &res)
return res, err
}
func CreateCalendar(c *models.Calendar) error {
ctx := context.Background()
db := get()
_, err := db.NewInsert().Model(c).Exec(ctx)
return err
}
/*
func GetCalendarsByOwner(user_id int) ([]models.Calendar, error) {
ctx := context.Background()
conn, err := get()
defer close(conn)
var res []models.Calendar
if err != nil {
return res, err
}
rows, err = conn.Query(ctx, "select * from calendar where owner = $1", user_id)
if err != nil {
return res, err
}
tmp := new(models.Calendar)
for rows.Next() {
err = rows.Scan(&res.Id, &res.Name, &res.Url, &res.IsPublic, &res.Owner)
if err != nil {
return res, err
}
if res != nil {
res = append(res, *tmp)
} else {
res = []models.Calendar{*tmp}
}
}
return res, nil
}
func SetPublic(id int, is_public bool) 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, "update calendar set is_public = $2 where id = $1", id, is_public)
if err != nil {
return err
}
tx.Commit(ctx)
return nil
}
func UpdateCalendar(c *models.Calendar) 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, "update calendar set name = $2, url = $3, owner = $4, is_public = $5 where id = $1", c.Id, c.Name, c.Url, c.Owner, c.IsPublic)
if err != nil {
return err
}
tx.Commit(ctx)
return nil
}
func DeleteCalendar(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 calendar where id = $1", id)
if err != nil {
return err
}
_, err := tx.Exec(ctx, "delete from event_in_calendar where calendar_id = $1", id)
if err != nil {
return err
}
tx.Commit(ctx)
return nil
}
*/