コード例 #1
0
        public ActionResult <IResource> AddResourceAction([FromQuery] string availableFrom, [FromBody] Resource resource)
        {
            if (resource == null)
            {
                return(BadRequest("Incorrect data provided"));
            }

            if (_resources.GetResource(resource.Id) != null)
            {
                return(BadRequest("Resource with such an ID already exists"));
            }

            DateTime date;

            DateTime.TryParseExact(availableFrom, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

            AddResource(resource, date);

            return(Created("add", resource));
        }
コード例 #2
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);
        }