171 lines
3.9 KiB
Go
171 lines
3.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"code.springupsoftware.com/cmc/cmc-sales/internal/cmc/db"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type ProductHandler struct {
|
|
queries *db.Queries
|
|
}
|
|
|
|
func NewProductHandler(queries *db.Queries) *ProductHandler {
|
|
return &ProductHandler{queries: queries}
|
|
}
|
|
|
|
func (h *ProductHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
limit := 50
|
|
offset := 0
|
|
|
|
if l := r.URL.Query().Get("limit"); l != "" {
|
|
if val, err := strconv.Atoi(l); err == nil {
|
|
limit = val
|
|
}
|
|
}
|
|
|
|
if o := r.URL.Query().Get("offset"); o != "" {
|
|
if val, err := strconv.Atoi(o); err == nil {
|
|
offset = val
|
|
}
|
|
}
|
|
|
|
products, err := h.queries.ListProducts(r.Context(), db.ListProductsParams{
|
|
Limit: int32(limit),
|
|
Offset: int32(offset),
|
|
})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(products)
|
|
}
|
|
|
|
func (h *ProductHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid product ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
product, err := h.queries.GetProduct(r.Context(), int32(id))
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
http.Error(w, "Product not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(product)
|
|
}
|
|
|
|
func (h *ProductHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
var params db.CreateProductParams
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
result, err := h.queries.CreateProduct(r.Context(), params)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"id": id,
|
|
})
|
|
}
|
|
|
|
func (h *ProductHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid product ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var params db.UpdateProductParams
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
params.ID = int32(id)
|
|
|
|
if err := h.queries.UpdateProduct(r.Context(), params); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *ProductHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid product ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.queries.DeleteProduct(r.Context(), int32(id)); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *ProductHandler) Search(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query().Get("q")
|
|
if query == "" {
|
|
http.Error(w, "Search query required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
limit := 50
|
|
offset := 0
|
|
|
|
if l := r.URL.Query().Get("limit"); l != "" {
|
|
if val, err := strconv.Atoi(l); err == nil {
|
|
limit = val
|
|
}
|
|
}
|
|
|
|
if o := r.URL.Query().Get("offset"); o != "" {
|
|
if val, err := strconv.Atoi(o); err == nil {
|
|
offset = val
|
|
}
|
|
}
|
|
|
|
products, err := h.queries.SearchProductsByTitle(r.Context(), db.SearchProductsByTitleParams{
|
|
CONCAT: query,
|
|
Limit: int32(limit),
|
|
Offset: int32(offset),
|
|
})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(products)
|
|
} |