just updating the branch with current changes
This commit is contained in:
@@ -40,32 +40,6 @@ func (h *CardHandler) GetCards(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *CardHandler) setDefaultCard(w http.ResponseWriter, r *http.Request) {
|
||||
db := database.GetDB()
|
||||
userID := middleware.UserIDFromContext(r.Context())
|
||||
|
||||
idStr := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
utils.RespondError(w, http.StatusBadRequest, "INVALID_ID", "invalid card id", nil)
|
||||
return
|
||||
}
|
||||
|
||||
var card models.Card
|
||||
if err := db.Where("id = ? AND user_id = ?", id, userID).First(&card).Error; err != nil {
|
||||
utils.RespondError(w, http.StatusNotFound, "CARD_NOT_FOUND", "card not found", nil)
|
||||
return
|
||||
}
|
||||
|
||||
db.Model(&models.Card{}).Where("user_id = ?", userID).Update("id_default", false)
|
||||
|
||||
card.IsDefault = true
|
||||
db.Save(&card)
|
||||
|
||||
utils.RespondSuccess(w, http.StatusOK, map[string]interface{}{
|
||||
"card": card,
|
||||
})
|
||||
}
|
||||
|
||||
// func utils.RespondError(w http.ResponseWriter, statusCode int, code string, message string, details map[string]interface{})
|
||||
|
||||
@@ -92,6 +66,33 @@ func (h *CardHandler) GetCard(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *CardHandler) SetDefaultCard(w http.ResponseWriter, r *http.Request) {
|
||||
db := database.GetDB()
|
||||
userID := middleware.UserIDFromContext(r.Context())
|
||||
|
||||
idStr := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
utils.RespondError(w, http.StatusBadRequest, "INVALID_ID", "invalid card id", nil)
|
||||
return
|
||||
}
|
||||
|
||||
var card models.Card
|
||||
if err := db.Where("id = ? AND user_id = ?", id, userID).First(&card).Error; err != nil {
|
||||
utils.RespondError(w, http.StatusNotFound, "CARD_NOT_FOUND", "card not found", nil)
|
||||
return
|
||||
}
|
||||
|
||||
db.Model(&models.Card{}).Where("user_id = ?", userID).Update("id_default", false)
|
||||
|
||||
card.IsDefault = true
|
||||
db.Save(&card)
|
||||
|
||||
utils.RespondSuccess(w, http.StatusOK, map[string]interface{}{
|
||||
"card": card,
|
||||
})
|
||||
}
|
||||
|
||||
// SaveCard creates a saved card from a Stripe token. The token is converted to
|
||||
// a PaymentMethod and attached to the user's Stripe customer.
|
||||
func (h *CardHandler) SaveCard(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -233,3 +234,4 @@ func (h *CardHandler) DeleteCard(w http.ResponseWriter, r *http.Request) {
|
||||
"message": "card deleted",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -62,6 +62,7 @@ func SetupRoutes(version string, stripeSecretKey string, jwtManager *auth.JWTMan
|
||||
r.Get("/{id}", cardHandler.GetCard)
|
||||
r.Post("/", cardHandler.SaveCard)
|
||||
r.Delete("/{id}", cardHandler.DeleteCard)
|
||||
r.Put("/{id}/default", cardHandler.SetDefaultCard)
|
||||
})
|
||||
|
||||
// Orders
|
||||
|
||||
Reference in New Issue
Block a user