initial commit
This commit is contained in:
parent
9baa0fab25
commit
fc46164556
14 changed files with 243 additions and 0 deletions
7
internal/handlers/handler.go
Normal file
7
internal/handlers/handler.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package handlers
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Handler interface {
|
||||
Handle(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
51
internal/handlers/link.go
Normal file
51
internal/handlers/link.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue