rest-api/handler/articles.go

44 lines
1.1 KiB
Go
Raw Normal View History

package handler
import (
"encoding/json"
"go-rest-api/model"
"log"
"net/http"
"github.com/gorilla/mux"
)
func getAllArticles(w http.ResponseWriter, r *http.Request) {
articles := model.Articles{
model.Article{Title: "Test Title", Description: "Test Description", Content: "Hello content!"},
}
json.NewEncoder(w).Encode(articles)
log.Println("Served /articles")
}
func getOneArticle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println(vars)
log.Println("Get single article")
}
func createArticle(w http.ResponseWriter, r *http.Request) {
log.Println("Create article")
}
func updateArticle(w http.ResponseWriter, r *http.Request) {
log.Println("Update article")
}
// AttachArticleHandlersToRouter func
func AttachArticleHandlersToRouter(root *mux.Router) {
router := root.PathPrefix("/articles").Subrouter()
router.HandleFunc("", getAllArticles).Methods(http.MethodGet)
router.HandleFunc("/{id:[0-9]+}", getOneArticle).Methods(http.MethodGet)
router.HandleFunc("", createArticle).Methods(http.MethodPost)
router.HandleFunc("/{id:[0-9]+}", updateArticle).Methods(http.MethodPut)
}