Skip to content

Commit

Permalink
Merge pull request #3 from AllanCapistrano/feat/message-by-message-id
Browse files Browse the repository at this point in the history
Feat/message by message
  • Loading branch information
AllanCapistrano authored Sep 28, 2023
2 parents 058a522 + 0729433 commit b17660c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
28 changes: 28 additions & 0 deletions endpoints/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ func GetAllMessagesByIndex(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer, jsonInString)
}

// Get a message by given message ID.
func GetMessageByMessageId(writer http.ResponseWriter, request *http.Request) {
var jsonInString string
nodeURL := config.GetNodeUrl(CONFIG_FILE_NAME, true)
nodePort := config.GetNodePort(CONFIG_FILE_NAME, true)
nodeAddress := fmt.Sprintf("http://%s:%s", nodeURL, nodePort)

vars := mux.Vars(request)
messageId := vars["messageID"]

message, err := messages.GetMessageFormattedByMessageID(nodeAddress, messageId)
if err != nil {
jsonInString = fmt.Sprintf("{\"error\": \"%s\"}", err.Error())
http.Error(writer, jsonInString, http.StatusInternalServerError)
} else {
jsonInBytes, err := json.Marshal(message)

if err != nil {
jsonInString = "{\"error\": \"Unable to convert the messages struct into JSON format.\"}"
http.Error(writer, jsonInString, http.StatusInternalServerError)
} else {
jsonInString = string(jsonInBytes) // TODO: Corrigir espaços em branco
}
}

fmt.Fprint(writer, jsonInString)
}

// Create and submit a new message.
func CreateNewMessage(writer http.ResponseWriter, request *http.Request) {
var message Message
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/allancapistrano/tangle-hornet-api
go 1.20

require (
github.com/allancapistrano/tangle-client-go v1.1.1
github.com/allancapistrano/tangle-client-go v1.2.0
github.com/gorilla/mux v1.8.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/Joker/jade v1.0.1-0.20190614124447-d475f43051e7/go.mod h1:6E6s8o2AE4K
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/allancapistrano/tangle-client-go v1.1.1 h1:zgj4qoiJvrJ7lGsXzmkA0xrZ9A3iImICwdcGM0KrgV0=
github.com/allancapistrano/tangle-client-go v1.1.1/go.mod h1:mDBHurufVjrhBL100yRzvpJEPNscjFlip6efNI3YQKM=
github.com/allancapistrano/tangle-client-go v1.2.0 h1:oiOZONDUF0wzhd4z4VMl+Q3tkttU5mRofmTtzYY8Eh8=
github.com/allancapistrano/tangle-client-go v1.2.0/go.mod h1:AwPSVwcfCVDDXdLQIM//9s9AHL7EOOn/LOxbk+0Sq80=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down
1 change: 1 addition & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func Routes() (router *mux.Router) {
// Messages
router.HandleFunc("/message", endpoints.CreateNewMessage).Methods("POST")
router.HandleFunc("/message/{index}", endpoints.GetAllMessagesByIndex)
router.HandleFunc("/message/messageId/{messageID}", endpoints.GetMessageByMessageId)

return router
}

0 comments on commit b17660c

Please sign in to comment.