edit: unique code shortlink ambil dari tabel shortcodes
This commit is contained in:
@@ -127,3 +127,38 @@ func (h *ShortLinkHandler) ShortLinkAuth(w http.ResponseWriter, r *http.Request)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// RecycleShortcodes handles requests to recycle shortcodes from expired shortlinks
|
||||
func (h *ShortLinkHandler) RecycleShortcodes(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow admin role to recycle shortcodes
|
||||
userRole, ok := r.Context().Value(middleware.UserRoleKey).(string)
|
||||
if !ok || userRole != "admin" {
|
||||
h.logger.Warn("Unauthorized attempt to recycle shortcodes",
|
||||
zap.String("role", userRole))
|
||||
http.Error(w, "Only admin can recycle shortcodes", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
// Call the service to recycle shortcodes
|
||||
count, err := h.shortLinkService.CleanupExpiredShortcodes()
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to recycle shortcodes", zap.Error(err))
|
||||
http.Error(w, "Failed to recycle shortcodes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Log successful recycling
|
||||
h.logger.Info("Shortcodes recycled successfully",
|
||||
zap.Int("count", count))
|
||||
|
||||
// Return response
|
||||
response := map[string]interface{}{
|
||||
"status": "success",
|
||||
"message": "Shortcodes recycled successfully",
|
||||
"count": count,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user