Esempio n. 1
0
        public void DeleteExistingEmployee()
        {
            // verify we're starting empty
            Assert.AreEqual(0, context.Employees.Count());

            // setup new employee
            Employee employee = new Employee()
            {
                Name     = "Test User",
                Position = "Tester"
            };

            context.Employees.Add(employee);
            context.SaveChanges();

            // verify count is 1
            Assert.AreEqual(1, context.Employees.Count());

            // delete
            context.Remove(employee);
            context.SaveChanges();

            // verify count is back down to 0
            Assert.AreEqual(0, context.Employees.Count());
        }
Esempio n. 2
0
        public async Task <CleanupDatasetResultDto> CleanupEmptyDatasets()
        {
            if (!await _authManager.IsUserAdmin())
            {
                throw new UnauthorizedException("Only admins can perform system related tasks");
            }

            var datasets = _context.Datasets.Include(ds => ds.Organization).ToArray();

            _logger.LogInformation("Found {DatasetsCount} with objects count zero", datasets.Length);

            var deleted    = new List <string>();
            var notDeleted = new List <CleanupDatasetErrorDto>();

            foreach (var ds in datasets)
            {
                _logger.LogInformation("Analyzing dataset {OrgSlug}/{DsSlug}", ds.Organization.Slug, ds.Slug);

                try
                {
                    // Check if objects count is ok
                    var ddb = _ddbManager.Get(ds.Organization.Slug, ds.InternalRef);

                    var entries = (await ddb.SearchAsync("*", true))?.ToArray();

                    if (entries == null || !entries.Any())
                    {
                        _context.Remove(ds);
                        await _context.SaveChangesAsync();

                        deleted.Add(ds.Slug);
                        _logger.LogInformation("Deleted");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Cannot remove dataset '{DsSlug}'", ds.Slug);
                    notDeleted.Add(new CleanupDatasetErrorDto
                    {
                        Dataset      = ds.Slug,
                        Organization = ds.Organization.Slug,
                        Message      = ex.Message
                    });
                }
            }

            return(new CleanupDatasetResultDto
            {
                RemoveDatasetErrors = notDeleted.ToArray(),
                RemovedDatasets = deleted.ToArray()
            });
        }