예제 #1
0
        public void Validate_ReturnFalse_WhenDataIsInvalid()
        {
            var command = new DeleteCommand
            {
                Id = 0,
            };

            var validator = new DeleteCommandValidator(_configConstants);
            var result    = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
        }
예제 #2
0
파일: Delete.cs 프로젝트: agao2/Chathub
            public async Task <Unit> Handle(DeleteCommand command, CancellationToken cancellationToken)
            {
                DeleteCommandValidator validator = new DeleteCommandValidator();
                var results = validator.Validate(command);

                // null or empty fields
                if (results.IsValid == false)
                {
                    throw new RestException(HttpStatusCode.BadRequest, results.Errors);
                }


                Chatroom chatroom = await _context.Chatrooms.FirstOrDefaultAsync(c => c.ChatroomId == command.Id);

                if (chatroom == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, "Chatroom not found");
                }

                _context.Chatrooms.Remove(chatroom);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
예제 #3
0
        public async Task <DeleteResult> Handle
            (DeleteCommand <_EntityType> command,
            CancellationToken cancellationToken)
        {
            if (command is null)
            {
                Logger.LogWarning(AppLogEvent.HandleArgumentError,
                                  "Entity Delete empty command");
                return(ErrorResult("Entity Delete empty command"));
            }

            try
            {
                var validator        = new DeleteCommandValidator <_EntityType>();
                var validationResult = await validator.ValidateAsync(command)
                                       .ConfigureAwait(false);

                if (!validationResult.IsValid)
                {
                    Logger.LogWarning(AppLogEvent.RequestValidationError,
                                      "Validation command error. Command={Command}. " +
                                      "Error={Error}.",
                                      command.ToDictionary(), validationResult.Errors);
                    return(ErrorResult
                               (validationResult.Errors.Select(x => x.ErrorMessage)));
                }

                var entityResponse = await Mediator
                                     .Send(new DbGetEntityByIdRequest <_EntityType>(command.Id))
                                     .ConfigureAwait(false);

                if (entityResponse.Success != true)
                {
                    Logger.LogWarning(AppLogEvent.HandleNullResponse,
                                      "Handle Entity Delete Command " +
                                      "can not get the entity. " +
                                      "Command={Command}. Error={Error}.",
                                      command.ToDictionary(), entityResponse.Errors);
                    return(ErrorResult(entityResponse.Errors));
                }
                var entity = entityResponse.Entity;

                // Get Actor for current user by user name
                var currentActorResponse = await Mediator
                                           .Send(new DbGetActorByNameRequest
                                                 (command.CurrentPrincipal?.Identity?.Name))
                                           .ConfigureAwait(false);

                Actor currentActor = null;
                if (currentActorResponse.Success)
                {
                    currentActor = currentActorResponse.Entity;
                }

                var checkPermissionResult = await CheckPermission
                                                (entity, currentActor, command.HardMode)
                                            .ConfigureAwait(false);

                if (!checkPermissionResult.Success)
                {
                    Logger.LogWarning(AppLogEvent.SecurityNotPassed,
                                      "Entity Delete permission not passed. " +
                                      "Entity={Entity}. CurrentActor={CurrentActor}. " +
                                      "Error={Error}.",
                                      entity.ToDictionary(), currentActor?.ToDictionary(),
                                      checkPermissionResult.Errors);
                    return(checkPermissionResult);
                }

                if (command.HardMode)
                {
                    await Mediator.Publish
                        (new BeforeEntityDelete <_EntityType>(entity))
                    .ConfigureAwait(false);

                    var result = await Mediator
                                 .Send(new DbDeleteCommand <_EntityType>(entity.Id))
                                 .ConfigureAwait(false);

                    await Mediator.Publish
                        (new AfterEntityDelete <_EntityType>(entity))
                    .ConfigureAwait(false);

                    return(result);
                }
                else
                {
                    return(await SoftDelete(command, entity, currentActor)
                           .ConfigureAwait(false));
                }
            }
            catch (Exception e)
            {
                Logger.LogError(AppLogEvent.HandleErrorResponse, e,
                                "Call repository exception");
                return(ErrorResult("Not found"));
            }
        }
예제 #4
0
 public void TestInitialize()
 {
     _validator = new DeleteCommandValidator();
 }