categoria.route.go 670 B

12345678910111213141516171819202122232425262728
  1. package routes
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/CesarSSH/api-estudiantes/db"
  6. "github.com/CesarSSH/api-estudiantes/models"
  7. )
  8. func GetCategoriasHandler(w http.ResponseWriter, r *http.Request) {
  9. var categorias []models.Categoria
  10. db.DB.Find(&categorias)
  11. json.NewEncoder(w).Encode(&categorias)
  12. }
  13. func PostCategoriaHandler(w http.ResponseWriter, r *http.Request) {
  14. var categoria models.Categoria
  15. json.NewDecoder(r.Body).Decode(&categoria)
  16. createdAtt := db.DB.Create(&categoria)
  17. err := createdAtt.Error
  18. if err != nil {
  19. w.WriteHeader(http.StatusBadRequest) //Error 400
  20. w.Write([]byte(err.Error()))
  21. }
  22. json.NewEncoder(w).Encode(&categoria)
  23. }