コード例 #1
0
        public async Task <int?> UpdateItems()
        {
            IEnumerable <ApiItem> apiItems = await _hirezApi.GetAllItems();

            int countNewAdditions = 0;

            foreach (ApiItem item in apiItems)
            {
                var     ItemInDB = _itemContext.GetByID((int)item.ItemId);
                ItemDTO iDTO     = new ItemDTO
                {
                    ItemID          = (int?)item.ItemId,
                    ItemName        = item.DeviceName,
                    ItemDescription = item.ItemDescription.SecondaryDescription != null?item.ItemDescription.SecondaryDescription.ToString() : item.ItemDescription.Description,
                                          ItemShortDescription = item.ShortDesc,
                                          ItemPrice            = item.Price,
                };
                //Add the itemStats ranging from 1 to 4 stats
                List <string> Stats     = new List <string>();
                int           statCount = item.ItemDescription.Menuitems.Count();
                //variable to set = the if statement ? if true do this : else do this
                Stats.Add(statCount > 0 ? item.ItemDescription.Menuitems[0].Value + " " + item.ItemDescription.Menuitems[0].Description : null);
                Stats.Add(statCount > 1 ? item.ItemDescription.Menuitems[1].Value + " " + item.ItemDescription.Menuitems[1].Description : null);
                Stats.Add(statCount > 2 ? item.ItemDescription.Menuitems[2].Value + " " + item.ItemDescription.Menuitems[2].Description : null);
                Stats.Add(statCount > 3 ? item.ItemDescription.Menuitems[3].Value + " " + item.ItemDescription.Menuitems[3].Description : null);
                iDTO.ItemStats = Stats;

                using (var webClient = new WebClient())
                {
                    try
                    {
                        byte[] icon = webClient.DownloadData(item.ItemIcon_Url);

                        iDTO.ItemIcon = icon;
                    }
                    catch
                    {
                        //something went wrong trying to get the image....
                    }
                }

                if (ItemInDB == null)
                {
                    //create
                    _itemContext.Add(iDTO);
                    countNewAdditions++;
                }
                else
                {
                    //update
                    _itemContext.Update(iDTO);
                }
            }
            //return number of added items.
            return(countNewAdditions);
        }
コード例 #2
0
        public async Task Ingest(RegistryItem newItem, Stream stream)
        {
            var item = _itemContext.GetItem(newItem);

            if (item is not null && !HasVersion(item, newItem.Version))
            {
                throw new AlreadyExistsException(item);
            }

            if (item is null)
            {
                item    = newItem;
                item.Id = Guid.NewGuid();
                _itemContext.Add(item);
            }

            item.AddVersion(newItem.Version);
            Validate(item);

            var fileKey = item.FileId(newItem.Version.Version);

            if (fileKey is null)
            {
                return(request.CreateStringResponse(HttpStatusCode.InternalServerError, "There was an issue trying to create the new version."));
            }

            bool hadContent = await _storageProvider.UploadZipAsync(fileKey.Value, stream, ObjectType.Module);

            if (hadContent)
            {
                _moduleContext.SaveChanges();
                return(request.CreateResponse(HttpStatusCode.Created));
            }
            else
            {
                return(request.CreateStringResponse(HttpStatusCode.BadRequest, "Uploaded file had zero bytes, and the module was not saved."));
            }
        }