Exemplo n.º 1
0
        public async Task <int> Create(Processor.Model.System model)
        {
            var dto = model.ToDomainModel();

            dto.Component = null;
            var entity = _db.System.Add(dto);
            await _db.SaveChangesAsync();

            foreach (var modelComponent in model.Components)
            {
                var dtoComponent = modelComponent.ToDomainModel();
                var exists       = await _db.Component.AnyAsync();

                dtoComponent.SystemId = entity.Entity.SystemId;

                if (exists)
                {
                    _db.Component.Update(dtoComponent);
                }
                else
                {
                    await _db.Component.AddAsync(dtoComponent);
                }

                await _db.SaveChangesAsync();
            }

            entity.State = EntityState.Detached;
            return(entity.Entity.SystemId);
        }
Exemplo n.º 2
0
        public async Task <bool> Update(Processor.Model.System model)
        {
            var dto      = model.ToDomainModel();
            var original = await ReadDtoSystem(_db, model.Id);

            var componentsToUnlink =
                original.Component.Where(c => !dto.Component.Select(comp => comp.ComponentId).Contains(c.ComponentId));
            var componentEntities = new List <EntityEntry <Component> >();

            foreach (var component in componentsToUnlink)
            {
                component.SystemId = null;
                component.System   = null;
                componentEntities.Add(_db.Component.Update(component));
            }

            var entity = _db.System.Update(dto);
            await _db.SaveChangesAsync();

            entity.State = EntityState.Detached;
            foreach (var componentEntity in componentEntities)
            {
                componentEntity.State = EntityState.Detached;
            }
            return(true);
        }
Exemplo n.º 3
0
        public static Models.System ToDomainModel(this Processor.Model.System model)
        {
            if (model == null)
            {
                return(null);
            }

            var dto = new Models.System
            {
                SystemId  = model.Id, SystemDescription = model.Description, SystemName = model.Name,
                Component = model.Components?.Select(c => c.ToDomainModel()).ToArray()
            };

            return(dto);
        }