Example #1
0
        public async Task <IActionResult> CreateVendor([FromBody] CreateVendorRequest vendorInput)
        {
            if (!ModelState.IsValid)
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Content = JsonConvert.SerializeObject(ModelState.Values.SelectMany(v => v.Errors))
                });
            }

            vendorInput.Type = UserType.Vendor;
            string vendorInputJson = JsonConvert.SerializeObject(vendorInput);
            User   user            = JsonConvert.DeserializeObject <User>(vendorInputJson);
            Vendor vendor          = JsonConvert.DeserializeObject <Vendor>(vendorInputJson);

            var createUserResponseObj = await this._CreateUser(user);

            var userResponse = createUserResponseObj.Item1;
            var userId       = createUserResponseObj.Item2;

            if (!userResponse.IsSuccessStatusCode)
            {
                return(await HttpHelper.ReturnResponseResult(userResponse));
            }

            vendor.UserID = userId;

            string addVendorUrl = $"http://{_billingService}/api/vendor";
            var    response     = await HttpHelper.PostAsync(addVendorUrl, new StringContent(
                                                                 JsonConvert.SerializeObject(vendor), Encoding.UTF8, "application/json"), this.Request);

            if (!response.IsSuccessStatusCode)
            {
                return(new ContentResult()
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Content = $"Billing controller couldn't create vendor billing data: {response.StatusCode} {await response.Content.ReadAsStringAsync()}"
                });
            }

            return(await GetUser(userId));
        }
        public async Task AddVendor(CreateVendorRequest Vendor)
        {
            await VendorService.AddNewVendorAsync(Vendor);

            await ModalInstance.CloseAsync();
        }
 protected async override Task OnInitializedAsync()
 {
     Vendor = new CreateVendorRequest();
 }
Example #4
0
        public async Task <IActionResult> UpdateVendor(string userId, [FromBody] CreateVendorRequest vendorInput)
        {
            if (!CheckValidUserId(userId))
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Content = "Invalid userId. The user Id is a valid Guid without the hyphens."
                });
            }

            vendorInput.Type = UserType.Vendor;

            var updateUserResponse = await this._UpdateUser(userId, vendorInput);

            if (updateUserResponse != null)
            {
                return(updateUserResponse);
            }

            // Now update Billing details
            string getVendorUrl = $"http://{_billingService}/api/vendor/{userId}";
            var    getResponse  = await HttpHelper.GetAsync(getVendorUrl, this.Request);

            if (!getResponse.IsSuccessStatusCode)
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Content = $"Couldn't find vendor billing details: {getResponse.StatusCode} {await getResponse.Content.ReadAsStringAsync()}"
                });
            }

            var currentVendor = JsonConvert.DeserializeObject <Vendor>(await getResponse.Content.ReadAsStringAsync());

            if (currentVendor == null)
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Content = "Unexpected object returned while getting vendor billing data"
                });
            }

            var updatedVendorDetails = new Vendor
            {
                UserID        = userId,
                AccountNumber = string.IsNullOrEmpty(vendorInput.AccountNumber) ? currentVendor.AccountNumber : vendorInput.AccountNumber,
                RoutingNumber = string.IsNullOrEmpty(vendorInput.RoutingNumber) ? currentVendor.RoutingNumber : vendorInput.RoutingNumber
            };

            string updateVendorDataUrl = $"http://{_billingService}/api/vendor";
            var    updateResponse      = await HttpHelper.PatchAsync(updateVendorDataUrl, new StringContent(
                                                                         JsonConvert.SerializeObject(updatedVendorDetails), Encoding.UTF8, "application/json"), this.Request);

            if (!updateResponse.IsSuccessStatusCode)
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Content = $"Couldn't update vendor billing details: {updateResponse.StatusCode} {await updateResponse.Content.ReadAsStringAsync()}"
                });
            }

            var updatedVendor = JsonConvert.DeserializeObject <Vendor>(await updateResponse.Content.ReadAsStringAsync());

            if (updatedVendor == null)
            {
                return(new ContentResult
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError,
                    Content = $"Unexpected object returned while updating vendor billing data"
                });
            }

            return(await GetUser(userId));
        }
 public async Task AddNewVendorAsync(CreateVendorRequest Vendor)
 {
     var VendorJson = new StringContent(JsonSerializer.Serialize(Vendor), Encoding.UTF8, "application/json");
     await HttpClient.PostAsync($"vendors", VendorJson);
 }