public Task SavePhilatelicItemAsync(UserIdentity collector, Guid collectionId, PhilatelicItem philatelicItem)
        {
            var path = $"{webRoot}{PersistencePathCreator.GetCollectionPersistencePath(collector, collectionId)}";

            PhilatelicCollection collection;

            return(Task.Run(() => {
                if (!File.Exists(path))
                {
                    throw new Exception("could not find required collection");
                }

                using (var streamReader = new StreamReader(new FileStream(path, FileMode.Open)))
                {
                    collection = JsonConvert.DeserializeObject <PhilatelicCollection>(streamReader.ReadToEnd());
                }

                var item = collection.Items.SingleOrDefault(itm => itm.Id == philatelicItem.Id);

                if (item == null)
                {
                    // add new item
                    collection.Items.Add(philatelicItem);
                }
                else
                {
                    // keep previous scans if no new scans provided
                    if (!philatelicItem.Scans.Any())
                    {
                        philatelicItem.Scans = item.Scans;
                    }
                    else
                    {
                        foreach (var scan in item.Scans)
                        {
                            scanRepository.RemoveScan(collector, collectionId, scan.Image);
                        }
                    }

                    // update existing item
                    var index = collection.Items.IndexOf(item);
                    collection.Items.RemoveAt(index);
                    collection.Items.Insert(index, philatelicItem);
                }

                using (var streamWriter = new StreamWriter(File.Create(path)))
                {
                    streamWriter.Write(JsonConvert.SerializeObject(collection));
                }
            }));
        }
        public Task <IPhilatelicCollection> GetCollectionAsync(UserIdentity collector, Guid collectionId)
        {
            var path = $"{webRoot}{PersistencePathCreator.GetCollectionPersistencePath(collector, collectionId)}";

            return(Task.Run(() =>
            {
                if (!File.Exists(path))
                {
                    throw new Exception("could not find required collection");
                }

                using (var streamReader = new StreamReader(new FileStream(path, FileMode.Open)))
                {
                    return JsonConvert.DeserializeObject <PhilatelicCollection>(streamReader.ReadToEnd()) as IPhilatelicCollection;
                }
            }));
        }
        public Task RemovePhilatelicItemAsync(UserIdentity collector, Guid collectionId, Guid itemId)
        {
            var path = $"{webRoot}{PersistencePathCreator.GetCollectionPersistencePath(collector, collectionId)}";

            PhilatelicCollection collection;

            return(Task.Run(() => {
                if (!File.Exists(path))
                {
                    throw new Exception("could not find required collection");
                }

                using (var streamReader = new StreamReader(new FileStream(path, FileMode.Open)))
                {
                    collection = JsonConvert.DeserializeObject <PhilatelicCollection>(streamReader.ReadToEnd());
                }

                var item = collection.Items.SingleOrDefault(itm => itm.Id == itemId);

                if (item == null)
                {
                    return;
                }

                foreach (var scan in item.Scans)
                {
                    scanRepository.RemoveScan(collector, collectionId, scan.Image);
                }

                var index = collection.Items.IndexOf(item);
                collection.Items.RemoveAt(index);

                using (var streamWriter = new StreamWriter(File.Create(path)))
                {
                    streamWriter.Write(JsonConvert.SerializeObject(collection));
                }
            }));
        }