-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_create.go
135 lines (116 loc) · 4.04 KB
/
group_create.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright © 2022 Miguel Ángel Álvarez Cabrerizo <mcabrerizo@arrakis.ovh>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handlers
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/doncicuto/glim/models"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
// AddMembers - TODO comment
func (h *Handler) AddMembers(g *models.Group, members []string) error {
var err error
// Update group
for _, member := range members {
member = strings.TrimSpace(member)
// Find user
createdBy := new(models.User)
err = h.DB.Model(&models.User{}).Where("username = ?", member).Take(&createdBy).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &echo.HTTPError{Code: http.StatusNotFound, Message: fmt.Sprintf("user %s not found", member)}
}
return err
}
// Append association
err = h.DB.Model(&g).Association("Members").Append(createdBy)
if err != nil {
return err
}
}
return nil
}
// SaveGroup - TODO comment
// @Summary Create a new group
// @Description Create a new group
// @Tags groups
// @Accept json
// @Produce json
// @Param group body models.JSONGroupBody true "Group body. Name is required. The members property expect a comma-separated list of usernames e.g 'bob,sally'. The replace property is not used in this command."
// @Success 200 {object} models.GroupInfo
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
// @Failure 404 {object} types.ErrorResponse
// @Failure 406 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Router /groups [post]
// @Security Bearer
func (h *Handler) SaveGroup(c echo.Context) error {
g := new(models.Group)
createdBy := new(models.User)
body := models.JSONGroupBody{}
// Get username that is updating this group
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
uid, ok := claims["uid"].(float64)
if !ok {
return &echo.HTTPError{Code: http.StatusNotAcceptable, Message: "wrong token or missing info in token claims"}
}
if err := h.DB.Model(&models.User{}).Where("id = ?", uint(uid)).First(&createdBy).Error; err != nil {
return &echo.HTTPError{Code: http.StatusForbidden, Message: "wrong user attempting to update group"}
}
// Get request body
if err := c.Bind(&body); err != nil {
return &echo.HTTPError{Code: http.StatusInternalServerError, Message: err.Error()}
}
// Validate body
if body.Name == "" {
return &echo.HTTPError{Code: http.StatusNotAcceptable, Message: "required group name"}
}
// Check if group already exists
err := h.DB.Where("name = ?", body.Name).First(&g).Error
if !errors.Is(err, gorm.ErrRecordNotFound) {
return &echo.HTTPError{Code: http.StatusBadRequest, Message: "group already exists"}
}
// Prepare new UUID
groupUUID := uuid.New().String()
g.UUID = &groupUUID
// Prepare new group
g.Name = &body.Name
g.Description = &body.Description
// Created by
g.CreatedBy = createdBy.Username
g.UpdatedBy = createdBy.Username
// Create group
err = h.DB.Create(&g).Error
if err != nil {
return &echo.HTTPError{Code: http.StatusInternalServerError, Message: err.Error()}
}
// Add members to group
if body.Members != "" {
members := strings.Split(body.Members, ",")
err = h.AddMembers(g, members)
if err != nil {
return &echo.HTTPError{Code: http.StatusInternalServerError, Message: err.Error()}
}
}
// Send group information
showMembers := true
i := models.GetGroupInfo(g, showMembers)
return c.JSON(http.StatusOK, i)
}