Created
May 3, 2018 15:02
-
-
Save adigunhammedolalekan/263ff1f6e3d9ffaa53d5936f8df1466d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import ( | |
"net/http" | |
"go-contacts/models" | |
"encoding/json" | |
u "go-contacts/utils" | |
"strconv" | |
"github.com/gorilla/mux" | |
"fmt" | |
) | |
var CreateContact = func(w http.ResponseWriter, r *http.Request) { | |
user := r.Context().Value("user") . (uint) //Grab the id of the user that send the request | |
contact := &models.Contact{} | |
err := json.NewDecoder(r.Body).Decode(contact) | |
if err != nil { | |
u.Respond(w, u.Message(false, "Error while decoding request body")) | |
return | |
} | |
contact.UserId = user | |
resp := contact.Create() | |
u.Respond(w, resp) | |
} | |
var GetContactsFor = func(w http.ResponseWriter, r *http.Request) { | |
params := mux.Vars(r) | |
id, err := strconv.Atoi(params["id"]) | |
if err != nil { | |
//The passed path parameter is not an integer | |
u.Respond(w, u.Message(false, "There was an error in your request")) | |
return | |
} | |
data := models.GetContacts(uint(id)) | |
resp := u.Message(true, "success") | |
resp["data"] = data | |
u.Respond(w, resp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment