add custom js functions for templates
This commit is contained in:
97
examples/custom-js-functions/templates/controller.tmpl
Normal file
97
examples/custom-js-functions/templates/controller.tmpl
Normal file
@ -0,0 +1,97 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"your-app/models"
|
||||
)
|
||||
|
||||
{{formatComment (printf "Controller for %s entity\nGenerated with custom JavaScript template functions" .Entity.Name)}}
|
||||
type {{.Entity.Name | title}}Controller struct {
|
||||
// Add your dependencies here (e.g., database, services)
|
||||
}
|
||||
|
||||
{{formatComment "Create a new controller instance"}}
|
||||
func New{{.Entity.Name | title}}Controller() *{{.Entity.Name | title}}Controller {
|
||||
return &{{.Entity.Name | title}}Controller{}
|
||||
}
|
||||
|
||||
{{formatComment "GET /{{pluralize (.Entity.Name | lower)}}"}}
|
||||
func (c *{{.Entity.Name | title}}Controller) List{{pluralize (.Entity.Name | title)}}(w http.ResponseWriter, r *http.Request) {
|
||||
// Implementation for listing {{pluralize (.Entity.Name | lower)}}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Example response
|
||||
{{pluralize (.Entity.Name | lower)}} := []models.{{.Entity.Name | title}}{}
|
||||
json.NewEncoder(w).Encode({{pluralize (.Entity.Name | lower)}})
|
||||
}
|
||||
|
||||
{{formatComment "GET /{{pluralize (.Entity.Name | lower)}}/:id"}}
|
||||
func (c *{{.Entity.Name | title}}Controller) Get{{.Entity.Name | title}}(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
// Convert id and fetch {{.Entity.Name | lower}}
|
||||
_ = id // TODO: implement fetch logic
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// json.NewEncoder(w).Encode({{.Entity.Name | lower}})
|
||||
}
|
||||
|
||||
{{formatComment "POST /{{pluralize (.Entity.Name | lower)}}"}}
|
||||
func (c *{{.Entity.Name | title}}Controller) Create{{.Entity.Name | title}}(w http.ResponseWriter, r *http.Request) {
|
||||
var {{.Entity.Name | lower}} models.{{.Entity.Name | title}}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&{{.Entity.Name | lower}}); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := {{.Entity.Name | lower}}.Validate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Save to database
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode({{.Entity.Name | lower}})
|
||||
}
|
||||
|
||||
{{formatComment "PUT /{{pluralize (.Entity.Name | lower)}}/:id"}}
|
||||
func (c *{{.Entity.Name | title}}Controller) Update{{.Entity.Name | title}}(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
var {{.Entity.Name | lower}} models.{{.Entity.Name | title}}
|
||||
if err := json.NewDecoder(r.Body).Decode(&{{.Entity.Name | lower}}); err != nil {
|
||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := {{.Entity.Name | lower}}.Validate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Update in database using id
|
||||
_ = id
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode({{.Entity.Name | lower}})
|
||||
}
|
||||
|
||||
{{formatComment "DELETE /{{pluralize (.Entity.Name | lower)}}/:id"}}
|
||||
func (c *{{.Entity.Name | title}}Controller) Delete{{.Entity.Name | title}}(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
// TODO: Delete from database using id
|
||||
_ = id
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
Reference in New Issue
Block a user