ui: add B2B olmayan stok (orphans) page
This commit is contained in:
@@ -107,11 +107,43 @@ func PostProductSeriesDefinitionHandler(pg *sql.DB) http.HandlerFunc {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
err := pg.QueryRowContext(ctx, `
|
||||
INSERT INTO dfgrp (code, title, is_active, typ, master, parent_filter, sort_order, is_required, notes)
|
||||
VALUES ($1, $2, COALESCE($3, TRUE), 'opt', 'zbggseri', $4, $5, FALSE, $6)
|
||||
RETURNING id
|
||||
WITH updated AS (
|
||||
UPDATE dfgrp
|
||||
SET title=$2,
|
||||
is_active=COALESCE($3, TRUE),
|
||||
parent_filter=$4,
|
||||
sort_order=$5,
|
||||
notes=$6
|
||||
WHERE master='zbggseri' AND code=$1
|
||||
RETURNING id
|
||||
),
|
||||
inserted AS (
|
||||
INSERT INTO dfgrp (code, title, is_active, typ, master, parent_filter, sort_order, is_required, notes)
|
||||
SELECT $1, $2, COALESCE($3, TRUE), 'opt', 'zbggseri', $4, $5, FALSE, $6
|
||||
WHERE NOT EXISTS (SELECT 1 FROM updated)
|
||||
RETURNING id
|
||||
)
|
||||
SELECT id FROM updated
|
||||
UNION ALL
|
||||
SELECT id FROM inserted
|
||||
LIMIT 1
|
||||
`, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes).Scan(&req.ID)
|
||||
if err != nil {
|
||||
if isPostgresUniqueViolation(err, "ndx_dfgrp_uq4") {
|
||||
if selErr := pg.QueryRowContext(ctx, `
|
||||
UPDATE dfgrp
|
||||
SET title=$2,
|
||||
is_active=COALESCE($3, TRUE),
|
||||
parent_filter=$4,
|
||||
sort_order=$5,
|
||||
notes=$6
|
||||
WHERE master='zbggseri' AND code=$1
|
||||
RETURNING id
|
||||
`, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes).Scan(&req.ID); selErr == nil {
|
||||
writeJSON(w, req)
|
||||
return
|
||||
}
|
||||
}
|
||||
http.Error(w, "Seri tanimi eklenemedi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -151,6 +183,10 @@ SET code=$2,
|
||||
WHERE id=$1 AND master='zbggseri'
|
||||
`, id, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes)
|
||||
if err != nil {
|
||||
if isPostgresUniqueViolation(err, "ndx_dfgrp_uq4") {
|
||||
http.Error(w, "Bu seri kodu zaten var. Mevcut seri satirini duzenleyin veya farkli kod kullanin.", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Seri tanimi guncellenemedi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -1155,3 +1191,14 @@ func writeJSON(w http.ResponseWriter, payload any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
func isPostgresUniqueViolation(err error, constraint string) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
pqErr, ok := err.(*pq.Error)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return string(pqErr.Code) == "23505" && (constraint == "" || pqErr.Constraint == constraint)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user