Esempio n. 1
0
        protected override ICommandResult Handle(TRequest request)
        {
            if (request is not UpdateEntityCommandBase <TDto> updateCommand)
            {
                throw new InvalidOperationException();
            }

            if (updateCommand.Dto == null)
            {
                throw new InvalidOperationException($"{nameof(updateCommand.Dto)} cannot be null.");
            }

            var entity = typeof(ILogicalDeletableEntity).IsAssignableFrom(typeof(TEntity))
                ? _session.QueryOver <TEntity>().Where(x => !((ILogicalDeletableEntity)x).IsDeleted)
                         .SingleOrDefault()
                : _session.Get <TEntity>(updateCommand.Dto.Id);

            if (entity == null)
            {
                return new CommandResult
                       {
                           Success      = false,
                           ErrorMessage = $"Entity with id {updateCommand.Dto.Id} cannot be found."
                       }
            }
            ;

            entity = MapToEntity(updateCommand.Dto, entity);

            var integrityResult = CommandSecurityService.SecureEntityIntegration(_session, entity);

            if (!integrityResult.Success)
            {
                return(integrityResult);
            }

            var securityResult = SetupSecurityCheck(entity, request);

            if (!securityResult.Success)
            {
                return(securityResult);
            }

            CommandSecurityService.LoadReferences(updateCommand.Dto, entity, _session);

            BeforeSave(entity, request);

            entity.ModificationDateUtc = DateTime.UtcNow;
            _session.Merge(entity);

            _loggingService.LogOperation(entity, LogOperationType.Modify);

            AfterSave(entity, request);

            return(new CommandResult {
                Success = true
            });
        }
Esempio n. 2
0
        protected override CreateEntityCommandResult Handle(TRequest request)
        {
            if (request is not CreateEntityCommandBase <TDto> createCommand)
            {
                throw new InvalidOperationException();
            }

            if (createCommand.Dto == null)
            {
                throw new InvalidOperationException($"{nameof(createCommand.Dto)} cannot be null.");
            }

            if (createCommand.Dto.Id > 0)
            {
                throw new InvalidCastException($"{nameof(createCommand.Dto.Id)} must be set to 0.");
            }

            var entity = _mapper.Map <TEntity>(createCommand.Dto);

            CommandSecurityService.SetDefaultValues(entity);

            var integrityResult = CommandSecurityService.SecureEntityIntegration(_session, entity);

            if (!integrityResult.Success)
            {
                return new CreateEntityCommandResult
                       {
                           Success      = false,
                           ErrorMessage = integrityResult.ErrorMessage
                       }
            }
            ;

            var securityResult = SetupSecurityCheck(entity, request);

            if (!securityResult.Success)
            {
                return new CreateEntityCommandResult
                       {
                           Success      = false,
                           ErrorMessage = securityResult.ErrorMessage
                       }
            }
            ;

            CommandSecurityService.LoadReferences(createCommand.Dto, entity, _session);

            BeforeSave(entity, request);

            _session.Save(entity);

            _loggingService.LogOperation(entity, LogOperationType.Create);

            AfterSave(entity, request);

            return(new CreateEntityCommandResult
            {
                Success = true,
                NewEntityId = entity.Id
            });
        }