Beispiel #1
0
        public async Task <IActionResult> SaveItem(PhilatelicItemViewModel item, List <IFormFile> scans)
        {
            var userIdentity = GetUserIdentity();

            var thisCulture = new CultureInfo("en-GB");

            DateTime.TryParseExact(item.Acquired,
                                   "dd/MM/yyyy",
                                   thisCulture,
                                   DateTimeStyles.None,
                                   out DateTime dateAcquired);

            var philatelicItem = new PhilatelicItem {
                Id = item.ItemId == Guid.Empty ? Guid.NewGuid() : item.ItemId,
                CatalogueReference = new CatalogueReference {
                    Catalogue = (CataloguesInUse)Enum.Parse(typeof(CataloguesInUse), item.Catalogue),
                    Area      = item.Area,
                    Number    = item.Number
                },
                Year        = item.Year,
                Description = item.Description,
                Paid        = new Price
                {
                    Currency = (Currency)Enum.Parse(typeof(Currency), item.Currency),
                    Figure   = item.Price
                },
                Acquired   = dateAcquired == DateTime.MinValue ? DateTime.Now.Date : dateAcquired.Date,
                Conditions = (Conditions)Enum.Parse(typeof(Conditions), item.Condition),
                Scans      = new Scans()
            };

            foreach (var scan in scans)
            {
                if (scan.Length > 0)
                {
                    var scanPath = await scanRepository.SaveCollectableScan(userIdentity, item.CollectionId, scan);

                    philatelicItem.Scans.Add(new Scan {
                        Image = $"/dataStorage{scanPath.Path}", Caption = scan.FileName
                    });
                }
            }

            await collectionRepository.SavePhilatelicItemAsync(userIdentity, item.CollectionId, philatelicItem);

            return(RedirectToAction("collection", new { id = item.CollectionId }));
        }
Beispiel #2
0
 public Task SavePhilatelicItemAsync(UserIdentity collector, Guid collectionId, PhilatelicItem philatelicItem)
 {
     throw new NotImplementedException();
 }
        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));
                }
            }));
        }
Beispiel #4
0
        public Task AddPhilatelicItemAsync(UserIdentity userIdentity, Guid collectionId, PhilatelicItem philatelicItem)
        {
            return(Task.Run(() => {
                if (InMemoryStore.CollectorColletions[userIdentity.Id].SingleOrDefault(collId => collId == collectionId) == null)
                {
                    throw new Exception("This is not one of the collector's collections");
                }

                InMemoryStore.PhilatelicCollections[collectionId].Items.Add(philatelicItem);
            }));
        }