Skip to content

Commit

Permalink
refactor(controller.client): simplify with body.Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 3, 2020
1 parent a025a1d commit 4735648
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions controller/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
func CreateShortUrl(res http.ResponseWriter, req *http.Request) {
shortCode, err := url.CreateUrlShortCodeFromRequest(req)
shortUrl := fmt.Sprintf("%s%s%s", "http://", req.Host, "/" + shortCode)
body := response.Body{"short_code": shortCode, "short_url": shortUrl}

if err == nil {
response.JSON(res, http.StatusOK, response.Body{"short_code": shortCode, "short_url": shortUrl})
response.JSON(res, http.StatusOK, body)
return
}

status, body := http.StatusUnprocessableEntity, response.Body{"message": err.Error()}
status, errBody := http.StatusUnprocessableEntity, response.Body{"message": err.Error()}
if shortCode != "" {
status = http.StatusConflict
body["short_code"] = shortCode
errBody.Merge(body)
}

response.JSON(res, status, body)
response.JSON(res, status, errBody)
}

0 comments on commit 4735648

Please sign in to comment.