Example #1
0
        public async Task <IActionResult> RegisterNode([FromBody] ServerNodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var username = Guid.NewGuid().ToString();
            var password = StringGenerator.GetRandomString(24);
            var user     = new VanguardUser
            {
                UserName = username
            };
            var userResult = await _userManager.CreateAsync(user, password);

            if (!userResult.Succeeded)
            {
                ModelState.AddIdentityErrors(userResult.Errors);
                return(BadRequest(ModelState));
            }
            await _userManager.AddToRoleAsync(user, RoleConstants.NodeAgent);

            var createResult = await _service.CreateAsync(model, user);

            if (!createResult.Succeeded)
            {
                ModelState.AddEntityTransactionErrors(createResult.Errors);
                await _userManager.DeleteAsync(user);

                return(BadRequest(ModelState));
            }

            return(CreatedAtRoute("GetServerNode", new { id = createResult.Value.Id }, new UsernamePasswordCredentialsViewModel
            {
                Username = username,
                Password = password
            }));
        }