public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { // Model is valid if (ModelState.IsValid) { // Ensure only create user if Kong consumer was created using (var transaction = _context.Database.BeginTransaction()) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var kongService = new KongService(); var createConsumer = await kongService.CreateConsumer(user.Id); if (createConsumer.IsSuccessStatusCode) { transaction.Commit(); return(Ok(new { succeeded = true })); } else { return(BadRequest(new { succeeded = false, errors = new string[] { "Cannot create Kong consumer." } })); } } return(BadRequest(result)); } } return(BadRequest(new { succeeded = false, errors = GetValidationErrors() })); }
public async Task <IActionResult> Edit(string id, [FromBody] EditUserViewModel model) { var user = await _userManager.FindByIdAsync(id); if (user == null) { return(NotFound()); } string kongAction = ""; if (user.LockoutEnd == DateTime.MaxValue && model.IsEnabled) { // Unlock user user.LockoutEnd = null; kongAction = "create"; } else if (user.LockoutEnd != DateTime.MaxValue && !model.IsEnabled) { // Lock user user.LockoutEnd = DateTime.MaxValue; kongAction = "delete"; } Utils.CopyProperties(model, user); if (ModelState.IsValid) { // Ensure only update user if Kong consumer was created or deleted using (var transaction = _context.Database.BeginTransaction()) { // Catch update database concurrency error try { // if (newAvatar != null && newAvatar.Length > 0) // { // // Todo: Validate upload files, resize images // // Catch save thumbnail error // try // { // var fileName = DateTime.Now.Ticks.ToString() + Path.GetExtension(newAvatar.FileName); // using (var stream = new FileStream(Path.Combine(_uploadPath, fileName), FileMode.Create)) // { // // Copy new avatar // await newAvatar.CopyToAsync(stream); // // Delete old avatar // //System.IO.File.Delete(Path.Combine(_uploadPath, user.Avatar)); // // Update new avatar // //user.Avatar = fileName; // } // } // catch // { // ModelState.AddModelError("Avatar", "Cannot save Avatar!"); // return BadRequest(new { succeeded = false, errors = GetValidationErrors() }); // } // } // Update User await _userManager.UpdateAsync(user); } catch (DbUpdateConcurrencyException) { if (!UserExists(user.Id)) { return(NotFound()); } else { throw; } } var kongService = new KongService(); HttpResponseMessage action = null; if (kongAction == "create") { action = await kongService.CreateConsumer(id); } else if (kongAction == "delete") { action = await kongService.DeleteConsumer(id); } if (action != null && !action.IsSuccessStatusCode) { return(BadRequest(new { succeeded = false, errors = new string[] { $"Cannot {kongAction} Kong consumer." } })); } transaction.Commit(); return(Ok(new { succeeded = true })); } } return(BadRequest(new { succeeded = false, errors = GetValidationErrors() })); }