package routes import ( "encoding/json" "net/http" "github.com/CesarSSH/api-estudiantes/db" "github.com/CesarSSH/api-estudiantes/models" ) func GetCategoriasHandler(w http.ResponseWriter, r *http.Request) { var categorias []models.Categoria db.DB.Find(&categorias) json.NewEncoder(w).Encode(&categorias) } func PostCategoriaHandler(w http.ResponseWriter, r *http.Request) { var categoria models.Categoria json.NewDecoder(r.Body).Decode(&categoria) createdAtt := db.DB.Create(&categoria) err := createdAtt.Error if err != nil { w.WriteHeader(http.StatusBadRequest) //Error 400 w.Write([]byte(err.Error())) } json.NewEncoder(w).Encode(&categoria) }