コード例 #1
0
        public Task <IEnumerable <PhilatelicItem> > GetPhilatelicItemsAsync(UserIdentity collector, IEnumerable <Guid> itemsToInsure)
        {
            var path = $"{webRoot}{PersistencePathCreator.GetCollectionSummaryPersistencePath(collector)}";

            return(Task.Run(() =>
            {
                if (!File.Exists(path))
                {
                    return new List <PhilatelicItem>() as IEnumerable <PhilatelicItem>;
                }

                var result = new List <PhilatelicItem>();
                CollectionsSummary summary;
                using (var streamReader = new StreamReader(new FileStream(path, FileMode.Open)))
                {
                    summary = JsonConvert.DeserializeObject <CollectionsSummary>(streamReader.ReadToEnd());
                };

                foreach (var coll in summary.Collections)
                {
                    var persistedCollection = GetCollectionAsync(collector, coll.Id).Result;
                    result.AddRange(persistedCollection.Items.Where(i => itemsToInsure.Contains(i.Id)));
                }

                return result;
            }));
        }
コード例 #2
0
        public Task <IEnumerable <ICollectableCollection> > GetCollectionNamesAsync(UserIdentity collector)
        {
            var path = $"{webRoot}{PersistencePathCreator.GetCollectionSummaryPersistencePath(collector)}";

            return(Task.Run(() =>
            {
                if (!File.Exists(path))
                {
                    return (new List <IPhilatelicCollection>() as IEnumerable <ICollectableCollection>);
                }

                using (var streamReader = new StreamReader(new FileStream(path, FileMode.Open)))
                {
                    var summary = JsonConvert.DeserializeObject <CollectionsSummary>(streamReader.ReadToEnd());
                    var result = new List <IPhilatelicCollection>();

                    foreach (var coll in summary.Collections)
                    {
                        result.Add(new PhilatelicCollection {
                            Id = coll.Id, Title = coll.Title, CollectorId = collector.Id
                        });
                    }

                    return result;
                }
            }));
        }
コード例 #3
0
        public Task CreateCollectionAsync(UserIdentity collector, string newCollection)
        {
            var collection = new PhilatelicCollection {
                Id = Guid.NewGuid(), CollectorId = collector.Id, Title = newCollection, Items = new List <PhilatelicItem>()
            };
            var jsonDocument = JsonConvert.SerializeObject(collection);

            var documentName = $"{collection.Id}.json";
            var paths        = PersistencePathCreator.CreateCollectionDocumentPath(collector, collection.Id, documentName);

            var collectionSummaryPath = $"{webRoot}{PersistencePathCreator.GetCollectionSummaryPersistencePath(collector)}";

            var directory = $"{webRoot}{paths.DirectoryPath}";

            return(Task.Run(() =>
            {
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                using (var streamWriter = new StreamWriter(File.Create($"{webRoot}{paths.FilestreamPath}")))
                {
                    streamWriter.Write(jsonDocument);
                }

                // update colletions summary
                CollectionsSummary summary;
                if (File.Exists(collectionSummaryPath))
                {
                    using (var streamReader = new StreamReader(new FileStream(collectionSummaryPath, FileMode.Open)))
                    {
                        summary = JsonConvert.DeserializeObject <CollectionsSummary>(streamReader.ReadToEnd());
                        summary.Collections.Add(new ColletionSummary {
                            Id = collection.Id, Title = collection.Title
                        });
                    }
                }
                else
                {
                    summary = new CollectionsSummary
                    {
                        Collector = collector,
                        Collections = new List <ColletionSummary>
                        {
                            new ColletionSummary {
                                Id = collection.Id, Title = collection.Title
                            }
                        }
                    };
                }

                using (var streamWriter = new StreamWriter(File.Create(collectionSummaryPath)))
                {
                    streamWriter.Write(JsonConvert.SerializeObject(summary));
                }
            }));
        }
コード例 #4
0
        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));
                }
            }));
        }
コード例 #5
0
        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;
                }
            }));
        }
コード例 #6
0
        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));
                }
            }));
        }
コード例 #7
0
        public async Task <ScanPath> SaveCollectableScan(UserIdentity collector, Guid collectionId, IFormFile scan)
        {
            var filename = $"{Guid.NewGuid()}.jpg";              //TODO: do not assume file will be a jpg

            var paths    = PersistencePathCreator.CreateCollectableScanPath(collector, collectionId, filename);
            var scanPath = new ScanPath {
                Path = paths.PathToPersist
            };

            var directory = $"{webRoot}{paths.DirectoryPath}";

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (var stream = new FileStream($"{webRoot}{paths.FilestreamPath}", FileMode.Create))
            {
                await scan.CopyToAsync(stream);
            };

            return(scanPath);
        }