51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"maot-shortner/internal/database"
|
|
"maot-shortner/internal/services"
|
|
"net/http"
|
|
)
|
|
|
|
type Response struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type LinkHandler struct {
|
|
db *database.Database
|
|
linkService *services.LinkService
|
|
}
|
|
|
|
func (h *LinkHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(&Response{Message: "Ola"})
|
|
}
|
|
|
|
func NewLinkHandler(db *database.Database) *LinkHandler {
|
|
return &LinkHandler{
|
|
db: db,
|
|
linkService: services.NewLinkService(db),
|
|
}
|
|
}
|
|
|
|
func (h *LinkHandler) ListLinks(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
linkList, err := h.linkService.GetLinks(r.Context())
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to list links: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(linkList)
|
|
}
|
|
|
|
func (h *LinkHandler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
|
currentCode := r.PathValue("id")
|
|
|
|
json.NewEncoder(w).Encode(&Response{Message: currentCode})
|
|
}
|