Skip to content

Commit

Permalink
docs: add response types
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielolivrp committed Oct 9, 2023
1 parent 7b32920 commit 0300fc9
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 124 deletions.
48 changes: 35 additions & 13 deletions controllers/check_phones_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ type phoneCheckBody struct {
Phones []string
}

type phone struct {
Query string
IsRegistered bool
JID struct {
AD bool
User string
Agent uint8
Device uint8
Server string
}
}

type checkPhonesResponse struct {
Phones []phone
}

type checkPhonesController struct {
wppService services.WppService
}
Expand All @@ -31,7 +47,7 @@ func NewCheckPhonesController(
// @Param data body phoneCheckBody true "Phone list"
// @Accept json
// @Produce json
// @Success 200 {array} string "List of verified numbers"
// @Success 200 {object} checkPhonesResponse "List of verified numbers"
// @Router /{instanceId}/check/phones [post]
func (p *checkPhonesController) Handler(c *gin.Context) {
var body phoneCheckBody
Expand All @@ -53,22 +69,28 @@ func (p *checkPhonesController) Handler(c *gin.Context) {
return
}

data := make([]gin.H, len(phones))
data := make([]phone, len(phones))
for i, p := range phones {
data[i] = gin.H{
"Query": p.Query,
"IsRegistered": p.IsIn,
"JID": gin.H{
"AD": p.JID.AD,
"User": p.JID.User,
"Agent": p.JID.Agent,
"Device": p.JID.Device,
"Server": p.JID.Server,
data[i] = phone{
Query: p.Query,
IsRegistered: p.IsIn,
JID: struct {
AD bool
User string
Agent uint8
Device uint8
Server string
}{
AD: p.JID.AD,
User: p.JID.User,
Agent: p.JID.Agent,
Device: p.JID.Device,
Server: p.JID.Server,
},
}
}

utils.RespondWithSuccess(c, gin.H{
"Phones": data,
utils.RespondWithSuccess(c, checkPhonesResponse{
Phones: data,
})
}
12 changes: 8 additions & 4 deletions controllers/get_contract_info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type getContactInfoController struct {
wppService services.WppService
}

type contactInfoResponse struct {
Info services.ContactInfo
}

func NewGetContactInfoController(
wppService services.WppService,
) *getContactInfoController {
Expand All @@ -27,7 +31,7 @@ func NewGetContactInfoController(
// @Param phone query string true "Phone"
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "Contact Information"
// @Success 200 {object} contactInfoResponse "Contact Information"
// @Router /{instanceId}/contact/info [get]
func (s *getContactInfoController) Handler(c *gin.Context) {
instanceID := c.Param("instanceId")
Expand All @@ -40,12 +44,12 @@ func (s *getContactInfoController) Handler(c *gin.Context) {
}

info, err := s.wppService.GetContactInfo(instanceID, jid)
if err != nil {
if err != nil || info == nil {
utils.RespondInternalServerError(c, err.Error())
return
}

utils.RespondWithSuccess(c, gin.H{
"Info": info,
utils.RespondWithSuccess(c, contactInfoResponse{
Info: *info,
})
}
12 changes: 8 additions & 4 deletions controllers/get_messages_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type getMessagesController struct {
messageService services.MessageService
}

type getMessagesResponse struct {
Messages []services.Message
}

func NewGetMessagesController(
wppService services.WppService,
messageService services.MessageService,
Expand All @@ -29,7 +33,7 @@ func NewGetMessagesController(
// @Param instanceId path string true "Instance ID"
// @Accept json
// @Produce json
// @Success 200 {array} string "List of chat messages"
// @Success 200 {object} getMessagesResponse "List of chat messages"
// @Router /{instanceId}/chat/messages [post]
func (m *getMessagesController) Handler(c *gin.Context) {
type Body struct {
Expand Down Expand Up @@ -58,12 +62,12 @@ func (m *getMessagesController) Handler(c *gin.Context) {
return
}

var data = []gin.H{}
var data []services.Message
for _, message := range *messages {
data = append(data, m.messageService.ToJSON(message))
}

utils.RespondWithSuccess(c, gin.H{
"Messages": data,
utils.RespondWithSuccess(c, getMessagesResponse{
Messages: data,
})
}
12 changes: 8 additions & 4 deletions controllers/get_profile_info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type getProfileInfoController struct {
wppService services.WppService
}

type getProfileInfoResponse struct {
Info services.ContactInfo
}

func NewGetProfileInfoController(
wppService services.WppService,
) *getProfileInfoController {
Expand All @@ -26,7 +30,7 @@ func NewGetProfileInfoController(
// @Param instanceId path string true "Instance ID"
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "Profile Information"
// @Success 200 {object} getProfileInfoResponse "Profile Information"
// @Router /{instanceId}/profile [get]
func (s *getProfileInfoController) Handler(c *gin.Context) {
instanceID := c.Param("instanceId")
Expand All @@ -44,12 +48,12 @@ func (s *getProfileInfoController) Handler(c *gin.Context) {
}

info, err := s.wppService.GetContactInfo(instanceID, jid)
if err != nil {
if err != nil || info == nil {
utils.RespondInternalServerError(c, err.Error())
return
}

utils.RespondWithSuccess(c, gin.H{
"Info": info,
utils.RespondWithSuccess(c, getProfileInfoResponse{
Info: *info,
})
}
13 changes: 9 additions & 4 deletions controllers/get_qrcode_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type getQrCodeController struct {
accountService services.AccountService
}

type getQrCodeResponse struct {
QrCode string
}

func NewGetQrCodeController(
wppService services.WppService,
messageService services.MessageService,
Expand All @@ -29,8 +33,9 @@ func NewGetQrCodeController(
// @Summary Get WhatsApp QR Code
// @Description Returns a QR code to initiate WhatsApp login.
// @Tags WhatsApp Login
// @Produce image/png
// @Success 200 {file} file "PNG image containing the QR code"
// @Param instanceId path string true "Instance ID"
// @Produce json
// @Success 200 {object} getQrCodeResponse "QR Code"
// @Router /{instanceId}/qrcode [get]
func (q *getQrCodeController) Handler(c *gin.Context) {
instanceID := c.Param("instanceId")
Expand All @@ -52,7 +57,7 @@ func (q *getQrCodeController) Handler(c *gin.Context) {
return
}

utils.RespondWithSuccess(c, gin.H{
"QrCode": account.QrCode,
utils.RespondWithSuccess(c, getQrCodeResponse{
QrCode: account.QrCode,
})
}
10 changes: 7 additions & 3 deletions controllers/get_status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type getStatusController struct {
accountService services.AccountService
}

type getStatusResponse struct {
Status string
}

func NewGetStatusController(
wppService services.WppService,
accountService services.AccountService,
Expand All @@ -29,7 +33,7 @@ func NewGetStatusController(
// @Param instanceId path string true "Instance ID"
// @Accept json
// @Produce json
// @Success 200 {string} string "Status Response"
// @Success 200 {object} getStatusResponse "Status Response"
// @Router /{instanceId}/status [get]
func (s *getStatusController) Handler(c *gin.Context) {
instanceID := c.Param("instanceId")
Expand Down Expand Up @@ -60,7 +64,7 @@ func (s *getStatusController) Handler(c *gin.Context) {
status = "UNPAIRED"
}

utils.RespondWithSuccess(c, gin.H{
"Status": status,
utils.RespondWithSuccess(c, getStatusResponse{
Status: status,
})
}
11 changes: 8 additions & 3 deletions controllers/send_audio_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ type audioMessageBody struct {
Phone string
Base64 string
}

type sendAudioMessageController struct {
wppService services.WppService
messageService services.MessageService
}

type sendAudioMessageResponse struct {
Message services.Message
}

func NewSendAudioMessageController(
wppService services.WppService,
messageService services.MessageService,
Expand All @@ -40,7 +45,7 @@ func NewSendAudioMessageController(
// @Param data body audioMessageBody true "Audio message body"
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "Message Send Response"
// @Success 200 {object} sendAudioMessageResponse "Message Send Response"
// @Router /{instanceId}/chat/send/audio [post]
func (a *sendAudioMessageController) Handler(c *gin.Context) {
var body audioMessageBody
Expand Down Expand Up @@ -131,7 +136,7 @@ func (a *sendAudioMessageController) Handler(c *gin.Context) {
return
}

utils.RespondWithSuccess(c, gin.H{
"Message": a.messageService.ToJSON(message),
utils.RespondWithSuccess(c, sendAudioMessageResponse{
Message: a.messageService.ToJSON(message),
})
}
10 changes: 7 additions & 3 deletions controllers/send_image_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type sendImageMessageController struct {
messageService services.MessageService
}

type sendImageMessageResponse struct {
Message services.Message
}

func NewSendImageMessageController(
wppService services.WppService,
messageService services.MessageService,
Expand All @@ -41,7 +45,7 @@ func NewSendImageMessageController(
// @Param data body imageMessageBody true "Image message body"
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "Message Send Response"
// @Success 200 {object} sendImageMessageResponse "Message Send Response"
// @Router /{instanceId}/chat/send/image [post]
func (i *sendImageMessageController) Handler(c *gin.Context) {
var body imageMessageBody
Expand Down Expand Up @@ -131,7 +135,7 @@ func (i *sendImageMessageController) Handler(c *gin.Context) {
return
}

utils.RespondWithSuccess(c, gin.H{
"Message": i.messageService.ToJSON(message),
utils.RespondWithSuccess(c, sendImageMessageResponse{
Message: i.messageService.ToJSON(message),
})
}
10 changes: 7 additions & 3 deletions controllers/sent_text_message_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type sendTextMessageController struct {
messageService services.MessageService
}

type sendTextMessageResponse struct {
Message services.Message
}

func NewSendTextMessageController(
wppService services.WppService,
messageService services.MessageService,
Expand All @@ -38,7 +42,7 @@ func NewSendTextMessageController(
// @Param data body textMessageBody true "Text message body"
// @Accept json
// @Produce json
// @Success 200 {object} string "Message Send Response"
// @Success 200 {object} sendTextMessageResponse "Message Send Response"
// @Router /{instanceId}/chat/send/text [post]
func (t *sendTextMessageController) Handler(c *gin.Context) {
var body textMessageBody
Expand Down Expand Up @@ -88,7 +92,7 @@ func (t *sendTextMessageController) Handler(c *gin.Context) {
return
}

utils.RespondWithSuccess(c, gin.H{
"Message": t.messageService.ToJSON(message),
utils.RespondWithSuccess(c, sendTextMessageResponse{
Message: t.messageService.ToJSON(message),
})
}
Loading

0 comments on commit 0300fc9

Please sign in to comment.