Skip to content

Commit

Permalink
add find message for GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashk123 committed Feb 14, 2024
1 parent a7baaa6 commit 15972f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internals/Base/Messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internals/Data/Response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 18 additions & 2 deletions internals/Routes/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 15972f8

Please sign in to comment.