This commit introduces several architectural improvements and optimizations across the codebase: - **Performance & Reliability**: - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips. - Added a circuit breaker to the JPush client to prevent cascading failures. - Introduced batch deletion and batch member addition capabilities in repositories. - Added message idempotency support using `client_msg_id` and a Redis-based cache. - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging. - **Code Refactoring**: - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability. - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake). - Standardized JSON serialization/deserialization in models using `json_helper.go`. - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses. - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`. - **Cache Improvements**: - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache. - Added `DeleteBatch` support to the cache interface. - **Other Changes**: - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides. - Improved WebSocket registration flow to handle connection limits gracefully.
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package dto
|
|
|
|
import (
|
|
"with_you/internal/model"
|
|
)
|
|
|
|
func ConvertTradeImageToResponse(img *model.TradeImage) TradeImageResponse {
|
|
if img == nil {
|
|
return TradeImageResponse{}
|
|
}
|
|
return TradeImageResponse{
|
|
ID: img.ID,
|
|
URL: img.URL,
|
|
ThumbnailURL: img.ThumbnailURL,
|
|
PreviewURL: img.PreviewURL,
|
|
PreviewURLLarge: img.PreviewURLLarge,
|
|
Width: img.Width,
|
|
Height: img.Height,
|
|
}
|
|
}
|
|
|
|
func ConvertTradeImagesToResponse(images []model.TradeImage) []TradeImageResponse {
|
|
result := make([]TradeImageResponse, 0, len(images))
|
|
for i := range images {
|
|
result = append(result, ConvertTradeImageToResponse(&images[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ConvertTradeItemToResponse(item *model.TradeItem, isFavorited bool) *TradeItemResponse {
|
|
if item == nil {
|
|
return nil
|
|
}
|
|
resp := &TradeItemResponse{
|
|
ID: item.ID,
|
|
UserID: item.UserID,
|
|
TradeType: string(item.TradeType),
|
|
Category: string(item.Category),
|
|
Title: item.Title,
|
|
Content: item.Content,
|
|
Price: item.Price,
|
|
Condition: item.Condition,
|
|
Status: string(item.Status),
|
|
ViewsCount: item.ViewsCount,
|
|
FavoritesCount: item.FavoritesCount,
|
|
IsFavorited: isFavorited,
|
|
Images: ConvertTradeImagesToResponse(item.Images),
|
|
CreatedAt: FormatTime(item.CreatedAt),
|
|
UpdatedAt: FormatTime(item.UpdatedAt),
|
|
}
|
|
if len(item.Segments) > 0 {
|
|
resp.Segments = item.Segments
|
|
}
|
|
if item.User != nil {
|
|
resp.Author = ConvertUserToResponse(item.User)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func ConvertTradeItemsToResponse(items []*model.TradeItem, favoriteMap map[string]bool) []*TradeItemResponse {
|
|
result := make([]*TradeItemResponse, 0, len(items))
|
|
for _, item := range items {
|
|
isFav := favoriteMap[item.ID]
|
|
result = append(result, ConvertTradeItemToResponse(item, isFav))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ConvertTradeItemToDetailResponse(item *model.TradeItem, isFavorited bool) *TradeItemResponse {
|
|
return ConvertTradeItemToResponse(item, isFavorited)
|
|
} |