implement api
This commit is contained in:
159
core/api/nkode_api_handler.go
Normal file
159
core/api/nkode_api_handler.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
CreateNewCustomer = "/create-new-customer"
|
||||
GenerateSignupInterface = "/generate-signup-interface"
|
||||
SetNKode = "/set-nkode"
|
||||
ConfirmNKode = "/confirm-nkode"
|
||||
GetLoginInterface = "/get-login-interface"
|
||||
Login = "/login"
|
||||
RenewAttributes = "/renew-attributes"
|
||||
)
|
||||
|
||||
type NKodeHandler struct {
|
||||
Api NKodeAPI
|
||||
}
|
||||
|
||||
type NewCustomerPost struct {
|
||||
KeypadSize models.KeypadSize `json:"keypad_size"`
|
||||
NKodePolicy models.NKodePolicy `json:"nkode_policy"`
|
||||
}
|
||||
|
||||
type GenerateSignupInterfacePost struct {
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
}
|
||||
|
||||
type SetNKodePost struct {
|
||||
Username string `json:"username"`
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
KeySelection []int `json:"key_selection"`
|
||||
SessionId uuid.UUID `json:"session_id"`
|
||||
}
|
||||
|
||||
type ConfirmNKodePost struct {
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
KeySelection []int `json:"key_selection"`
|
||||
SessionId uuid.UUID `json:"session_id"`
|
||||
}
|
||||
|
||||
type GetLoginInterfacePost struct {
|
||||
Username string `json:"username"`
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
}
|
||||
|
||||
type LoginPost struct {
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
Username string `json:"username"`
|
||||
KeySelection []int `json:"key_selection"`
|
||||
}
|
||||
|
||||
type RenewAttributesPost struct {
|
||||
CustomerId uuid.UUID `json:"customer_id"`
|
||||
}
|
||||
|
||||
func (h *NKodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case CreateNewCustomer:
|
||||
h.CreateNewCustomerHandler(w, r)
|
||||
return
|
||||
case GenerateSignupInterface:
|
||||
h.GenerateSignupInterfaceHandler(w, r)
|
||||
case SetNKode:
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
h.SetNKodeHandler(w, r)
|
||||
case ConfirmNKode:
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
case GetLoginInterface:
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
|
||||
case Login:
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
|
||||
case RenewAttributes:
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("404 not found"))
|
||||
}
|
||||
}
|
||||
|
||||
func internalServerErrorHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("500 Internal Server Error"))
|
||||
}
|
||||
|
||||
func methodNotAllowed(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
w.Write([]byte("405 method not allowed"))
|
||||
}
|
||||
|
||||
func (h *NKodeHandler) CreateNewCustomerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
var customerPost NewCustomerPost
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&customerPost); err != nil {
|
||||
internalServerErrorHandler(w, r)
|
||||
return
|
||||
}
|
||||
customerId, err := h.Api.CreateNewCustomer(customerPost.KeypadSize, customerPost.NKodePolicy)
|
||||
if err != nil {
|
||||
internalServerErrorHandler(w, r)
|
||||
return
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"customer_id": customerId,
|
||||
}
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonBytes)
|
||||
}
|
||||
|
||||
func (h *NKodeHandler) GenerateSignupInterfaceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
|
||||
var signupPost GenerateSignupInterfacePost
|
||||
if err := json.NewDecoder(r.Body).Decode(&signupPost); err != nil {
|
||||
internalServerErrorHandler(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Println("Customer Id: ", signupPost.CustomerId)
|
||||
}
|
||||
|
||||
func (h *NKodeHandler) SetNKodeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user