mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
wip: client api with net/http poc
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package api
|
||||
|
||||
import "github.com/hazemKrimi/crimson-vault/internal/models"
|
||||
|
||||
type APIWrapper struct {
|
||||
dbWrapper *models.DBWrapper
|
||||
}
|
||||
|
||||
func (api *APIWrapper) Initialize() {
|
||||
wrapper := models.DBWrapper{}
|
||||
|
||||
wrapper.Connect()
|
||||
wrapper.MigrateClients()
|
||||
|
||||
api.dbWrapper = &wrapper;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/models"
|
||||
)
|
||||
|
||||
func (api *APIWrapper) CreateClientHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
var body models.CreateClientBody
|
||||
|
||||
if err := json.NewDecoder(request.Body).Decode(&body); err != nil {
|
||||
http.Error(writer, "Invalid JSON", http.StatusBadRequest)
|
||||
log.Println(fmt.Sprintf("Error creating Client: %v.", err))
|
||||
return
|
||||
}
|
||||
|
||||
client := api.dbWrapper.CreateClient(body)
|
||||
|
||||
log.Println(fmt.Sprintf("Client created with ID %d.", client.ID))
|
||||
json.NewEncoder(writer).Encode(client)
|
||||
}
|
||||
|
||||
func (api *APIWrapper) GetClientsHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
idString := request.URL.Query().Get("id")
|
||||
|
||||
if idString != "" {
|
||||
id, err := strconv.Atoi(idString)
|
||||
|
||||
if err != nil {
|
||||
http.Error(writer, "Unexpected error getting Client.", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var client models.Client
|
||||
|
||||
if err := api.dbWrapper.GetClient(id, &client); err != nil {
|
||||
http.Error(writer, "Client not found.", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(fmt.Sprintf("Got client with ID %d.", client.ID))
|
||||
json.NewEncoder(writer).Encode(client)
|
||||
return
|
||||
}
|
||||
|
||||
clients, err := api.dbWrapper.GetClients()
|
||||
|
||||
if err != nil {
|
||||
http.Error(writer, "Unexpected error getting Clients.", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Got all Clients.")
|
||||
json.NewEncoder(writer).Encode(clients)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
func ClientRoutes(api *APIWrapper) (*http.ServeMux) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /", api.GetClientsHandler)
|
||||
mux.HandleFunc("POST /", api.CreateClientHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
Reference in New Issue
Block a user