package asset import ( "encoding/json" "github.com/google/uuid" domainAsset "base/internal/domain/asset" ) func toCategoryModel(category *domainAsset.Category) *CategoryModel { return &CategoryModel{ ID: category.ID, Name: category.Name, Icon: category.Icon, Color: category.Color, CardType: category.CardType, Featured: category.Featured, Description: category.Description, } } func toCategoryDomain(model *CategoryModel) *domainAsset.Category { return &domainAsset.Category{ ID: model.ID, Name: model.Name, Icon: model.Icon, Color: model.Color, CardType: model.CardType, Featured: model.Featured, Description: model.Description, } } func toAssetModel(asset *domainAsset.Asset) *Model { return &Model{ ID: asset.ID, ProfileID: asset.ProfileID, Status: int(asset.Status), AssetCategoryID: asset.AssetCategoryID, Title: asset.Title, Description: asset.Description, Link: asset.Link, Analytics: asset.Analytics, CreatedAt: asset.CreatedAt, UpdatedAt: asset.UpdatedAt, } } func toAssetDomain(model *Model, category *domainAsset.Category, artifacts []domainAsset.Artifact, comments []domainAsset.Comment, reports []domainAsset.Report) *domainAsset.Asset { cat := domainAsset.Category{} if category != nil { cat = *category } return &domainAsset.Asset{ ID: model.ID, ProfileID: model.ProfileID, Status: domainAsset.Status(model.Status), AssetCategoryID: model.AssetCategoryID, AssetCategory: cat, Title: model.Title, Description: model.Description, Link: model.Link, Analytics: model.Analytics, Reports: reports, AssetArtifacts: artifacts, Comments: comments, CreatedAt: model.CreatedAt, UpdatedAt: model.UpdatedAt, } } func copyAssetFromModel(asset *domainAsset.Asset, model *Model) { asset.ID = model.ID asset.CreatedAt = model.CreatedAt asset.UpdatedAt = model.UpdatedAt } func toArtifactModels(assetID uuid.UUID, artifacts []domainAsset.Artifact) []ArtifactModel { models := make([]ArtifactModel, len(artifacts)) for i, a := range artifacts { models[i] = ArtifactModel{ AssetID: assetID, Type: a.Type, DownloadURL: a.DownloadURL, Price: a.Price, Title: a.Title, Description: a.Description, } } return models } func toArtifactDomains(models []ArtifactModel) []domainAsset.Artifact { out := make([]domainAsset.Artifact, len(models)) for i, m := range models { out[i] = domainAsset.Artifact{ ID: m.ID, AssetID: m.AssetID, Type: m.Type, DownloadURL: m.DownloadURL, Price: m.Price, Title: m.Title, Description: m.Description, } } return out } // flattenComments turns a tree of comments (with Replies) into a single slice: // top-level first, then each comment's replies recursively. Used when saving. func flattenComments(comments []domainAsset.Comment) []domainAsset.Comment { var out []domainAsset.Comment for _, c := range comments { out = append(out, c) out = append(out, flattenComments(c.Replies)...) } return out } func toCommentModels(assetID uuid.UUID, comments []domainAsset.Comment) []CommentModel { flat := flattenComments(comments) models := make([]CommentModel, 0, len(flat)) for _, c := range flat { models = append(models, CommentModel{ AssetID: assetID, Content: c.Content, WriterID: c.WriterID, WriterType: c.WriterType, ParentID: c.ParentID, CreatedAt: c.CreatedAt, UpdatedAt: c.UpdatedAt, }) } return models } func toCommentDomains(models []CommentModel) []domainAsset.Comment { out := make([]domainAsset.Comment, len(models)) for i, m := range models { out[i] = domainAsset.Comment{ ID: m.ID, AssetID: m.AssetID, Content: m.Content, WriterID: m.WriterID, WriterType: m.WriterType, ParentID: m.ParentID, CreatedAt: m.CreatedAt, UpdatedAt: m.UpdatedAt, } } return buildCommentTree(out) } // buildCommentTree turns a flat list of comments (with ParentID set) into a tree: // top-level comments have Replies populated; nested Replies are not further nested in this type. func buildCommentTree(flat []domainAsset.Comment) []domainAsset.Comment { if len(flat) == 0 { return nil } byID := make(map[uuid.UUID]*domainAsset.Comment) for i := range flat { flat[i].Replies = nil byID[flat[i].ID] = &flat[i] } // First pass: attach replies to parents for i := range flat { c := &flat[i] if c.ParentID == nil { continue } if parent, ok := byID[*c.ParentID]; ok { parent.Replies = append(parent.Replies, *c) } } // Second pass: collect top-level comments (with Replies already populated) var roots []domainAsset.Comment for i := range flat { c := &flat[i] if c.ParentID == nil { roots = append(roots, *c) } } return roots } func toReportModels(assetID uuid.UUID, reports []domainAsset.Report) ([]ReportModel, error) { models := make([]ReportModel, len(reports)) for i, r := range reports { reportedBy, err := json.Marshal(r.ReportedBy) if err != nil { return nil, err } reason, err := json.Marshal(r.Reason) if err != nil { return nil, err } var attachments json.RawMessage if len(r.Attachments) > 0 { attachments, err = json.Marshal(r.Attachments) if err != nil { return nil, err } } models[i] = ReportModel{ AssetID: assetID, ReportedBy: reportedBy, ReportedAt: r.ReportedAt, Reason: reason, Status: int(r.Status), Notes: r.Notes, Attachments: attachments, CreatedAt: r.ReportedAt, UpdatedAt: r.ReportedAt, } } return models, nil } func toReportDomains(models []ReportModel) ([]domainAsset.Report, error) { out := make([]domainAsset.Report, len(models)) for i, m := range models { var reportedBy domainAsset.ReportedBy if err := json.Unmarshal(m.ReportedBy, &reportedBy); err != nil { return nil, err } var reason domainAsset.ReportReason if err := json.Unmarshal(m.Reason, &reason); err != nil { return nil, err } var attachments []domainAsset.Attachment if len(m.Attachments) > 0 { if err := json.Unmarshal(m.Attachments, &attachments); err != nil { return nil, err } } out[i] = domainAsset.Report{ ID: m.ID, AssetID: m.AssetID, ReportedBy: reportedBy, ReportedAt: m.ReportedAt, Reason: reason, Status: domainAsset.ReportStatus(m.Status), Notes: m.Notes, Attachments: attachments, } } return out, nil }