Beispiel #1
0
        private static void ImportCsvToDatabase(ApplianceDbContext db, string partsFilePath)
        {
            var parts = CsvDataLoader.LoadPartsFromCsv(partsFilePath);

            db.AddRange(parts);
            db.SaveChanges();
        }
Beispiel #2
0
        private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AppliancePart> partsToUpload = CsvDataLoader.LoadPartsFromCsv("ApplianceParts.csv");
            List <AppliancePart> partsToDelete = new List <AppliancePart>();

            var newUploadTime = DateTime.UtcNow;

            Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes");
            bool success = true;

            foreach (var part in partsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.Id.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Text
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.Id}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.Id}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in partsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.Id}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }