59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"steam_analyzer/internal/repository"
|
|
"steam_analyzer/internal/utils/jsonutils"
|
|
"steam_analyzer/pkg/utils/httputils"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type ItemService interface {
|
|
GetAllItemsWithLastPrice(ctx context.Context, itemName string) ([]repository.ItemWithLastPrice, error)
|
|
// GetAllItemsWithLastPrice(itemName string) ([]*domain.SteamItem, error)
|
|
}
|
|
|
|
type SteamHandler struct {
|
|
itemService ItemService
|
|
}
|
|
|
|
func NewSteamHandler(itemService ItemService) SteamHandler {
|
|
return SteamHandler{
|
|
itemService: itemService,
|
|
}
|
|
}
|
|
|
|
func (h SteamHandler) RegisterRoutes(router *mux.Router, prefix string) {
|
|
steamRouter := router.PathPrefix(prefix)
|
|
|
|
getRouter := steamRouter.Methods(http.MethodGet).Subrouter()
|
|
|
|
getRouter.HandleFunc("/items", h.GetAllItemsWithLastPrice)
|
|
}
|
|
|
|
type GetAllItemsWithLastPriceResponse struct {
|
|
Result []repository.ItemWithLastPrice `json:"result"`
|
|
}
|
|
|
|
func (h SteamHandler) GetAllItemsWithLastPrice(w http.ResponseWriter, r *http.Request) {
|
|
name := r.URL.Query().Get("name")
|
|
|
|
if name == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
jsonutils.ToJSON(w, httputils.HTTPError{Message: "Name query param couldn't be empty"})
|
|
return
|
|
}
|
|
|
|
items, err := h.itemService.GetAllItemsWithLastPrice(r.Context(), name)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
jsonutils.ToJSON(w, GetAllItemsWithLastPriceResponse{Result: items})
|
|
}
|