|
| 1 | +package cache_serialization |
| 2 | + |
| 3 | +import ( |
| 4 | + "goproxy/domain/aggregates" |
| 5 | + "goproxy/domain/valueobjects" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +type PlanFeatureDto struct { |
| 10 | + PlanId int `json:"plan_id"` |
| 11 | + FeatureName string `json:"name"` |
| 12 | + FeatureDescription string `json:"description"` |
| 13 | +} |
| 14 | + |
| 15 | +type PlanDto struct { |
| 16 | + Id int `json:"id"` |
| 17 | + Name string `json:"name"` |
| 18 | + BytesLimit int64 `json:"bytes_limit"` |
| 19 | + Duration int `json:"duration"` |
| 20 | + Features []PlanFeatureDto `json:"features"` |
| 21 | + CreatedAt time.Time `json:"created_at"` |
| 22 | +} |
| 23 | + |
| 24 | +type AggegatePlanCacheSerializer struct{} |
| 25 | + |
| 26 | +func NewAggegatePlanCacheSerializer() CacheSerializer[aggregates.Plan, PlanDto] { |
| 27 | + return &AggegatePlanCacheSerializer{} |
| 28 | +} |
| 29 | + |
| 30 | +func (a *AggegatePlanCacheSerializer) ToT(dto PlanDto) aggregates.Plan { |
| 31 | + features := make([]valueobjects.PlanFeature, len(dto.Features)) |
| 32 | + for i, v := range dto.Features { |
| 33 | + features[i] = valueobjects.NewPlanFeature(v.PlanId, v.FeatureName, v.FeatureDescription) |
| 34 | + } |
| 35 | + |
| 36 | + plan, _ := aggregates.NewPlan(dto.Id, dto.Name, dto.BytesLimit, dto.Duration, features) |
| 37 | + return plan |
| 38 | +} |
| 39 | + |
| 40 | +func (a *AggegatePlanCacheSerializer) ToTArray(dtos []PlanDto) []aggregates.Plan { |
| 41 | + arr := make([]aggregates.Plan, len(dtos)) |
| 42 | + for i, dto := range dtos { |
| 43 | + arr[i] = a.ToT(dto) |
| 44 | + } |
| 45 | + |
| 46 | + return arr |
| 47 | +} |
| 48 | + |
| 49 | +func (a *AggegatePlanCacheSerializer) ToD(plan aggregates.Plan) PlanDto { |
| 50 | + features := make([]PlanFeatureDto, len(plan.Features())) |
| 51 | + for i, v := range plan.Features() { |
| 52 | + features[i] = PlanFeatureDto{ |
| 53 | + PlanId: v.PlanId(), |
| 54 | + FeatureName: v.Feature(), |
| 55 | + FeatureDescription: v.Description(), |
| 56 | + } |
| 57 | + } |
| 58 | + return PlanDto{ |
| 59 | + Id: plan.Id(), |
| 60 | + Name: plan.Name(), |
| 61 | + BytesLimit: plan.LimitBytes(), |
| 62 | + Duration: plan.DurationDays(), |
| 63 | + Features: features, |
| 64 | + CreatedAt: plan.CreatedAt(), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func (a *AggegatePlanCacheSerializer) ToDArray(plans []aggregates.Plan) []PlanDto { |
| 69 | + arr := make([]PlanDto, len(plans)) |
| 70 | + for i, plan := range plans { |
| 71 | + arr[i] = a.ToD(plan) |
| 72 | + } |
| 73 | + |
| 74 | + return arr |
| 75 | +} |
0 commit comments