public async Task <Response> RegisterClient(string urlBase, string servicePrefix, string controller, PostClieAsDistRequest userRequest) { try { string request = JsonConvert.SerializeObject(userRequest); StringContent content = new StringContent(request, Encoding.UTF8, "application/json"); HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase) }; string url = $"{servicePrefix}{controller}"; HttpResponseMessage response = await client.PostAsync(url, content); string answer = await response.Content.ReadAsStringAsync(); Response obj = JsonConvert.DeserializeObject <Response>(answer); return(obj); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public RegisterClientPageViewModel(INavigationService navigationService, IRegexHelper regexHelper, IApiService apiService) : base(navigationService) { Title = "Registrar Cliente"; _navigationService = navigationService; _regexHelper = regexHelper; _apiService = apiService; IsEnabled = true; NewUser = new PostClieAsDistRequest(); LoadUser(); }
public async Task <IActionResult> PostClientAsDist([FromBody] PostClieAsDistRequest request) { if (!ModelState.IsValid) { return(BadRequest(new Response { IsSuccess = false, Message = "Bad request", Result = ModelState })); } UsuarioEntity user = await _userHelper.GetUserAsync(request.Email); if (user != null) { return(BadRequest(new Response { IsSuccess = false, Message = "El usuario ya existe" })); } user = new UsuarioEntity { Email = request.Email, UserName = request.Email, UserType = UserType.Cliente }; IdentityResult result = await _userHelper.AddUserAsync(user, request.Password); await _userHelper.AddUserToRoleAsync(user, user.UserType.ToString()); ClienteEntity cliente = new ClienteEntity() { Nombre = request.Nombre, Apellidos = request.Apellidos, Direccion = request.Direccion, Telefono = request.Telefono, Email = request.Email, ClienteAuth = user, }; _dataContext.Cliente.Add(cliente); await _dataContext.SaveChangesAsync(); if (result != IdentityResult.Success) { return(BadRequest(result.Errors.FirstOrDefault().Description)); } var distribuidor = _dataContext.Distribuidor.Where(d => d.id == request.DistId).FirstOrDefault(); if (distribuidor == null) { return(BadRequest(new Response { IsSuccess = false, Message = "El distribuidor especificado no existe." })); } _dataContext.ClienteDistribuidor.Add(new ClienteDistribuidor { Cliente = cliente, ClienteId = cliente.id, Distribuidor = distribuidor, DistribuidorId = distribuidor.id }); await _dataContext.SaveChangesAsync(); return(Ok(new Response { IsSuccess = true, Message = "completado" })); }