public async Task <SiteViewModelWithChilds> SaveSite(SiteViewModelWithChilds model)
        {
            Site entity;

            if (model.Id == 0)
            {
                entity           = _deepMapper.Map <Site>(model);
                entity.CreatedAt = DateTime.UtcNow;
                entity           = _siteRepository.Add(entity);
            }
            else
            {
                entity           = _simpleMapper.Map <Site>(model);
                entity.UpdatedAt = DateTime.UtcNow;
                entity           = _siteRepository.Update(entity);
                var modelEndpoints = model.Endpoints.Select(x => x.Id).ToArray();

                var removed = await _endpointRepository.Query()
                              .Where(x => x.SiteId == model.Id && !modelEndpoints.Any(me => me == x.Id)).ToListAsync();

                _endpointRepository.Delete(removed);

                model.Endpoints.ForEach(x => x.SiteId = model.Id);
                var endpoints = _simpleMapper.Map <Endpoint[]>(model.Endpoints);
                endpoints = _endpointRepository.AddOrUpdate(endpoints).ToArray();
            }



            await _uow.CommitAsync();

            var result = _deepMapper.Map <SiteViewModelWithChilds>(entity);

            return(result);
        }
示例#2
0
        public ICommandResult Handle(DeleteEndpointCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Dados inválidos. Verifique o preenchimento dos campos e tente novamente.", command.Notifications));
            }

            var endpoint = _repository.GetBySerialNumber(command.SerialNumber);

            if (endpoint == null)
            {
                return(new GenericCommandResult(false, "Nenhum Endpoint encontrado com esse Número Serial.", null));
            }

            if (_repository.Delete(command.SerialNumber))
            {
                return(new GenericCommandResult(true, "Registro excluído com sucesso.", endpoint));
            }
            else
            {
                return(new GenericCommandResult(false, "Erro ao excluir registro.", endpoint));
            }
        }