171 lines
4.1 KiB
Go
171 lines
4.1 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 PurchaseOrderHandler struct {
|
|
queries *db.Queries
|
|
}
|
|
|
|
func NewPurchaseOrderHandler(queries *db.Queries) *PurchaseOrderHandler {
|
|
return &PurchaseOrderHandler{queries: queries}
|
|
}
|
|
|
|
func (h *PurchaseOrderHandler) 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
|
|
}
|
|
}
|
|
|
|
purchaseOrders, err := h.queries.ListPurchaseOrders(r.Context(), db.ListPurchaseOrdersParams{
|
|
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(purchaseOrders)
|
|
}
|
|
|
|
func (h *PurchaseOrderHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid purchase order ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
purchaseOrder, err := h.queries.GetPurchaseOrder(r.Context(), int32(id))
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
http.Error(w, "Purchase order not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(purchaseOrder)
|
|
}
|
|
|
|
func (h *PurchaseOrderHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
var params db.CreatePurchaseOrderParams
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
result, err := h.queries.CreatePurchaseOrder(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 *PurchaseOrderHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid purchase order ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var params db.UpdatePurchaseOrderParams
|
|
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.UpdatePurchaseOrder(r.Context(), params); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *PurchaseOrderHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid purchase order ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.queries.DeletePurchaseOrder(r.Context(), int32(id)); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *PurchaseOrderHandler) 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
|
|
}
|
|
}
|
|
|
|
purchaseOrders, err := h.queries.SearchPurchaseOrdersByTitle(r.Context(), db.SearchPurchaseOrdersByTitleParams{
|
|
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(purchaseOrders)
|
|
} |