Exemplo n.º 1
0
        public async Task <BaseApiResponse> Add(AddAdminRequest request)
        {
            request.CheckNotNull(nameof(request));

            var newadminid = GuidUtil.NewSequentialId();
            var command    = new CreateAdminCommand(
                newadminid,
                request.Name,
                request.LoginName,
                request.Portrait,
                PasswordHash.CreateHash(request.Password),
                request.Role,
                request.IsLocked
                );
            var result = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }

            //添加操作记录
            var currentAdmin = _contextService.GetCurrentAdmin(HttpContext.Current);

            RecordOperat(currentAdmin.AdminId.ToGuid(), "添加管理员", newadminid, "管理员:{0}".FormatWith(request.Name));

            return(new BaseApiResponse());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateAdmin(CreateAdminCommand createAdminCommand)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await Mediator.Send(createAdminCommand);

                    return(RedirectToAction("Login", new LoginViewModel
                    {
                        ReturnUrl = createAdminCommand.ReturnUrl
                    }));
                }
                catch (AuthenticationException e)
                {
                    ModelState.AddModelError("", e.Message);
                }
            }

            return(View("Login", new LoginViewModel
            {
                ReturnUrl = createAdminCommand.ReturnUrl,
                Username = createAdminCommand.Username
            }));
        }
        public async Task Handle_InCaseOfAddUserFailure_Should_Throw_BadRequestException()
        {
            this.mockedUserManager
            .Setup(x => x.AddToRoleAsync(It.IsAny <AuctionUser>(), AppConstants.AdministratorRole))
            .ReturnsAsync(IdentityResult.Failed());

            var command = new CreateAdminCommand {
                Email = "*****@*****.**", Role = "Administrator"
            };

            await Assert.ThrowsAsync <BadRequestException>(() => this.handler.Handle(command, CancellationToken.None));
        }
        public async Task Handle_Given_ValidModel_Should_Not_ThrowException()
        {
            this.mockedUserManager
            .Setup(x => x.AddToRoleAsync(It.IsAny <AuctionUser>(), AppConstants.AdministratorRole))
            .ReturnsAsync(IdentityResult.Success);

            var command = new CreateAdminCommand {
                Email = "*****@*****.**", Role = "Administrator"
            };

            await this.handler.Handle(command, CancellationToken.None);
        }
        public async Task <ActionResult <BaseResponse <Admin> > > CreateAdmin(CreateAdminCommand command)
        {
            try {
                var result = await _mediator.Send(command);

                return(Ok(result));
            } catch (ValidationException ex) {
                var err = new BaseResponse <Admin> ();
                err.Status  = ResponseType.Error;
                err.Message = ex.Message;
                err.Content = null;
                return(Ok(err));
            }
        }
Exemplo n.º 6
0
        public async Task <BaseApiResponse> Add([FromBody] AddAdminRequest request)
        {
            request.CheckNotNull(nameof(request));

            var command = new CreateAdminCommand(
                GuidUtil.NewSequentialId(),
                request.Name,
                request.LoginName,
                request.Portrait,
                PasswordHash.CreateHash(request.Password),
                request.Role,
                request.IsLocked
                );
            var result = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }
            return(new BaseApiResponse());
        }
        public async Task <BaseResponse <Admin> > Handle(CreateAdminCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <Admin> {
                ReponseName = nameof(CreateAdminCommand), Content = new List <Admin> ()
                {
                }
            };
            var entity    = _mapper.Map <Admin> (request);
            var newentity = await _adminRepository.AddAsync(entity);

            if (newentity == null)
            {
                response.Status  = ResponseType.Warning;
                response.Message = $"{nameof(Admin)} could not be created.";
                response.Content = null;
            }
            else
            {
                response.Status  = ResponseType.Success;
                response.Message = $"{nameof(Admin)} created successfully.";
                response.Content.Add(newentity);
            }
            return(response);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Post([FromBody] CreateAdminCommand model)
        {
            await this.Mediator.Send(model);

            return(this.NoContent());
        }
 public GenericCommandResult CreateAdmin(
     [FromServices] AdminHandler handler,
     [FromBody] CreateAdminCommand command)
 {
     return((GenericCommandResult)handler.Handle(command));
 }
Exemplo n.º 10
0
 public async Task <ActionResult <string> > CreateAdmin(CreateAdminCommand command, CancellationToken cancellationToken)
 {
     return(Ok(await Mediator.Send(command, cancellationToken)));
 }