From a8ecbd9717a2be651867d6ba4cbafc6401d5d77d Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Tue, 24 Jul 2018 12:28:43 +0530 Subject: [PATCH] api: standardise marshal and unmarshal fn names --- api/comment_approve.go | 16 ++++++++-------- api/comment_delete.go | 16 ++++++++-------- api/comment_list.go | 12 ++++++------ api/comment_new.go | 14 +++++++------- api/comment_vote.go | 12 ++++++------ api/commenter_login.go | 8 ++++---- api/commenter_new.go | 8 ++++---- api/commenter_self.go | 8 ++++---- api/commenter_session_new.go | 4 ++-- api/domain_delete.go | 14 +++++++------- api/domain_import_disqus.go | 14 +++++++------- api/domain_list.go | 10 +++++----- api/domain_moderator_delete.go | 14 +++++++------- api/domain_moderator_new.go | 14 +++++++------- api/domain_new.go | 12 ++++++------ api/domain_statistics.go | 16 ++++++++-------- api/domain_update.go | 14 +++++++------- api/owner_login.go | 8 ++++---- api/owner_new.go | 10 +++++----- api/owner_reset_hex.go | 8 ++++---- api/owner_reset_password.go | 8 ++++---- api/owner_self.go | 10 +++++----- api/utils_http.go | 4 ++-- 23 files changed, 127 insertions(+), 127 deletions(-) diff --git a/api/comment_approve.go b/api/comment_approve.go index 1af5960..19428e4 100644 --- a/api/comment_approve.go +++ b/api/comment_approve.go @@ -31,38 +31,38 @@ func commentApproveHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } c, err := commenterGetByCommenterToken(*x.CommenterToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain, err := commentDomainGet(*x.CommentHex) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } isModerator, err := isDomainModerator(domain, c.Email) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isModerator { - writeBody(w, response{"success": false, "message": errorNotModerator.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()}) return } if err = commentApprove(*x.CommentHex); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/comment_delete.go b/api/comment_delete.go index a1a9189..f9afdff 100644 --- a/api/comment_delete.go +++ b/api/comment_delete.go @@ -30,38 +30,38 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } c, err := commenterGetByCommenterToken(*x.CommenterToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain, err := commentDomainGet(*x.CommentHex) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } isModerator, err := isDomainModerator(domain, c.Email) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isModerator { - writeBody(w, response{"success": false, "message": errorNotModerator.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()}) return } if err = commentDelete(*x.CommentHex); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/comment_list.go b/api/comment_list.go index aae3940..ab0c21d 100644 --- a/api/comment_list.go +++ b/api/comment_list.go @@ -97,8 +97,8 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -107,7 +107,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) { d, err := domainGet(domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -119,7 +119,7 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) { if err == errorNoSuchToken { commenterHex = "anonymous" } else { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } } else { @@ -138,11 +138,11 @@ func commentListHandler(w http.ResponseWriter, r *http.Request) { comments, commenters, err := commentList(commenterHex, domain, path, isModerator) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{ + bodyMarshal(w, response{ "success": true, "domain": domain, "comments": comments, diff --git a/api/comment_new.go b/api/comment_new.go index c240d94..dd14607 100644 --- a/api/comment_new.go +++ b/api/comment_new.go @@ -44,8 +44,8 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -54,12 +54,12 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) { d, err := domainGet(domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if d.State == "frozen" { - writeBody(w, response{"success": false, "message": errorDomainFrozen.Error()}) + bodyMarshal(w, response{"success": false, "message": errorDomainFrozen.Error()}) return } @@ -80,7 +80,7 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) { } else { c, err := commenterGetByCommenterToken(*x.CommenterToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -108,9 +108,9 @@ func commentNewHandler(w http.ResponseWriter, r *http.Request) { commentHex, err := commentNew(commenterHex, domain, path, *x.ParentHex, *x.Markdown, state, time.Now().UTC()) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"}) + bodyMarshal(w, response{"success": true, "commentHex": commentHex, "approved": state == "approved"}) } diff --git a/api/comment_vote.go b/api/comment_vote.go index 356ed77..d082cdd 100644 --- a/api/comment_vote.go +++ b/api/comment_vote.go @@ -51,19 +51,19 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if *x.CommenterToken == "anonymous" { - writeBody(w, response{"success": false, "message": errorUnauthorisedVote.Error()}) + bodyMarshal(w, response{"success": false, "message": errorUnauthorisedVote.Error()}) return } c, err := commenterGetByCommenterToken(*x.CommenterToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -75,9 +75,9 @@ func commentVoteHandler(w http.ResponseWriter, r *http.Request) { } if err := commentVote(c.CommenterHex, *x.CommentHex, direction); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/commenter_login.go b/api/commenter_login.go index c9996d5..c127eec 100644 --- a/api/commenter_login.go +++ b/api/commenter_login.go @@ -56,16 +56,16 @@ func commenterLoginHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } commenterToken, err := commenterLogin(*x.Email, *x.Password) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "commenterToken": commenterToken}) + bodyMarshal(w, response{"success": true, "commenterToken": commenterToken}) } diff --git a/api/commenter_new.go b/api/commenter_new.go index 7bce2e7..d3d1045 100644 --- a/api/commenter_new.go +++ b/api/commenter_new.go @@ -63,8 +63,8 @@ func commenterNewHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } @@ -72,9 +72,9 @@ func commenterNewHandler(w http.ResponseWriter, r *http.Request) { // TODO: email confirmation if provider = commento? // TODO: email confirmation if provider = commento? if _, err := commenterNew(*x.Email, *x.Name, *x.Website, "undefined", "commento", *x.Password); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "confirmEmail": smtpConfigured}) + bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured}) } diff --git a/api/commenter_self.go b/api/commenter_self.go index 632b025..23a0d7a 100644 --- a/api/commenter_self.go +++ b/api/commenter_self.go @@ -10,16 +10,16 @@ func commenterSelfHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } c, err := commenterGetByCommenterToken(*x.CommenterToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "commenter": c}) + bodyMarshal(w, response{"success": true, "commenter": c}) } diff --git a/api/commenter_session_new.go b/api/commenter_session_new.go index 59f4a5e..d65c133 100644 --- a/api/commenter_session_new.go +++ b/api/commenter_session_new.go @@ -29,9 +29,9 @@ func commenterTokenNew() (string, error) { func commenterTokenNewHandler(w http.ResponseWriter, r *http.Request) { commenterToken, err := commenterTokenNew() if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "commenterToken": commenterToken}) + bodyMarshal(w, response{"success": true, "commenterToken": commenterToken}) } diff --git a/api/domain_delete.go b/api/domain_delete.go index 83ff8db..998ac0f 100644 --- a/api/domain_delete.go +++ b/api/domain_delete.go @@ -70,33 +70,33 @@ func domainDeleteHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isOwner { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } if err = domainDelete(*x.Domain); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/domain_import_disqus.go b/api/domain_import_disqus.go index 41975ee..1116414 100644 --- a/api/domain_import_disqus.go +++ b/api/domain_import_disqus.go @@ -174,34 +174,34 @@ func domainImportDisqusHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isOwner { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } numImported, err := domainImportDisqus(domain, *x.URL) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "numImported": numImported}) + bodyMarshal(w, response{"success": true, "numImported": numImported}) } diff --git a/api/domain_list.go b/api/domain_list.go index fd4dd46..8699381 100644 --- a/api/domain_list.go +++ b/api/domain_list.go @@ -46,22 +46,22 @@ func domainListHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domains, err := domainList(o.OwnerHex) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "domains": domains}) + bodyMarshal(w, response{"success": true, "domains": domains}) } diff --git a/api/domain_moderator_delete.go b/api/domain_moderator_delete.go index ce46635..5c4dd1a 100644 --- a/api/domain_moderator_delete.go +++ b/api/domain_moderator_delete.go @@ -30,33 +30,33 @@ func domainModeratorDeleteHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) authorised, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !authorised { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } if err = domainModeratorDelete(domain, *x.Email); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/domain_moderator_new.go b/api/domain_moderator_new.go index 40dc2d2..b226e00 100644 --- a/api/domain_moderator_new.go +++ b/api/domain_moderator_new.go @@ -32,33 +32,33 @@ func domainModeratorNewHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isOwner { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } if err = domainModeratorNew(domain, *x.Email); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/domain_new.go b/api/domain_new.go index 05a8638..43f923a 100644 --- a/api/domain_new.go +++ b/api/domain_new.go @@ -32,28 +32,28 @@ func domainNewHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) if err = domainNew(o.OwnerHex, *x.Name, domain); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if err = domainModeratorNew(domain, o.Email); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "domain": domain}) + bodyMarshal(w, response{"success": true, "domain": domain}) } diff --git a/api/domain_statistics.go b/api/domain_statistics.go index 334bb50..979ab26 100644 --- a/api/domain_statistics.go +++ b/api/domain_statistics.go @@ -44,40 +44,40 @@ func domainStatisticsHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain(*x.Domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isOwner { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } viewsLast30Days, err := domainStatistics(domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } commentsLast30Days, err := commentStatistics(domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days}) + bodyMarshal(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days}) } diff --git a/api/domain_update.go b/api/domain_update.go index 7490875..630be7b 100644 --- a/api/domain_update.go +++ b/api/domain_update.go @@ -27,33 +27,33 @@ func domainUpdateHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } domain := stripDomain((*x.D).Domain) isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if !isOwner { - writeBody(w, response{"success": false, "message": errorNotAuthorised.Error()}) + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) return } if err = domainUpdate(*x.D); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/owner_login.go b/api/owner_login.go index 923907a..54f7ed5 100644 --- a/api/owner_login.go +++ b/api/owner_login.go @@ -61,16 +61,16 @@ func ownerLoginHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } ownerToken, err := ownerLogin(*x.Email, *x.Password) if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "ownerToken": ownerToken}) + bodyMarshal(w, response{"success": true, "ownerToken": ownerToken}) } diff --git a/api/owner_new.go b/api/owner_new.go index d8e7279..cfdaf55 100644 --- a/api/owner_new.go +++ b/api/owner_new.go @@ -69,20 +69,20 @@ func ownerNewHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if _, err := ownerNew(*x.Email, *x.Name, *x.Password); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if _, err := commenterNew(*x.Email, *x.Name, "undefined", "undefined", "commento", *x.Password); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "confirmEmail": smtpConfigured}) + bodyMarshal(w, response{"success": true, "confirmEmail": smtpConfigured}) } diff --git a/api/owner_reset_hex.go b/api/owner_reset_hex.go index ebe4add..62b5506 100644 --- a/api/owner_reset_hex.go +++ b/api/owner_reset_hex.go @@ -56,15 +56,15 @@ func ownerSendResetHexHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if err := ownerSendResetHex(*x.Email); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/owner_reset_password.go b/api/owner_reset_password.go index 7eaf92c..c97284b 100644 --- a/api/owner_reset_password.go +++ b/api/owner_reset_password.go @@ -58,15 +58,15 @@ func ownerResetPasswordHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } if err := ownerResetPassword(*x.ResetHex, *x.Password); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true}) + bodyMarshal(w, response{"success": true}) } diff --git a/api/owner_self.go b/api/owner_self.go index 58cf5fe..3dafa14 100644 --- a/api/owner_self.go +++ b/api/owner_self.go @@ -10,21 +10,21 @@ func ownerSelfHandler(w http.ResponseWriter, r *http.Request) { } var x request - if err := unmarshalBody(r, &x); err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } o, err := ownerGetByOwnerToken(*x.OwnerToken) if err == errorNoSuchToken { - writeBody(w, response{"success": true, "loggedIn": false}) + bodyMarshal(w, response{"success": true, "loggedIn": false}) return } if err != nil { - writeBody(w, response{"success": false, "message": err.Error()}) + bodyMarshal(w, response{"success": false, "message": err.Error()}) return } - writeBody(w, response{"success": true, "loggedIn": true, "owner": o}) + bodyMarshal(w, response{"success": true, "loggedIn": true, "owner": o}) } diff --git a/api/utils_http.go b/api/utils_http.go index 3b1c084..8e50158 100644 --- a/api/utils_http.go +++ b/api/utils_http.go @@ -11,7 +11,7 @@ type response map[string]interface{} // TODO: Add tests in utils_http_test.go -func unmarshalBody(r *http.Request, x interface{}) error { +func bodyUnmarshal(r *http.Request, x interface{}) error { b, err := ioutil.ReadAll(r.Body) if err != nil { logger.Errorf("cannot read POST body: %v\n", err) @@ -32,7 +32,7 @@ func unmarshalBody(r *http.Request, x interface{}) error { return nil } -func writeBody(w http.ResponseWriter, x map[string]interface{}) error { +func bodyMarshal(w http.ResponseWriter, x map[string]interface{}) error { resp, err := json.Marshal(x) if err != nil { w.Write([]byte(`{"success":false,"message":"Some internal error occurred"}`))