-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_team.go
More file actions
80 lines (63 loc) · 1.98 KB
/
create_team.go
File metadata and controls
80 lines (63 loc) · 1.98 KB
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
package handler
import (
"encoding/json"
"fmt"
"net/http"
api "github.com/dxta-dev/app/internal/internal-api"
"github.com/dxta-dev/app/internal/util"
"github.com/go-playground/validator/v10"
)
type CreateTeamRequestBody struct {
TeamName string `json:"teamName" validate:"required"`
}
type CreateTeamResponse struct {
TeamId int64 `json:"team_id"`
}
func CreateTeam(validate *validator.Validate) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
body := &CreateTeamRequestBody{}
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
fmt.Printf("Issue while parsing body. Error: %s", err.Error())
util.JSONError(w, util.ErrorParam{Error: "Bad Request"}, http.StatusBadRequest)
return
}
err := validate.Struct(body)
if err != nil {
fmt.Printf("Bad request body: %v", err.Error())
util.JSONError(w, util.ErrorParam{Error: "Bad Request"}, http.StatusBadRequest)
return
}
authId := ctx.Value(util.AuthIdCtxKey).(string)
apiState, err := api.InternalApiState(authId, ctx)
if err != nil {
util.JSONError(w, util.ErrorParam{Error: "Internal Server Error"}, http.StatusInternalServerError)
return
}
organizationId, err := apiState.DB.GetOrganizationIdByAuthId(authId, ctx)
if err != nil {
util.JSONError(w, util.ErrorParam{Error: "Bad request"}, http.StatusBadRequest)
return
}
newTeamRes, err := apiState.DB.CreateTeam(body.TeamName, organizationId, ctx)
if err != nil {
util.JSONError(
w,
util.ErrorParam{Error: "Could not create new team"},
http.StatusInternalServerError,
)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(CreateTeamResponse{TeamId: newTeamRes.Id}); err != nil {
fmt.Printf("Issue while formatting response. Error: %s", err.Error())
util.JSONError(
w,
util.ErrorParam{Error: "Internal Server Error"},
http.StatusInternalServerError,
)
return
}
}
}