aboutsummaryrefslogtreecommitdiffstats
path: root/api/handlers/calendar.go
blob: 85a9b0058b47a9598fa55ec4be16a5bf4f98dc83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package handlers

import (
	"errors"

	"github.com/LMBishop/confplanner/api/dto"
	"github.com/LMBishop/confplanner/pkg/calendar"
	"github.com/gofiber/fiber/v2"
)

func GetCalendar(calendarService calendar.Service, baseURL string) fiber.Handler {
	// TODO create config service
	return func(c *fiber.Ctx) error {
		uid := c.Locals("uid").(int32)

		cal, err := calendarService.GetCalendarForUser(uid)
		if err != nil {
			if errors.Is(err, calendar.ErrCalendarNotFound) {
				return &dto.ErrorResponse{
					Code:    fiber.StatusNotFound,
					Message: "Calendar not found",
				}
			}

			return err
		}

		return &dto.OkResponse{
			Code: fiber.StatusOK,
			Data: &dto.GetCalendarResponse{
				ID:   cal.ID,
				Name: cal.Name,
				Key:  cal.Key,
				URL:  baseURL + "/calendar/ical?name=" + cal.Name + "&key=" + cal.Key,
			},
		}
	}
}

func CreateCalendar(calendarService calendar.Service, baseURL string) fiber.Handler {
	return func(c *fiber.Ctx) error {
		uid := c.Locals("uid").(int32)

		cal, err := calendarService.CreateCalendarForUser(uid)
		if err != nil {
			return err
		}

		return &dto.OkResponse{
			Code: fiber.StatusCreated,
			Data: &dto.CreateCalendarResponse{
				ID:   cal.ID,
				Name: cal.Name,
				Key:  cal.Key,
				URL:  baseURL + "/calendar/ical?name=" + cal.Name + "&key=" + cal.Key,
			},
		}
	}
}

func DeleteCalendar(calendarService calendar.Service) fiber.Handler {
	return func(c *fiber.Ctx) error {
		uid := c.Locals("uid").(int32)

		err := calendarService.DeleteCalendarForUser(uid)
		if err != nil {
			if errors.Is(err, calendar.ErrCalendarNotFound) {
				return &dto.ErrorResponse{
					Code:    fiber.StatusNotFound,
					Message: "Calendar not found",
				}
			}

			return err
		}

		return &dto.OkResponse{
			Code: fiber.StatusOK,
		}
	}
}