go-crm improvements

This commit is contained in:
2026-05-01 15:43:13 -03:00
parent ee6673265f
commit 31247a63f6
17 changed files with 416 additions and 63 deletions

View File

@@ -11,13 +11,18 @@ import (
)
func ListClients(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
if !ok {
return
}
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
if limit == 0 {
limit = 20
}
clients, err := db.ListClients(DB, limit, offset)
clients, err := db.ListClients(DB, accountID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -81,8 +86,14 @@ func ListClients(w http.ResponseWriter, r *http.Request) {
}
func CreateClient(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
if !ok {
return
}
r.ParseForm()
client := db.Client{
AccountID: accountID,
Name: r.FormValue("name"),
Phone: r.FormValue("phone"),
Email: r.FormValue("email"),
@@ -101,8 +112,13 @@ func CreateClient(w http.ResponseWriter, r *http.Request) {
}
func ViewClient(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
client, err := db.GetClientByID(DB, id)
client, err := db.GetClientByID(DB, accountID, id)
if err != nil {
http.Error(w, "Client not found", http.StatusNotFound)
return
@@ -124,10 +140,16 @@ func ViewClient(w http.ResponseWriter, r *http.Request) {
}
func UpdateClient(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
r.ParseForm()
client := db.Client{
ClientID: id,
ClientID: id,
AccountID: accountID,
Name: r.FormValue("name"),
Phone: r.FormValue("phone"),
Email: r.FormValue("email"),
@@ -144,8 +166,14 @@ func UpdateClient(w http.ResponseWriter, r *http.Request) {
}
func DeleteClient(w http.ResponseWriter, r *http.Request) {
accountID, ok := requireAuth(w, r)
if !ok {
return
}
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err := (&db.Client{}).Delete(DB, id); err != nil {
client := &db.Client{ClientID: id, AccountID: accountID}
if err := client.Delete(DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}