diff --git a/EbayListingTool/Services/SavedListingsService.cs b/EbayListingTool/Services/SavedListingsService.cs index dedb37f..db18b66 100644 --- a/EbayListingTool/Services/SavedListingsService.cs +++ b/EbayListingTool/Services/SavedListingsService.cs @@ -78,6 +78,76 @@ public class SavedListingsService return (listing, sources.Count - photoPaths.Count); } + /// + /// Saves a fully-posted listing draft, preserving aspects, postage, and shipping cost. + /// + public (SavedListing Listing, int SkippedPhotos) Save(ListingDraft draft) + { + return SaveFull( + draft.Title, + draft.Description, + draft.Price, + draft.CategoryName, + draft.CategoryId, + draft.Condition, + draft.Postage, + draft.ShippingCost, + draft.Aspects, + draft.Description, + draft.PhotoPaths + ); + } + + private (SavedListing Listing, int SkippedPhotos) SaveFull( + string title, string description, decimal price, + string category, string categoryId, + ItemCondition condition, PostageOption postage, decimal shippingCost, + Dictionary aspects, + string conditionNotes, + IEnumerable sourcePaths) + { + var safeName = MakeSafeFilename(title); + var exportDir = UniqueDir(Path.Combine(ExportsDir, safeName)); + Directory.CreateDirectory(exportDir); + + var photoPaths = new List(); + var sources = sourcePaths.ToList(); + for (int i = 0; i < sources.Count; i++) + { + var src = sources[i]; + if (!File.Exists(src)) continue; + var ext = Path.GetExtension(src); + var dest = i == 0 + ? Path.Combine(exportDir, $"{safeName}{ext}") + : Path.Combine(exportDir, $"{safeName}_{i + 1}{ext}"); + File.Copy(src, dest, overwrite: true); + photoPaths.Add(dest); + } + + var textFile = Path.Combine(exportDir, $"{safeName}.txt"); + File.WriteAllText(textFile, BuildTextExport(title, description, price, category, conditionNotes)); + + var listing = new SavedListing + { + Title = title, + Description = description, + Price = price, + Category = category, + CategoryId = categoryId, + Condition = condition, + Postage = postage, + ShippingCost = shippingCost, + Aspects = new Dictionary(aspects), + ConditionNotes = conditionNotes, + ExportFolder = exportDir, + PhotoPaths = photoPaths + }; + + _listings.Insert(0, listing); + Persist(); + return (listing, sources.Count - photoPaths.Count); + } + public void Delete(SavedListing listing) { _listings.Remove(listing);