primer intento de servicio para acortar urls
This commit is contained in:
33
routes/url.go
Normal file
33
routes/url.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"url-short/dto"
|
||||
"url-short/repository"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
func Url(w http.ResponseWriter, r *http.Request, db *gorm.DB) {
|
||||
if r.Method == http.MethodPost {
|
||||
var reqBody dto.ReqBody
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&reqBody)
|
||||
if err != nil {
|
||||
http.Error(w, "Error: ", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
shortUrl, err := repository.CrearUrl(reqBody.LongUrl, db)
|
||||
if err != nil {
|
||||
http.Error(w, "Error: "+err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"shortUrl": shortUrl,
|
||||
})
|
||||
}else{
|
||||
http.Error(w, "404", http.StatusBadGateway);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user