dealroom/internal/handler/contacts.go

46 lines
1.2 KiB
Go

package handler
import (
"net/http"
"dealroom/internal/model"
"dealroom/templates"
)
func (h *Handler) handleContacts(w http.ResponseWriter, r *http.Request) {
profile := getProfile(r.Context())
dealID := r.URL.Query().Get("deal_id")
deals := h.getDeals(profile)
rows, err := h.db.Query("SELECT id, full_name, email, phone, company, title, contact_type, tags FROM contacts WHERE organization_id = ? ORDER BY full_name", profile.OrganizationID)
if err != nil {
http.Error(w, "Error loading contacts", 500)
return
}
defer rows.Close()
var contacts []*model.Contact
for rows.Next() {
c := &model.Contact{}
rows.Scan(&c.ID, &c.FullName, &c.Email, &c.Phone, &c.Company, &c.Title, &c.ContactType, &c.Tags)
contacts = append(contacts, c)
}
// Filter contacts by deal's target company if deal_id is set
if dealID != "" {
var targetCompany string
h.db.QueryRow("SELECT target_company FROM deals WHERE id = ?", dealID).Scan(&targetCompany)
if targetCompany != "" {
var filtered []*model.Contact
for _, c := range contacts {
if c.Company == targetCompany {
filtered = append(filtered, c)
}
}
contacts = filtered
}
}
templates.ContactsPage(profile, contacts, deals, dealID).Render(r.Context(), w)
}