Пример #1
0
        public List <InventoryItem> GetTrash(FileSystemConnectionContext connectionContext)
        {
            var snapshot = ProcessEvents(connectionContext, _inventorySnapshotProcessor, InitializeData);

            return((snapshot?.InventoryItems ?? new List <InventoryItem>())
                   .Where(item => item.Quality <= 0)
                   .ToList());
        }
Пример #2
0
        public void Insert <TEntity>(FileSystemConnectionContext connectionContext, TEntity model)
            where TEntity : DataEvent
        {
            var fileTypeMarker = model is Snapshot
                ? "snapshot"
                : "event";

            var fullName = Path.Combine(connectionContext.DataRoot, $"{fileTypeMarker}--{model.ContractName}--{model.Id}.json");
            var contents = JsonConvert.SerializeObject(model, Formatting.Indented);

            _fileSystem.CreateDirectoryIfItDoesntExist(connectionContext.DataRoot);
            _fileSystem.WriteAllText(fullName, contents);
        }
Пример #3
0
        protected TSnapshot ProcessEvents <TSnapshot>(
            FileSystemConnectionContext connectionContext,
            ISnapshotProcessor <TSnapshot> snapshotProcessor,
            Action <FileSystemConnectionContext> dataInitializer)
            where TSnapshot : Snapshot
        {
            _fileSystem.CreateDirectoryIfItDoesntExist(connectionContext.DataRoot);

            var loadInfos = new Func <List <IFileSystemInfoWrapper> >(() =>
                                                                      _fileSystem.GetFileInfos(connectionContext.DataRoot, "event-*.json")
                                                                      .OrderBy(item => item.CreationTimeUtc)
                                                                      .ToList());

            var infos = loadInfos();

            if (!infos.Any())
            {
                dataInitializer?.Invoke(connectionContext);
                infos = loadInfos();
            }

            var shouldSaveSnapshot = false;
            var snapshot           = LoadSnapshot <TSnapshot>(connectionContext);

            if (snapshot == null)
            {
                // dataInitializer?.Invoke(connectionContext);
                snapshot           = Activator.CreateInstance <TSnapshot>();
                shouldSaveSnapshot = true;
            }

            foreach (var info in infos)
            {
                if (snapshot.LastFileCreationTimeUtc.HasValue && snapshot.LastFileCreationTimeUtc >= info.CreationTimeUtc)
                {
                    continue;
                }

                shouldSaveSnapshot = true;

                var contents = _fileSystem.ReadAllText(info.FullName);
                snapshotProcessor.ProcessEvent(snapshot, contents, info.CreationTimeUtc);
            }

            if (shouldSaveSnapshot)
            {
                Insert(connectionContext, snapshot);
            }

            return(snapshot);
        }
Пример #4
0
        protected TSnapshot LoadSnapshot <TSnapshot>(FileSystemConnectionContext connectionContext)
            where TSnapshot : Snapshot
        {
            _fileSystem.CreateDirectoryIfItDoesntExist(connectionContext.DataRoot);

            var contractName = Activator.CreateInstance <TSnapshot>().ContractName;
            var info         = _fileSystem.GetFileInfos(connectionContext.DataRoot, $"snapshot--{contractName}-*.json")
                               .OrderByDescending(item => item.CreationTimeUtc)
                               .FirstOrDefault();

            if (info != null)
            {
                var contents = _fileSystem.ReadAllText(info.FullName);
                return(JsonConvert.DeserializeObject <TSnapshot>(contents));
            }

            return(null);
        }
Пример #5
0
        private List <InventoryItem> GetInitialInventory(FileSystemConnectionContext connectionContext)
        {
            const string ResourceName  = "gild_repo.res.inventory.txt";
            var          inventoryText = _resourceRepo.GetResource(ResourceName);
            var          lines         = inventoryText.Replace("\r\n", "\r").Replace("\n", "\r").Split('\r')
                                         .Where(queryLine => !string.IsNullOrWhiteSpace(queryLine))
                                         .Select(queryLine => queryLine.Trim())
                                         .ToList();

            var initialGoods = lines.Select(queryLine =>
            {
                var pieces = queryLine.Split(',').ToList();

                // name, category, sell-in, quality
                const int ExpectedPieces = 4;
                if (pieces.Count != ExpectedPieces)
                {
                    throw new ApplicationException($"Expected {ExpectedPieces} pieces, found {pieces.Count}.");
                }

                var name     = pieces[0];
                var category = pieces[1];
                var sellIn   = decimal.Parse(pieces[2]);
                var quality  = decimal.Parse(pieces[3]);

                return(new InventoryItem
                {
                    Name = name,
                    Category = category,
                    SellIn = sellIn,
                    Quality = quality
                });
            }).ToList();

            return(initialGoods);
        }
Пример #6
0
        public int GetCurrentDay(FileSystemConnectionContext connectionContext)
        {
            var files = _fileSystemWrapper.GetFileInfos(connectionContext.DataRoot, "event--advance-day-*.json");

            return(files != null ? files.Count : 0);
        }
Пример #7
0
 public void AdvanceDay(FileSystemConnectionContext connectionContext)
 {
     Insert(connectionContext, new AdvanceDayDataEvent());
 }
Пример #8
0
 public void InitializeData(FileSystemConnectionContext connectionContext)
 {
     Insert(connectionContext, new SetInitialInventoryDataEvent {
         InventoryItems = GetInitialInventory(connectionContext)
     });
 }
Пример #9
0
        public List <InventoryItem> Get(FileSystemConnectionContext connectionContext)
        {
            var snapshot = ProcessEvents(connectionContext, _inventorySnapshotProcessor, InitializeData);

            return(snapshot?.InventoryItems ?? new List <InventoryItem>());
        }