( urlPath string, create createFunc[Ent], get getFunc[Ent], update updateFunc[Ent], deleteFn deleteFunc, list listFunc[Ent], options *RouteOptions, )
| 74 | ) |
| 75 | |
| 76 | func handleEntityWithPath[Ent any]( |
| 77 | urlPath string, |
| 78 | create createFunc[Ent], |
| 79 | get getFunc[Ent], |
| 80 | update updateFunc[Ent], |
| 81 | deleteFn deleteFunc, |
| 82 | list listFunc[Ent], |
| 83 | options *RouteOptions, |
| 84 | ) (path string, handler http.HandlerFunc) { |
| 85 | handler = func(w http.ResponseWriter, r *http.Request) { |
| 86 | switch r.Method { |
| 87 | case http.MethodPost: |
| 88 | handleCreate(r.Method, urlPath, create, options)(w, r) |
| 89 | case http.MethodGet: |
| 90 | _, _, _, _, accept, editMode, err := getIDAndEditMode(w, r, r.Method, urlPath, options) |
| 91 | if err != nil { |
| 92 | writeError(w, accept, err, options) |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | if urlPath == r.URL.Path && editMode == encoding.EditOff { |
| 97 | handleListAll(urlPath, r.Method, list, options)(w, r) |
| 98 | return |
| 99 | } |
| 100 | handleGet(urlPath, r.Method, get, options)(w, r) |
| 101 | case http.MethodPut: |
| 102 | updatePathID(urlPath, r.Method, update, get, options)(w, r) |
| 103 | case http.MethodDelete: |
| 104 | deletePathID(urlPath, r.Method, deleteFn, options)(w, r) |
| 105 | default: |
| 106 | badMethodHandler(w, r, options) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for i := len(options.middlewares) - 1; i >= 0; i-- { |
| 111 | // we wrap the handler in the middlewares |
| 112 | handler = options.middlewares[i](handler) |
| 113 | } |
| 114 | |
| 115 | return urlPath, handler |
| 116 | } |
| 117 | |
| 118 | func resID(requestPath, prefixPath string) string { |
| 119 | pathID := strings.TrimPrefix(requestPath, prefixPath) |
no test coverage detected