MCPcopy Index your code
hub / github.com/53AI/53AIHub / createOrUpdateOrderFromCache

Function createOrUpdateOrderFromCache

api/controller/pay.go:218–276  ·  view source on GitHub ↗

createOrUpdateOrderFromCache creates a new order from cache or updates an existing one

(eid int64, orderId string, status int, transactionId string)

Source from the content-addressed store, hash-verified

216
217// createOrUpdateOrderFromCache creates a new order from cache or updates an existing one
218func createOrUpdateOrderFromCache(eid int64, orderId string, status int, transactionId string) (*model.Order, error) {
219 // Lock to prevent concurrent operations
220 orderMutex.Lock()
221 defer orderMutex.Unlock()
222
223 // Check if order exists in database
224 dbOrder, err := model.GetOrderByOrderId(eid, orderId)
225 if err != nil {
226 // Order not found in database, check cache
227 cachedOrder, found := getCachedOrder(orderId)
228 if !found {
229 return nil, fmt.Errorf("order not found in database or cache: %s", orderId)
230 }
231
232 xlog.Info("Order found in cache:", orderId)
233 // Create order from cached data
234 cachedOrder.Status = status
235 if transactionId != "" {
236 cachedOrder.TransactionId = transactionId
237 }
238
239 if status == model.OrderStatusPaid {
240 cachedOrder.PayTime = time.Now().UTC().UnixMilli()
241 }
242
243 if err = cachedOrder.Create(); err != nil {
244 return nil, fmt.Errorf("create order from cache error: %v", err)
245 }
246 xlog.Info("Successfully created order from cache with status:", status)
247
248 // Remove from cache
249 removeCachedOrder(orderId)
250 xlog.Info("Removed order from cache")
251
252 return cachedOrder, nil
253 }
254
255 // Order exists, update status
256 if status == model.OrderStatusPaid {
257 err = model.UpdateOrderPaid(eid, orderId, transactionId)
258 dbOrder.Status = model.OrderStatusPaid
259 dbOrder.TransactionId = transactionId
260 dbOrder.PayTime = time.Now().UTC().UnixMilli()
261 } else {
262 err = model.UpdateOrderStatus(eid, orderId, status)
263 dbOrder.Status = status
264 }
265
266 if err != nil {
267 return nil, fmt.Errorf("update order status error: %v", err)
268 }
269 xlog.Info("Successfully updated order status to:", status)
270
271 // Remove from cache if exists
272 removeCachedOrder(orderId)
273 xlog.Info("Attempted to remove order from cache")
274
275 return dbOrder, nil

Callers 1

QueryOrderStatusFunction · 0.85

Calls 6

getCachedOrderFunction · 0.85
removeCachedOrderFunction · 0.85
ErrorfMethod · 0.80
InfoMethod · 0.80
UnlockMethod · 0.65
CreateMethod · 0.45

Tested by

no test coverage detected