Exemplo n.º 1
0
        private void RemoveDeletedRemoteEntriesToLocal(SDSchemaObject data)
        {
            var removedTables = _efContext.SDStatuses
                                .Where(s => !data.SDDataTables.Any(t => t.Id == s.Id))
                                .Select(s => _efContext.SDDataTables.SingleOrDefault(t => t.Id == s.Id))
                                .Where(s => s != null)
                                .ToList();

            var removedColumns = _efContext.SDStatuses
                                 .Where(s => !data.SDDataTables.SelectMany(t => t.Columns).Any(c => c.Id == s.Id))
                                 .Select(i => _efContext.SDColumns.SingleOrDefault(c => c.Id == i.Id))
                                 .Where(i => i != null)
                                 .Where(c => !removedTables.Any(t => t.Id != c.SDDataTableId))
                                 .ToList();

            foreach (var table in removedTables)
            {
                _dbTableRepository.Remove(table);
                _efContext.SDDataTables.Remove(table);
                _efContext.SDStatuses.Remove(new SDStatus(table.Id));
                _efContext.SDStatuses.RemoveRange(table.Columns.Select(c => new SDStatus(c.Id)));
            }
            foreach (var column in removedColumns)
            {
                var tableName = _efContext.SDDataTables.Single(t => t.Id == column.SDDataTableId).Name;
                _dbTableRepository.RemoveColumn(tableName, column);
                _efContext.SDColumns.Remove(column);
                _efContext.SDStatuses.Remove(new SDStatus(column.Id));
            }
            _efContext.SaveChanges();
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PostSchemaObject([FromBody] SDSchemaObject schemaObject)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.SDDataTables.AddRange(schemaObject.SDDataTables);
            _context.SDColumns.AddRange(schemaObject.SDColumns);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetDbSchema", schemaObject));
        }
Exemplo n.º 3
0
        public async Task CopyNewLocalEntriesToRemote()
        {
            var newTables = _efContext.SDDataTables
                            .Include(t => t.Columns)
                            .Where(table => !_efContext.SDStatuses.Any(s => s.Id == table.Id))
                            .ToList();

            var newColumns = _efContext.SDColumns
                             .Where(column => !_efContext.SDStatuses.Any(s => s.Id == column.Id))
                             .Where(column => !newTables.Any(t => t.Columns.Any(c => c.Id != column.SDDataTableId)))
                             .ToList();

            var schemaObject = new SDSchemaObject(newTables, newColumns);


            var request = new RestRequest(ApiUrl, Method.POST);

            request.AddHeader("Authorization", "Bearer token");
            request.AddJsonBody(schemaObject);

            var response = _client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.Created)
            {
                Console.WriteLine("OK");
            }
            else
            {
                throw new Exception("Network error");
            }


            foreach (var table in schemaObject.SDDataTables)
            {
                _efContext.SDStatuses.Add(new SDStatus(table.Id));
                foreach (var column in table.Columns)
                {
                    _efContext.SDStatuses.Add(new SDStatus(column.Id));
                }
            }
            foreach (var column in schemaObject.SDColumns)
            {
                _efContext.SDStatuses.Add(new SDStatus(column.Id));
            }
            _efContext.SaveChanges();
        }
Exemplo n.º 4
0
        private void CopyNewReomteEntriesToLocal(SDSchemaObject data)
        {
            var newTables = data.SDDataTables
                            .Where(remote => !_efContext.SDStatuses.Any(local => local.Id == remote.Id))
                            .ToList();

            var newColumns = data.SDDataTables
                             .SelectMany(t => t.Columns)
                             .Where(remote => !_efContext.SDStatuses.Any(local => local.Id == remote.Id))
                             .ToList();

            foreach (var table in newTables)
            {
                // Remove Navigation Property in order to take care of it manually later
                table.Columns = null;

                var createdEmptyTable = _dbTableRepository.TryCreateEmptyTable(table);
                if (createdEmptyTable)
                {
                    _efContext.SDDataTables.Add(table);
                }
                else
                {
                    var tableInContext = _efContext.SDDataTables.Include(t => t.Columns).Single(t => t.Name.Equals(table.Name, StringComparison.OrdinalIgnoreCase));
                    _efContext.SDDataTables.Remove(tableInContext);
                    _efContext.SaveChanges();
                    tableInContext.Id = table.Id;
                    foreach (var col in tableInContext.Columns)
                    {
                        col.SDDataTableId = table.Id;
                    }
                    tableInContext.Columns = null;
                    _efContext.SDDataTables.Add(tableInContext);
                }
                _efContext.SDStatuses.Add(new SDStatus(table.Id));
                _efContext.SaveChanges();
            }
            foreach (var column in newColumns)
            {
                var tableName = _efContext.SDDataTables.Single(t => t.Id == column.SDDataTableId).Name;

                var result = _dbTableRepository.TryAddColumn(tableName, column);
                if (result == ColumnAddState.Added)
                {
                    _efContext.SDColumns.Add(column);
                    _efContext.SDStatuses.Add(new SDStatus(column.Id));
                }
                else if (result == ColumnAddState.DuplicateWithoutConflict)
                {
                    var columnInContext = _efContext.SDColumns.Single(t => t.Name.Equals(column.Name, StringComparison.OrdinalIgnoreCase));
                    _efContext.SDColumns.Remove(columnInContext);
                    _efContext.SaveChanges();
                    columnInContext.Id = column.Id;
                    _efContext.SDColumns.Add(columnInContext);
                    _efContext.SDStatuses.Add(new SDStatus(column.Id));
                }
                else
                {
                    throw new Exception("Column Conflict");
                }
                _efContext.SaveChanges();
            }
            _efContext.SaveChanges();
        }