Skip to content

Instantly share code, notes, and snippets.

@adigunhammedolalekan
Created May 3, 2018 13:36
Show Gist options
  • Save adigunhammedolalekan/3de82c00311bd8bb05f13f95451166e4 to your computer and use it in GitHub Desktop.
Save adigunhammedolalekan/3de82c00311bd8bb05f13f95451166e4 to your computer and use it in GitHub Desktop.
package controllers
import (
"net/http"
u "go-contacts/utils"
"go-contacts/models"
"encoding/json"
)
var CreateAccount = func(w http.ResponseWriter, r *http.Request) {
account := &models.Account{}
err := json.NewDecoder(r.Body).Decode(account) //decode the request body into struct and failed if any error occur
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := account.Create() //Create account
u.Respond(w, resp)
}
var Authenticate = func(w http.ResponseWriter, r *http.Request) {
account := &models.Account{}
err := json.NewDecoder(r.Body).Decode(account) //decode the request body into struct and failed if any error occur
if err != nil {
u.Respond(w, u.Message(false, "Invalid request"))
return
}
resp := models.Login(account.Email, account.Password)
u.Respond(w, resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment