forked from stakwork/sphinx-tribes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stakwork#1989 from aliraza556/mplement-ticket-crud…
…-endpoints-and-util-function Implement Ticket CRUD Endpoints With Supporting Utilities Functions
- Loading branch information
Showing
10 changed files
with
850 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/google/uuid" | ||
"github.com/stakwork/sphinx-tribes/db" | ||
) | ||
|
||
type ticketHandler struct { | ||
db db.Database | ||
} | ||
|
||
func NewTicketHandler(database db.Database) *ticketHandler { | ||
return &ticketHandler{ | ||
db: database, | ||
} | ||
} | ||
|
||
func (th *ticketHandler) GetTicket(w http.ResponseWriter, r *http.Request) { | ||
uuid := chi.URLParam(r, "uuid") | ||
if uuid == "" { | ||
http.Error(w, "UUID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
ticket, err := th.db.GetTicket(uuid) | ||
if err != nil { | ||
if err.Error() == "ticket not found" { | ||
http.Error(w, "Ticket not found", http.StatusNotFound) | ||
return | ||
} | ||
http.Error(w, fmt.Sprintf("Failed to get ticket: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
respondWithJSON(w, http.StatusOK, ticket) | ||
} | ||
|
||
func (th *ticketHandler) UpdateTicket(w http.ResponseWriter, r *http.Request) { | ||
uuidStr := chi.URLParam(r, "uuid") | ||
if uuidStr == "" { | ||
http.Error(w, "UUID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
ticketUUID, err := uuid.Parse(uuidStr) | ||
if err != nil { | ||
http.Error(w, "Invalid UUID format", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, "Error reading request body", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var ticket db.Tickets | ||
if err := json.Unmarshal(body, &ticket); err != nil { | ||
http.Error(w, "Error parsing request body", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
ticket.UUID = ticketUUID | ||
|
||
updatedTicket, err := th.db.UpdateTicket(ticket) | ||
if err != nil { | ||
if err.Error() == "feature_uuid, phase_uuid, and name are required" { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
http.Error(w, fmt.Sprintf("Failed to update ticket: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
respondWithJSON(w, http.StatusOK, updatedTicket) | ||
} | ||
|
||
func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) { | ||
uuid := chi.URLParam(r, "uuid") | ||
if uuid == "" { | ||
http.Error(w, "UUID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err := th.db.DeleteTicket(uuid) | ||
if err != nil { | ||
if err.Error() == "ticket not found" { | ||
http.Error(w, "Ticket not found", http.StatusNotFound) | ||
return | ||
} | ||
http.Error(w, fmt.Sprintf("Failed to delete ticket: %v", err), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
respondWithJSON(w, http.StatusOK, map[string]string{"status": "success"}) | ||
} | ||
|
||
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(code) | ||
json.NewEncoder(w).Encode(payload) | ||
} |
Oops, something went wrong.