339 lines
13 KiB
Go
339 lines
13 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"go-crm/internal/db"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func ListQuestions(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"))
|
|
clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64)
|
|
if limit == 0 {
|
|
limit = 20
|
|
}
|
|
|
|
questions, err := db.ListQuestions(DB, clientID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
clients, err := db.ListClients(DB, accountID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
customers, err := db.ListCustomers(DB, accountID, clientID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var clientOptions, customerOptions string
|
|
clientNames := make(map[int64]string)
|
|
customerNames := make(map[int64]string)
|
|
for _, c := range clients {
|
|
clientOptions += `<option value="` + strconv.FormatInt(c.ClientID, 10) + `">` + c.Name + `</option>`
|
|
clientNames[c.ClientID] = c.Name
|
|
}
|
|
for _, cu := range customers {
|
|
customerOptions += `<option value="` + strconv.FormatInt(cu.CustomerID, 10) + `">` + cu.Name + `</option>`
|
|
customerNames[cu.CustomerID] = cu.Name
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><html><head><script src="https://unpkg.com/htmx.org@1.9.10"></script><style>.edit-row{display:none}</style></head><body><h1>Questions</h1><button class="btn" onclick="document.getElementById('questionForm').style.display='block'">Add Question</button><div id="questionForm" style="display:none; margin-top:1rem;"><form hx-post="/questions" hx-target="#questionList"><select name="client_id" required><option value="">Select Client</option>` + clientOptions + `</select><select name="customer_id" required><option value="">Select Customer</option>` + customerOptions + `</select><textarea name="question" placeholder="Question" required></textarea><select name="status"><option value="pending">Pending</option><option value="answered">Answered</option></select><button type="submit">Add</button></form></div><table><thead><tr><th>Client</th><th>Customer</th><th>Question</th><th>Status</th><th>Actions</th></tr></thead><tbody id="questionList">`))
|
|
for _, q := range questions {
|
|
clientName := clientNames[q.ClientID]
|
|
if clientName == "" {
|
|
clientName = strconv.FormatInt(q.ClientID, 10)
|
|
}
|
|
customerName := customerNames[q.CustomerID]
|
|
if customerName == "" {
|
|
customerName = strconv.FormatInt(q.CustomerID, 10)
|
|
}
|
|
w.Write([]byte(`<tr><td>` + clientName + `</td><td>` + customerName + `</td><td>` + q.Question + `</td><td>` + q.Status + `</td><td><a href="/questions/` + strconv.FormatInt(q.QuestionID, 10) + `">View</a><button type="button" onclick="document.getElementById('editQ` + strconv.FormatInt(q.QuestionID, 10) + `').style.display='table-row'">Edit</button><form method="DELETE" style="display:inline" hx-delete="/questions/` + strconv.FormatInt(q.QuestionID, 10) + `" hx-target="closest tr"><button type="submit">Delete</button></form></td></tr><tr id="editQ` + strconv.FormatInt(q.QuestionID, 10) + `" style="display:none"><td colspan="5"><form hx-put="/questions/` + strconv.FormatInt(q.QuestionID, 10) + `" hx-target="#questionList" hx-swap="innerHTML"><select name="client_id"><option value="` + strconv.FormatInt(q.ClientID, 10) + `">` + clientName + `</option>` + clientOptions + `</select><select name="customer_id"><option value="` + strconv.FormatInt(q.CustomerID, 10) + `">` + customerName + `</option>` + customerOptions + `</select><textarea name="question">` + q.Question + `</textarea><select name="status"><option value="` + q.Status + `">` + q.Status + `</option><option value="pending">Pending</option><option value="answered">Answered</option></select><button type="submit">Save</button></form></td></tr>`))
|
|
}
|
|
w.Write([]byte(`</tbody></table></body></html>`))
|
|
}
|
|
|
|
func CreateQuestion(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
|
|
question := db.Question{
|
|
ClientID: clientID,
|
|
CustomerID: customerID,
|
|
Question: r.FormValue("question"),
|
|
Timestamp: time.Now().Unix(),
|
|
Status: r.FormValue("status"),
|
|
CreatedAt: time.Now().Unix(),
|
|
}
|
|
|
|
if err := question.Create(DB); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func ViewQuestion(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
var q db.Question
|
|
err := DB.QueryRow("SELECT question_id, client_id, customer_id, question, timestamp, status, created_at FROM questions WHERE question_id = ?", id).Scan(&q.QuestionID, &q.ClientID, &q.CustomerID, &q.Question, &q.Timestamp, &q.Status, &q.CreatedAt)
|
|
if err != nil {
|
|
http.Error(w, "Question not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><body><h1>Question</h1><p>Client ID: ` + strconv.FormatInt(q.ClientID, 10) + `</p><p>Customer ID: ` + strconv.FormatInt(q.CustomerID, 10) + `</p><p>Question: ` + q.Question + `</p><p>Status: ` + q.Status + `</p><a href="/questions">Back</a></body></html>`))
|
|
}
|
|
|
|
func UpdateQuestion(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
r.ParseForm()
|
|
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
|
|
_, err := DB.Exec("UPDATE questions SET client_id=?, customer_id=?, question=?, status=? WHERE question_id=?", clientID, customerID, r.FormValue("question"), r.FormValue("status"), id)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func DeleteQuestion(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
DB.Exec("DELETE FROM questions WHERE question_id = ?", id)
|
|
}
|
|
|
|
func ListAnswers(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"))
|
|
questionID, _ := strconv.ParseInt(r.URL.Query().Get("question_id"), 10, 64)
|
|
if limit == 0 {
|
|
limit = 20
|
|
}
|
|
|
|
answers, err := db.ListAnswersWithDetails(DB, accountID, questionID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
questions, err := db.ListQuestions(DB, 0, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var questionOptions string
|
|
questionText := make(map[int64]string)
|
|
for _, q := range questions {
|
|
questionOptions += `<option value="` + strconv.FormatInt(q.QuestionID, 10) + `">` + q.Question + `</option>`
|
|
questionText[q.QuestionID] = q.Question
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
|
<style>
|
|
.edit-form { display:none; margin-top:0.5rem; padding:0.5rem; border:1px solid #ccc; }
|
|
.edit-row { display:none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Answers</h1>
|
|
<button class="btn" onclick="document.getElementById('answerForm').style.display='block'">Add Answer</button>
|
|
<div id="answerForm" style="display:none; margin-top:1rem;">
|
|
<form hx-post="/answers" hx-target="#answerList" hx-swap="innerHTML">
|
|
<select name="question_id" required>
|
|
<option value="">Select Question</option>
|
|
` + questionOptions + `
|
|
</select>
|
|
<textarea name="answer" placeholder="Answer" required></textarea>
|
|
<button type="submit">Add Answer</button>
|
|
</form>
|
|
</div>
|
|
<table>
|
|
<thead>
|
|
<tr><th>Client</th><th>Customer</th><th>Question</th><th>Answer</th><th>Status</th><th>Actions</th></tr>
|
|
</thead>
|
|
<tbody id="answerList">
|
|
`))
|
|
for _, a := range answers {
|
|
qText := a.QuestionText
|
|
if qText == "" {
|
|
qText = questionText[a.QuestionID]
|
|
if qText == "" {
|
|
qText = strconv.FormatInt(a.QuestionID, 10)
|
|
}
|
|
}
|
|
clientName := a.ClientName
|
|
if clientName == "" {
|
|
clientName = strconv.FormatInt(a.ClientID, 10)
|
|
}
|
|
customerName := a.CustomerName
|
|
if customerName == "" {
|
|
customerName = "Unknown"
|
|
}
|
|
w.Write([]byte(`
|
|
<tr>
|
|
<td>` + clientName + `</td>
|
|
<td>` + customerName + `</td>
|
|
<td>` + qText + `</td>
|
|
<td>` + a.Answer + `</td>
|
|
<td>` + a.Status + `</td>
|
|
<td>
|
|
<a href="/answers/` + strconv.FormatInt(a.AnswerID, 10) + `">View</a>
|
|
<button type="button" onclick="document.getElementById('editA`+strconv.FormatInt(a.AnswerID, 10)+`').style.display='table-row'">Edit</button>
|
|
<form method="DELETE" style="display:inline" hx-delete="/answers/`+strconv.FormatInt(a.AnswerID, 10)+`" hx-target="closest tr">
|
|
<button type="submit">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<tr id="editA`+strconv.FormatInt(a.AnswerID, 10)+`" class="edit-row" style="display:none">
|
|
<td colspan="6">
|
|
<form hx-put="/answers/`+strconv.FormatInt(a.AnswerID, 10)+`" hx-target="#answerList" hx-swap="innerHTML">
|
|
<select name="question_id">
|
|
<option value="`+strconv.FormatInt(a.QuestionID, 10)+`">`+qText+`</option>
|
|
`+questionOptions+`
|
|
</select>
|
|
<textarea name="answer">`+a.Answer+`</textarea>
|
|
<button type="submit">Save</button>
|
|
</form>
|
|
</td>
|
|
</tr>`))
|
|
}
|
|
w.Write([]byte(`
|
|
</tbody>
|
|
</table>
|
|
</body>
|
|
</html>`))
|
|
}
|
|
|
|
func CreateAnswer(w http.ResponseWriter, r *http.Request) {
|
|
accountID, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
questionID, _ := strconv.ParseInt(r.FormValue("question_id"), 10, 64)
|
|
|
|
var clientID int64
|
|
err := DB.QueryRow("SELECT client_id FROM questions WHERE question_id = ?", questionID).Scan(&clientID)
|
|
if err != nil {
|
|
http.Error(w, "Question not found", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var checkID int64
|
|
err = DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID)
|
|
if err != nil {
|
|
http.Error(w, "Invalid question", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
answer := db.Answer{
|
|
ClientID: clientID,
|
|
QuestionID: questionID,
|
|
Answer: r.FormValue("answer"),
|
|
Timestamp: time.Now().Unix(),
|
|
Status: "active",
|
|
CreatedAt: time.Now().Unix(),
|
|
}
|
|
|
|
if err := answer.Create(DB); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func ViewAnswer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
var a db.Answer
|
|
err := DB.QueryRow("SELECT answer_id, client_id, question_id, answer, timestamp, status, created_at FROM answers WHERE answer_id = ?", id).Scan(&a.AnswerID, &a.ClientID, &a.QuestionID, &a.Answer, &a.Timestamp, &a.Status, &a.CreatedAt)
|
|
if err != nil {
|
|
http.Error(w, "Answer not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><body><h1>Answer</h1><p>Question ID: ` + strconv.FormatInt(a.QuestionID, 10) + `</p><p>Answer: ` + a.Answer + `</p><a href="/answers">Back</a></body></html>`))
|
|
}
|
|
|
|
func UpdateAnswer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
r.ParseForm()
|
|
questionID, _ := strconv.ParseInt(r.FormValue("question_id"), 10, 64)
|
|
_, err := DB.Exec("UPDATE answers SET question_id=?, answer=? WHERE answer_id=?", questionID, r.FormValue("answer"), id)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func DeleteAnswer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
DB.Exec("DELETE FROM answers WHERE answer_id = ?", id)
|
|
} |