forked from red-gold/telar-social-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
131 lines (107 loc) · 4.76 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
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
package handlers
import (
"fmt"
"net/http"
"github.com/GMcD/ts-serverless/micros/vang/database"
domain "github.com/GMcD/ts-serverless/micros/vang/dto"
models "github.com/GMcD/ts-serverless/micros/vang/models"
service "github.com/GMcD/ts-serverless/micros/vang/services"
"github.com/gofiber/fiber/v2"
"github.com/gofrs/uuid"
"github.com/red-gold/telar-core/pkg/log"
"github.com/red-gold/telar-core/types"
"github.com/red-gold/telar-core/utils"
)
// UpdateMessageHandle handle create a new vang
func UpdateMessageHandle(c *fiber.Ctx) error {
// Create the model object
model := new(models.MessageModel)
if err := c.BodyParser(model); err != nil {
errorMessage := fmt.Sprintf("Parse MessageModel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/parseModel", "Error happened while parsing model!"))
}
// Create service
messageService, serviceErr := service.NewMessageService(database.Db)
if serviceErr != nil {
log.Error("NewMessageService %s", serviceErr.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/messageService", "Error happened while creating messageService!"))
}
currentUser, ok := c.Locals(types.UserCtxName).(types.UserContext)
if !ok {
log.Error("[UpdateMessageHandle] Can not get current user")
return c.Status(http.StatusBadRequest).JSON(utils.Error("invalidCurrentUser",
"Can not get current user"))
}
updatedMessage := &domain.Message{
ObjectId: model.ObjectId,
OwnerUserId: currentUser.UserID,
RoomId: model.RoomId,
Text: model.Text,
CreatedDate: model.CreatedDate,
UpdatedDate: model.UpdatedDate,
}
if err := messageService.UpdateMessageById(updatedMessage); err != nil {
errorMessage := fmt.Sprintf("Update Message Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/updateMessage", "Error happened while updating message!"))
}
return c.SendStatus(http.StatusOK)
}
// UpdateMessageHandle handle create a new vang
func UpdateReadMessageHandle(c *fiber.Ctx) error {
// Create the model object
model := new(models.UpdateReadMessageModel)
if err := c.BodyParser(model); err != nil {
errorMessage := fmt.Sprintf("Parse UpdateReadMessageModel Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/parseModel", "Error happened while parsing model!"))
}
// Create service
roomService, serviceErr := service.NewRoomService(database.Db)
if serviceErr != nil {
log.Error("NewRoomService %s", serviceErr.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/roomService", "Error happened while creating roomService!"))
}
currentUser, ok := c.Locals(types.UserCtxName).(types.UserContext)
if !ok {
log.Error("[UpdateReadMessageHandle] Can not get current user")
return c.Status(http.StatusBadRequest).JSON(utils.Error("invalidCurrentUser",
"Can not get current user"))
}
if err := roomService.UpdateMemberRead(model.RoomId, currentUser.UserID, model.Amount, model.MessageCreatedDate); err != nil {
errorMessage := fmt.Sprintf("Update Message Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/updateMessage", "Error happened while updating message!"))
}
return c.SendStatus(http.StatusOK)
}
// DeactiveUserRoomHandle handl deactive room for a user
func DeactiveUserRoomHandle(c *fiber.Ctx) error {
roomId := c.Params("roomId")
if roomId == "" {
return c.Status(http.StatusBadRequest).JSON(utils.Error("roomIdIsRequired",
"Room ID is required!"))
}
roomUUID, uuidErr := uuid.FromString(roomId)
if uuidErr != nil {
errorMessage := fmt.Sprintf("Parse room UUID Error %s", uuidErr.Error())
log.Error(errorMessage)
return c.Status(http.StatusBadRequest).JSON(utils.Error("invalidRoomId", "Invalid roomId!"))
}
roomService, serviceErr := service.NewRoomService(database.Db)
if serviceErr != nil {
log.Error("NewRoomService %s", serviceErr.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/roomService", "Error happened while creating roomService!"))
}
currentUser, ok := c.Locals(types.UserCtxName).(types.UserContext)
if !ok {
log.Error("[DeactiveUserRoomHandle] Can not get current user")
return c.Status(http.StatusBadRequest).JSON(utils.Error("invalidCurrentUser", "Can not get current user!"))
}
if err := roomService.DeactiveUserRoom(roomUUID, currentUser.UserID); err != nil {
log.Error("[DeactiveUserRoomHandle] %s", err.Error())
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/deactiveUserRoom", "Error happened while deactivating user room!"))
}
return c.SendStatus(http.StatusOK)
}