-
Notifications
You must be signed in to change notification settings - Fork 0
/
channelController.go
114 lines (93 loc) · 2.4 KB
/
channelController.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package api
import (
"github.com/gofiber/fiber/v2"
"github.com/seclusionapp/seclusion/database"
structs "github.com/seclusionapp/seclusion/database/structs"
"github.com/seclusionapp/seclusion/util"
)
func Channels(c *fiber.Ctx) error {
token := c.Cookies("token")
if !util.VerifyToken(token) {
return c.Status(401).JSON(fiber.Map{
"status": "error",
"message": "Invalid Token",
})
}
//Get all channels from DB and return list
if c.Method() == "GET" {
var channels []structs.Channel
database.DB.Find(&channels)
return c.Status(200).JSON(channels)
} else {
return c.Status(400).JSON(fiber.Map{
"status": "error",
"message": "Invalid Method",
})
}
}
func Channel(c *fiber.Ctx) error {
token := c.Cookies("token")
if !util.VerifyToken(token) {
return c.Status(401).JSON(fiber.Map{
"status": "error",
"message": "Invalid Token",
})
}
if c.Method() == "GET" {
var channel structs.Channel
database.DB.Where("id = ?", c.Params("id")).First(&channel) // Get channel from database
if channel.ID == 0 {
return c.Status(404).JSON(fiber.Map{
"status": "error",
"message": "Channel not found",
})
}
return c.JSON(fiber.Map{
"status": "ok",
"method": c.Method(),
"channel": channel,
})
}
if c.Method() == "POST" {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(400).JSON(fiber.Map{
"error": err.Error(),
})
}
// Check if channel name is taken
channel := structs.Channel{}
database.DB.Where("name = ?", data["name"]).First(&channel)
if channel.ID != 0 {
return c.Status(400).JSON(fiber.Map{
"error": "Channel name is already taken",
})
}
channel = structs.Channel{
Name: data["name"],
}
database.DB.Create(&channel)
return c.JSON(fiber.Map{
"status": "ok",
"method": c.Method(),
"channel": channel,
})
}
if c.Method() == "DELETE" {
var channel structs.Channel
database.DB.Where("id = ?", c.Params("id")).First(&channel) // Get channel from database
database.DB.Delete(&channel) // Delete channel from database
return c.JSON(fiber.Map{
"status": "ok",
"method": c.Method(),
"channel": channel,
})
}
return c.Status(400).JSON(fiber.Map{
"status": "error",
"message": "Invalid Method",
})
}
func ChannelMessages(c *fiber.Ctx) error {
return c.Status(200).JSON([]string{"channel", "messages"})
}