forked from red-gold/telar-social-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
74 lines (63 loc) · 2.89 KB
/
update.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
package handlers
import (
"fmt"
"net/http"
"github.com/GMcD/ts-serverless/micros/user-rels/database"
domain "github.com/GMcD/ts-serverless/micros/user-rels/dto"
models "github.com/GMcD/ts-serverless/micros/user-rels/models"
service "github.com/GMcD/ts-serverless/micros/user-rels/services"
"github.com/gofiber/fiber/v2"
"github.com/red-gold/telar-core/pkg/log"
"github.com/red-gold/telar-core/types"
"github.com/red-gold/telar-core/utils"
)
// UpdateUserRelHandle handle create a new userRel
func UpdateUserRelHandle(c *fiber.Ctx) error {
// Create the model object
model := new(domain.UserRel)
if err := c.BodyParser(model); err != nil {
errorMessage := fmt.Sprintf("Parse UserRel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/parseModel", "Error happened while parsing model!"))
}
// Create service
userRelService, serviceErr := service.NewUserRelService(database.Db)
if serviceErr != nil {
log.Error("NewUserRelService %s", serviceErr.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/userRelService", "Error happened while creating userRelService!"))
}
if err := userRelService.UpdateUserRelById(model); err != nil {
errorMessage := fmt.Sprintf("Update UserRel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/updateUserRelById", "Error happened while updating UserRel!"))
}
return c.SendStatus(http.StatusOK)
}
// UpdateRelCirclesHandle handle create a new userRel
func UpdateRelCirclesHandle(c *fiber.Ctx) error {
// Create the model object
model := new(models.RelCirclesModel)
if err := c.BodyParser(model); err != nil {
errorMessage := fmt.Sprintf("Parse RelCirclesModel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/parseModel", "Error happened while parsing model!"))
}
// Create service
userRelService, serviceErr := service.NewUserRelService(database.Db)
if serviceErr != nil {
log.Error("NewUserRelService %s", serviceErr.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/userRelService", "Error happened while creating userRelService!"))
}
currentUser, ok := c.Locals(types.UserCtxName).(types.UserContext)
if !ok {
log.Error("[UpdateRelCirclesHandle] Can not get current user")
return c.Status(http.StatusBadRequest).JSON(utils.Error("invalidCurrentUser",
"Can not get current user"))
}
if err := userRelService.UpdateRelCircles(currentUser.UserID, model.RightId, model.CircleIds); err != nil {
errorMessage := fmt.Sprintf("Update UserRel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/updateRelCircles", "Error happened while updating UserRel!"))
}
return c.SendStatus(http.StatusOK)
}