package rest

import (
	"fmt"
	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
	"nbarest/lib/persistence"
	"net/http"
)

func ServeAPI(endpoint string,dbhandler persistence.DatabaseHandler) error{
	handler:=NewPlayerServiceHandler(dbhandler)
	r:=mux.NewRouter()
	playersrouter:=r.PathPrefix("/players").Subrouter()

	playersrouter.Path("/{SearchCriteria}/{search}").HandlerFunc(handler.FindPlayerHandler).Methods("GET")
	playersrouter.Path("/").HandlerFunc(handler.FindAllPlayerHandler).Methods("GET")
	playersrouter.Path("/").HandlerFunc(handler.NewPlayerHandler).Methods("POST")
	fmt.Println("listen")
	server := handlers.CORS()(r)
	return http.ListenAndServe(endpoint, server)
}

