Skip to content

Commit

Permalink
Endpoint for bounty ticket review send
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Nov 28, 2024
1 parent 3d51ab7 commit 22ed20e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ type Tickets struct {
Dependency []int `gorm:"type:integer[]"`
Description string `gorm:"type:text"`
Status TicketStatus `gorm:"type:varchar(50);not null;default:'draft'"`
Version int `gorm:"type:integer" json:"version"`
CreatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `gorm:"type:timestamp;not null;default:current_timestamp" json:"updated_at"`
}
Expand Down
63 changes: 63 additions & 0 deletions handlers/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ type ticketHandler struct {
db db.Database
}

type TicketResponse struct {
Success bool `json:"success"`
TicketID string `json:"ticket_id,omitempty"`
Message string `json:"message"`
Errors []string `json:"errors,omitempty"`
}

func NewTicketHandler(database db.Database) *ticketHandler {
return &ticketHandler{
db: database,
Expand Down Expand Up @@ -101,6 +108,62 @@ func (th *ticketHandler) DeleteTicket(w http.ResponseWriter, r *http.Request) {
respondWithJSON(w, http.StatusOK, map[string]string{"status": "success"})
}

func (th *ticketHandler) PostTicketDataToStakwork(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
respondWithJSON(w, http.StatusBadRequest, TicketResponse{
Success: false,
Message: "Validation failed",
Errors: []string{"Error reading request body"},
})
return
}

var ticket db.Tickets
if err := json.Unmarshal(body, &ticket); err != nil {
respondWithJSON(w, http.StatusBadRequest, TicketResponse{
Success: false,
Message: "Validation failed",
Errors: []string{"Error parsing request body: " + err.Error()},
})
return
}

var validationErrors []string
if ticket.UUID == uuid.Nil {
validationErrors = append(validationErrors, "UUID is required")
} else {
if _, err := uuid.Parse(ticket.UUID.String()); err != nil {
validationErrors = append(validationErrors, "Invalid UUID format")
}
}

if ticket.FeatureUUID == "" {
validationErrors = append(validationErrors, "FeatureUUID is required")
}
if ticket.PhaseUUID == "" {
validationErrors = append(validationErrors, "PhaseUUID is required")
}
if ticket.Name == "" {
validationErrors = append(validationErrors, "Name is required")
}

if len(validationErrors) > 0 {
respondWithJSON(w, http.StatusBadRequest, TicketResponse{
Success: false,
Message: "Validation failed",
Errors: validationErrors,
})
return
}

respondWithJSON(w, http.StatusOK, TicketResponse{
Success: true,
TicketID: ticket.UUID.String(),
Message: "Ticket submission is valid",
})
}

func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
Expand Down
1 change: 1 addition & 0 deletions routes/ticket_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TicketRoutes() chi.Router {
r.Group(func(r chi.Router) {
r.Use(auth.PubKeyContext)

r.Post("/review/send", ticketHandler.PostTicketDataToStakwork)
r.Get("/{uuid}", ticketHandler.GetTicket)
r.Put("/{uuid}", ticketHandler.UpdateTicket)
r.Delete("/{uuid}", ticketHandler.DeleteTicket)
Expand Down

0 comments on commit 22ed20e

Please sign in to comment.