Skip to content

Instantly share code, notes, and snippets.

@adigunhammedolalekan
Created May 3, 2018 15:02
Show Gist options
  • Save adigunhammedolalekan/263ff1f6e3d9ffaa53d5936f8df1466d to your computer and use it in GitHub Desktop.
Save adigunhammedolalekan/263ff1f6e3d9ffaa53d5936f8df1466d to your computer and use it in GitHub Desktop.
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