From 15972f8050662825833f085e60de82d8447b9aa9 Mon Sep 17 00:00:00 2001 From: ashk123 Date: Wed, 14 Feb 2024 19:26:14 +0330 Subject: [PATCH] add find message for GET requests --- internals/Base/Messages.go | 4 ++-- internals/Data/Response.go | 1 - internals/Routes/chat.go | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internals/Base/Messages.go b/internals/Base/Messages.go index b5ed1d5..a446200 100644 --- a/internals/Base/Messages.go +++ b/internals/Base/Messages.go @@ -68,7 +68,7 @@ func Itsr(data int) string { // } -func FindMsgByChannelID(CID string, MID string, flags Data.Flags) (*Data.Message, int) { +func FindMsgByChannelID(CID string, MID string, flags *Data.Flags) (*Data.Message, int) { message_rows := DB.QueryRow("SELECT * FROM Messages WHERE CID == " + CID + " AND MID == " + MID) var user_cid, user_mid, user_ReplyID int var user_Author, user_Content, user_Date string @@ -87,7 +87,7 @@ func FindMsgByChannelID(CID string, MID string, flags Data.Flags) (*Data.Message return msg_obj, 0 } -func FindMsgsByChannelID(ID string, flags Data.Flags) ([]*Data.Message, int) { +func FindMsgsByChannelID(ID string, flags *Data.Flags) ([]*Data.Message, int) { var message_rows *sql.Rows var res int if len(flags.SetRangeMessage) >= 1 { diff --git a/internals/Data/Response.go b/internals/Data/Response.go index c354ce7..7c9ee20 100644 --- a/internals/Data/Response.go +++ b/internals/Data/Response.go @@ -73,7 +73,6 @@ func GetErrorByResult(res_code int) *Error { // TODO: make a struct for return v return GenerateNewError("You Reached the Limit request time", http.StatusForbidden, "FAILD") // respponse Status error - Reached request limit case 19: return GenerateNewError("There is not any message with that ID", http.StatusNotFound, "FAILD") - default: return GenerateNewError("There is a problem", http.StatusNotAcceptable, "FAILD") // Response Status Error - uknown Eror } diff --git a/internals/Routes/chat.go b/internals/Routes/chat.go index 30be904..f5de04a 100644 --- a/internals/Routes/chat.go +++ b/internals/Routes/chat.go @@ -74,8 +74,9 @@ func StreamResponseJSON(c echo.Context, chat_data *Data.Response) error { // TODO: It's better to *Stream* the JSON message file func chatActionFunc(c echo.Context) error { // Handle the Chat Channel with c.Param("id") - flags := Data.Flags{SetRangeMessage: []string{}} // initial of flags + flags := &Data.Flags{SetRangeMessage: []string{}} // initial of flags channel_id := c.Param("id") + find_id := c.QueryParam("find") var chat_collection []*Data.Message get_message_range := c.QueryParam("range") var response *Data.Response @@ -86,7 +87,22 @@ func chatActionFunc(c echo.Context) error { error_obj := Data.GetErrorByResult(res_exists_channel) return c.JSON(error_obj.StatusCode, response) } - fmt.Println(res_exists_channel) + + // If user just wants to find a message + if find_id != "" { + // if the data is digit + chat_collection, res := Base.FindMsgByChannelID(channel_id, find_id, nil) + if res != 0 { + response, _ = Data.NewResponse(c, res, channel_id, nil) + error_obj2 := Data.GetErrorByResult(res) + return c.JSON(error_obj2.StatusCode, response) + } + response, _ = Data.NewResponse(c, res, channel_id, chat_collection) + return c.JSON(http.StatusOK, response) + } + + // Otherwise user wants to have the group of messages from a channel + // fmt.Println(res_exists_channel) if get_message_range != "" { // If user wants to have specific amount of data data_spl := strings.Split(get_message_range, "-") flags.SetRangeMessage = data_spl