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));
                }
            }));
        }
Example #2
0
        public Task CreateCollectionAsync(UserIdentity userIdentity, string newCollection)
        {
            var collection = new PhilatelicCollection {
                Id = Guid.NewGuid(), CollectorId = userIdentity.Id, Title = newCollection
            };

            return(Task.Run(() =>
            {
                InMemoryStore.PhilatelicCollections.Add(collection.Id, collection);

                if (InMemoryStore.CollectorColletions.ContainsKey(userIdentity.Id))
                {
                    InMemoryStore.CollectorColletions[userIdentity.Id].Add(collection.Id);
                }
                else
                {
                    InMemoryStore.CollectorColletions.Add(userIdentity.Id, new List <Guid> {
                        collection.Id
                    });
                }
            }));
        }