Exemple #1
0
        public async Task <IActionResult> Put(string id, [FromBody] ApplicationPostModel model)
        {
            var app = await _applicationManager.FindByIdAsync(id);

            if (app != null)
            {
                app.Name          = model.Name;
                app.ApplicationId = model.ApplicationId;
                app.Description   = model.Description;
                app.Enabled       = model.Enabled;

                var result = await _applicationManager.UpdateAsync(app);

                if (result.Succeeded)
                {
                    return(NoContent());
                }

                var ex = new MyApplicationException();
                ex.SetIdentityErrors(result.Errors);
                throw ex;
            }

            var msg = $"找不到指定的应用程序 {id}";

            _logger.LogWarning(msg);
            return(NotFound(new ApiErrorResult <ApiError>(new ApiError(ApiErrorCodes.ObjectNotFound, msg))));
        }
Exemple #2
0
        public async Task <IActionResult> Post([FromBody] ApplicationPostModel model)
        {
            if (await _applicationManager.Applications.AnyAsync(x => x.Name == model.Name || x.ApplicationId == model.ApplicationId))
            {
                return(BadRequest(new ApiErrorResult <ApiError>(new ApiError(ApiErrorCodes.BadArgument, "存在重复的名称或应用程序Id。"))));
            }

            var app = new Application
            {
                Id            = await _identityGenerator.GenerateAsync(),
                Name          = model.Name,
                ApplicationId = model.ApplicationId,
                Description   = model.Description,
                Enabled       = model.Enabled
            };
            var result = await _applicationManager.CreateApplicationAsync(app);

            if (result.Succeeded)
            {
                return(StatusCode(201,
                                  new ApiResult <ObjectCreationOutputModel <long> >(new ObjectCreationOutputModel <long>(app.Id,
                                                                                                                         $"{Request.Scheme}://{Request.Host.Value}/api/applications/{app.Id}"))));
            }

            var ex = new MyApplicationException();

            ex.SetIdentityErrors(result.Errors);
            throw ex;
        }
        public async Task <IActionResult> Delete([FromRoute] string id)
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            user.AvatarId = _applicationSettings.CurrentValue.DefaultAvatar;
            var result = await _userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok(new ApiResult <string>($"/api/files/{user.AvatarId}")));
            }

            var ex = new MyApplicationException();

            ex.SetIdentityErrors(result.Errors);
            throw ex;
        }
Exemple #4
0
        public async Task <IActionResult> Delete(string id)
        {
            var app = await _applicationManager.FindByIdAsync(id);

            if (app != null)
            {
                var result = await _applicationManager.RemoveApplicationAsync(app);

                if (result.Succeeded)
                {
                    return(NoContent());
                }

                var ex = new MyApplicationException();
                ex.SetIdentityErrors(result.Errors);
                throw ex;
            }

            return(NotFound(new ApiErrorResult <ApiError>(new ApiError(ApiErrorCodes.ObjectNotFound, $"找不到指定的应用程序 {id}"))));
        }
        public async Task <IActionResult> Patch([FromRoute] string id, [FromBody] AccountPasswordPatchModel model)
        {
            var user = await _userManager.FindByEmailAsync(User.Identity.Name);

            var result = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.Password);

            if (result.Succeeded)
            {
                return(NoContent());
            }

            if (result.Errors.FirstOrDefault(x => x.Code == _errorDescriber.PasswordMismatch().Code) != null)
            {
                return(BadRequest(new ApiErrorResult <ApiError>(new ApiError(ApiErrorCodes.BadArgument, "当前密码验证失败。"))));
            }

            var ex = new MyApplicationException();

            ex.SetIdentityErrors(result.Errors);
            throw ex;
        }
Exemple #6
0
        public async Task <IActionResult> Patch(string id, [FromBody] ApplicationPatchSubscribersModel model)
        {
            var app = await _applicationManager.FindByIdAsync(id);

            if (app == null)
            {
                var msg = $"找不到指定的应用程序 {id}";
                _logger.LogWarning(msg);
                return(NotFound(new ApiErrorResult <ApiError>(new ApiError(ApiErrorCodes.ObjectNotFound, msg))));
            }

            var result = await _applicationManager.SetSubscribersAsync(app, model.UserList);

            if (result.Succeeded)
            {
                return(NoContent());
            }

            var ex = new MyApplicationException();

            ex.SetIdentityErrors(result.Errors);
            throw ex;
        }