Пример #1
0
        //Method Post use to create one outlet
        public static async Task <bool> CreateOutlet(OutletRequest request)
        {
            try
            {
                HttpClient client = new HttpClient();
                client = RestAPI.Get_HttpClient_Token();

                MultipartFormDataContent form = new MultipartFormDataContent();

                form.Add(new StreamContent(new MemoryStream(request.filePhoto)), "file", Path.GetFileName(request.pathPhoto));
                form.Add(new StringContent(request.outletName), "outlet_name");
                form.Add(new StringContent(request.outletAddress), "outlet_address");


                var response = new HttpResponseMessage();

                response = await client.PostAsync("/api/outlet", form);

                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }

                return(false);
            }
            catch
            {
                return(false);
            }
        }
Пример #2
0
        private async void btn_create_Click(object sender, RoutedEventArgs e)
        {
            FormCreateOutlet form = new FormCreateOutlet();

            form.ShowDialog();

            if (form.status)
            {
                progress_bar.Visibility = Visibility.Visible;
                OutletRequest outlet = new OutletRequest();

                outlet.outletName    = form.getOuletName();
                outlet.outletAddress = form.getOutletAddress();
                outlet.pathPhoto     = form.getPathPhoto();

                outlet.filePhoto = File.ReadAllBytes(form.getPathPhoto());

                if (await RestAPI.CreateOutlet(outlet))
                {
                    progress_bar.Visibility = Visibility.Collapsed;
                    MessageBox.Show("Create Outlet Successful");
                    if (send != null)
                    {
                        send.Invoke();
                    }
                }
                else
                {
                    progress_bar.Visibility = Visibility.Collapsed;
                    MessageBox.Show("Create Outlet Fail");
                }
            }
        }
Пример #3
0
        public async Task <GlobalResponse> Create(Guid businessId, OutletRequest request)
        {
            var business = await DbContext.Businesses.Include(x => x.Outlets).Include(x => x.ApplicationUsers)
                           .FirstOrDefaultAsync(x => x.Id == businessId);

            if (business == null)
            {
                throw new KeyNotFoundException("Business not found");
            }

            var outlet = business.Outlets.FirstOrDefault(x =>
                                                         string.Equals(x.Name.ToLower(), request.Name.ToLower(), StringComparison.Ordinal));

            if (outlet != null)
            {
                throw new ApplicationException("Outlet already exist");
            }

            var newOutlet = new Outlet
            {
                Name = request.Name, DateAdded = DateTime.Now, Inventory = new Inventory()
                {
                    Quantity = 0
                }
            };

            business.Outlets.Add(newOutlet);
            //await DbContext.Outlets.AddAsync(newOutlet);


            var applicationUsers = business.ApplicationUsers;

            if (!applicationUsers.Any())
            {
                throw new AppException("Cannot find the account owner of the business");
            }

            foreach (var applicationUser in applicationUsers)
            {
                var roles = await UserManager.GetRolesAsync(applicationUser);

                var roleCheck = roles.FirstOrDefault(x => x == "AccountOwner");
                if (!(roleCheck is null))
                {
                    applicationUser.Outlets.Add(newOutlet);
                    // newOutlet.ApplicationUsers.Add(applicationUser);
                    DbContext.Update(applicationUser);
                }
            }
            DbContext.Update(business);
            await DbContext.SaveChangesAsync();

            return(new GlobalResponse()
            {
                Message = "Successful", Status = true
            });
        }
        //[Authorize(Roles = "Cashier, Administrator")]
        public async Task <IActionResult> Create([FromRoute] Guid businessId, [FromBody] OutletRequest request)
        {
            var res = await OutletService.Create(businessId, request);

            return(Ok(res));
        }