feat(search): implement searching

This commit is contained in:
2025-04-06 21:56:01 -03:00
parent ee1a597c9e
commit dc5dd34835

View File

@@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/gabdlr/api-cuit-go/cache"
"github.com/gabdlr/api-cuit-go/cuit"
"github.com/gabdlr/api-cuit-go/rate_limit" "github.com/gabdlr/api-cuit-go/rate_limit"
) )
@@ -20,7 +22,6 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
errorResponse := &CuitError{Error: "Fallo exitosamente"} errorResponse := &CuitError{Error: "Fallo exitosamente"}
argument := r.URL.Path argument := r.URL.Path
//fmt.Printf("%v\n", cuit.ValidateWithVerifierDigit(cuit.StandardizeCuit(argument[1:])))
if len(argument) == 1 { if len(argument) == 1 {
errorResponse.Error = NO_SEARCH_ARG errorResponse.Error = NO_SEARCH_ARG
} else { } else {
@@ -30,6 +31,21 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) {
if timeLeft > 0 { if timeLeft > 0 {
errorResponse.Error = fmt.Sprintf("Recurso no disponible, debe esperar %v segundos", timeLeft) errorResponse.Error = fmt.Sprintf("Recurso no disponible, debe esperar %v segundos", timeLeft)
} }
if cuit.IsValid(argument) {
cRes, cErr := cache.Search(argument)
if cErr == nil {
w.Write(cRes)
return
}
res, err := cuit.Search(argument)
if err == nil {
cache.Save(argument, res)
w.Write(res)
return
} else {
errorResponse.Error = err.Error()
}
}
} }
jsonResponse, _ := json.Marshal(errorResponse) jsonResponse, _ := json.Marshal(errorResponse)