- Generated SQL queries for actors, films, categories. - Introduced HTTP handlers for actor, film, and category endpoints. - Included utility functions for parsing query parameters and building URLs. - Enabled pagination, filtering, and HAL representation for responses.
53 lines
867 B
Go
53 lines
867 B
Go
package httpapi
|
|
|
|
import (
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func parseIntDefault(s string, def int) int {
|
|
if s == "" {
|
|
return def
|
|
}
|
|
v, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return v
|
|
}
|
|
|
|
func parseExpand(s string) map[string]bool {
|
|
m := map[string]bool{}
|
|
if strings.TrimSpace(s) == "" {
|
|
return m
|
|
}
|
|
for _, p := range strings.Split(s, ",") {
|
|
p = strings.TrimSpace(p)
|
|
if p != "" {
|
|
m[p] = true
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
func buildURL(base string, limit, offset int, order, expand string) string {
|
|
v := url.Values{}
|
|
if limit > 0 {
|
|
v.Set("limit", strconv.Itoa(limit))
|
|
}
|
|
if offset > 0 {
|
|
v.Set("offset", strconv.Itoa(offset))
|
|
}
|
|
if strings.TrimSpace(order) != "" {
|
|
v.Set("order", order)
|
|
}
|
|
if strings.TrimSpace(expand) != "" {
|
|
v.Set("expand", expand)
|
|
}
|
|
qs := v.Encode()
|
|
if qs == "" {
|
|
return base
|
|
}
|
|
return base + "?" + qs
|
|
}
|