public async Task <IActionResult> Create([FromBody] RegisterStoreRequestDto model) { if (!ModelState.IsValid) { return(BadRequest(new ManagedErrorResponse(ManagedErrorCode.Validation, "Hay errores de validación", ModelState))); } //Register User data, new user var user = new Account { UserName = model.UserName.Trim(), Email = model.Email.Trim() }; var result = await _userManager.CreateAsync(user, model.Password.Trim()); //Register Store data if (result.Succeeded) { //Store var store = new Store(); store.Name = model.StoreName; store.Address = model.StoreAddress; _storeRepository.Insert(store); //Relationship var accountStore = new AccountStore(); accountStore.AccountId = user.Id; accountStore.StoreId = store.Id; _accountStoreRepository.Insert(accountStore); await _context.SaveChangesAsync(); //Realiza el login y devuelve ya el token SecurityManager mgr = new SecurityManager(_jwtSettings, _userManager, _roleManager, _accountStoreRepository); var authUser = await mgr.BuildAuthenticatedUserObject(user); return(Ok(authUser)); } else { var errors = result.Errors.Select(x => x.Description).ToList(); return(BadRequest(new ManagedErrorResponse(ManagedErrorCode.Validation, "Identity validation errors", errors))); } }
public async Task <IActionResult> Post([FromBody] RegisterProductRequestDto model) { if (!ModelState.IsValid) { return(BadRequest(new ManagedErrorResponse(ManagedErrorCode.Validation, "Hay errores de validación", ModelState))); } try { var product = _mapper.Map <Product>(model); //StoreId assignment var storeId = AccountHelper.CurrentStoreId(User as ClaimsPrincipal); product.StoreId = Guid.Parse(storeId); _productRepository.Insert(product); await _context.SaveChangesAsync(); var response = _mapper.Map <RegisterProductResponseDto>(product); return(Ok(response)); } catch (Exception ex) { return(BadRequest(new ManagedErrorResponse(ManagedErrorCode.Exception, ex.Message, ex))); } }