Skip to content

Commit

Permalink
Delete message by ID
Browse files Browse the repository at this point in the history
- delete by only message id

Author: [email protected]
  • Loading branch information
thanhpp committed Jan 22, 2022
1 parent 1c7996f commit 6fca4f2
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 2 deletions.
57 changes: 57 additions & 0 deletions docs/Zola.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,63 @@
}
},
"response": []
},
{
"name": "delete conversation",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "DELETE",
"header": [],
"url": {
"raw": "{{auco}}/conversations/e92beb1c-6faa-4f04-b805-c968916711f0",
"host": [
"{{auco}}"
],
"path": [
"conversations",
"e92beb1c-6faa-4f04-b805-c968916711f0"
]
}
},
"response": []
},
{
"name": "delete message",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "DELETE",
"header": [],
"url": {
"raw": "{{auco}}/conversations/message/d83bd715-2985-4530-bbd8-22c3a846c656|1642789194219",
"host": [
"{{auco}}"
],
"path": [
"conversations",
"message",
"d83bd715-2985-4530-bbd8-22c3a846c656|1642789194219"
]
}
},
"response": []
}
]
},
Expand Down
6 changes: 4 additions & 2 deletions internal/auco/app/app_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app
import "errors"

var (
ErrBlocked = errors.New("blocked")
ErrInvalidUser = errors.New("invalid user")
ErrBlocked = errors.New("blocked")
ErrInvalidUser = errors.New("invalid user")
ErrInvalidMsgID = errors.New("invalid msgID")
ErrNotSender = errors.New("not sender")
)
35 changes: 35 additions & 0 deletions internal/auco/app/cvsthandler_deletemsg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package app

import (
"context"
"strconv"
"strings"

"github.com/thanhpp/zola/pkg/logger"
)

func (c ConversationHandler) DeleteMessage(ctx context.Context, requestorID, msgID string) error {
msgIDSlice := strings.Split(msgID, "|")
if len(msgIDSlice) != 2 {
logger.Errorf("ConversationHandler - invalid msgID len: %d", len(msgIDSlice))
return ErrInvalidMsgID
}

createdAtStr := msgIDSlice[1]
createdAtInt64, err := strconv.ParseInt(createdAtStr, 10, 64)
if err != nil {
logger.Errorf("ConversationHandler - invalid msgID createdAt: %v", err)
return ErrInvalidMsgID
}

msg, err := c.msgRepo.GetMessage(ctx, msgIDSlice[0], createdAtInt64)
if err != nil {
return err
}

if msg.SenderID != requestorID {
return ErrNotSender
}

return c.msgRepo.DeleteMessage(ctx, msgIDSlice[0], createdAtInt64)
}
3 changes: 3 additions & 0 deletions internal/auco/app/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var (
ErrRoomNotFound = errors.New("room not found")
ErrMsgNotFound = errors.New("msg not found")
)

type RoomRepository interface {
Expand All @@ -23,8 +24,10 @@ type MessageRepository interface {
// read
GetLastMessageByRoomID(ctx context.Context, roomID string) (*WsMessage, error)
GetMessagesByRoomID(ctx context.Context, roomID string, offset, limit int) ([]*WsMessage, error)
GetMessage(ctx context.Context, roomID string, createdAt int64) (*WsMessage, error)

// write
CreateMessage(msg *WsMessage) error
DeleteByRoomID(ctx context.Context, roomID string) error
DeleteMessage(ctx context.Context, roomID string, createdAt int64) error
}
5 changes: 5 additions & 0 deletions internal/auco/app/wsclient_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (c *WsClient) handleSend(msg *WsMessage) {
return
}

// check content
if len(msg.Content) == 0 {
return
}

// create a new message
newMsg, err := c.wsManager.fac.NewMessage(room.ID, msg.SenderID, msg.ReceiverID, msg.Created, msg.Content, false)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/auco/infra/adapter/gormdb/messagedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ func (g gormDB) CreateMessage(msg *app.WsMessage) error {
func (g gormDB) DeleteByRoomID(ctx context.Context, roomID string) error {
return g.db.Model(g.msgModel).WithContext(ctx).Where("room_id = ?", roomID).Delete(MessageDB{}).Error
}

func (g gormDB) DeleteMessage(ctx context.Context, roomID string, createdAt int64) error {
return g.db.Model(g.msgModel).WithContext(ctx).Where("room_id = ? AND created_at = ?", roomID, createdAt).Delete(MessageDB{}).Error
}
18 changes: 18 additions & 0 deletions internal/auco/infra/adapter/gormdb/messsagedb_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package gormdb

import (
"context"
"errors"

"github.com/thanhpp/zola/internal/auco/app"
"gorm.io/gorm"
)

func (g gormDB) GetLastMessageByRoomID(ctx context.Context, roomID string) (*app.WsMessage, error) {
Expand Down Expand Up @@ -40,3 +42,19 @@ func (g gormDB) GetMessagesByRoomID(ctx context.Context, roomID string, offset,

return listMsg, nil
}

func (g gormDB) GetMessage(ctx context.Context, roomID string, createdAt int64) (*app.WsMessage, error) {
var msgDB = new(MessageDB)

err := g.db.Model(g.msgModel).
Where("room_id = ? AND created_at = ?", roomID, createdAt).
Take(msgDB).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, app.ErrMsgNotFound
}
return nil, err
}

return g.unmarshalMessage(msgDB)
}
5 changes: 5 additions & 0 deletions internal/auco/infra/adapter/gormdb/roomdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package gormdb

import (
"context"
"errors"
"time"

"github.com/thanhpp/zola/internal/auco/app"
"github.com/thanhpp/zola/pkg/logger"
"gorm.io/gorm"
)

type RoomDB struct {
Expand Down Expand Up @@ -37,6 +39,9 @@ func (g gormDB) GetRoomByID(ctx context.Context, roomID string) (*app.WsRoom, er

err := g.db.Model(g.roomModel).WithContext(ctx).Where("id = ?", roomID).Take(roomDB).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, app.ErrRoomNotFound
}
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ func (ctrl ConversationController) DeleteByConversationID(c *gin.Context) {

c.JSON(http.StatusOK, resp)
}

func (ctrl ConversationController) DeleteMessage(c *gin.Context) {
requestorID, err := getRequestorIDFromClaims(c)
if err != nil {
logger.Errorf("CvsCtrl - get claims %v", err)
ginAbortUnauthorized(c, responsevalue.CodeInvalidateUser, "invalid user", nil)
return
}

messageID := c.Param("id")

if err := ctrl.conversationHandler.DeleteMessage(c, requestorID, messageID); err != nil {
logger.Errorf("CvsCtrl - delete message %v", err)
ginAbortInternalError(c, responsevalue.CodeUnknownError, responsevalue.MsgUnknownError, err.Error())
return
}

resp := new(dto.DefaultResp)
resp.SetCode(responsevalue.CodeOK)
resp.SetMsg(responsevalue.MsgOK)
c.JSON(http.StatusOK, resp)
}
1 change: 1 addition & 0 deletions internal/auco/infra/port/websocketserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (s *WebsocketServer) newRouter() *gin.Engine {
conversationGr.GET("/partner/:id", cCtrl.GetByPartnerID)
conversationGr.GET("/:id", cCtrl.GetByRoomID)
conversationGr.DELETE("/:id", cCtrl.DeleteByConversationID)
conversationGr.DELETE("/message/:id", cCtrl.DeleteMessage)
}

return router
Expand Down

0 comments on commit 6fca4f2

Please sign in to comment.