Esempio n. 1
0
        public async Task <long> Create(AddStoreVMRequest entity)
        {
            var response = await _httpService.Post2 <AddStoreVMRequest, long>(url, entity);

            if (!response.Success)
            {
                throw new ApplicationException(await response.GetBody());
            }
            return(response.Response);
        }
Esempio n. 2
0
        public async Task <ActionResult> Post(AddStoreVMRequest entity)
        {
            #region Start the watch
            var watch = new Stopwatch();
            watch.Start();
            #endregion


            var result = await _entityServices.Post(entity, User.Identity.Name);

            #region End the watch
            watch.Stop();
            result.Meta.TotalProcessingTime = watch.ElapsedMilliseconds;
            #endregion

            return(Ok(result));
        }
        public async Task <ApiResponse> Post(AddStoreVMRequest entity, string userName)
        {
            try
            {
                var user = await _userCServices.GetCurrent(userName);

                long max   = 0;
                var  query = await _entityRepository.TableNoTracking.FirstOrDefaultAsync();

                if (query != null)
                {
                    max = await _entityRepository.TableNoTracking.MaxAsync(x => x.Id);
                }
                Store store = new()
                {
                    Name       = entity.Name,
                    Address    = entity.Address,
                    CityId     = entity.CityId,
                    CurrencyId = entity.CurrencyId,
                    Image      = entity.Image,
                    Code       = $"Store_{max + 1}",
                    userCId    = user.Id
                };
                if (!string.IsNullOrWhiteSpace(entity.Image))
                {
                    var entityImage = Convert.FromBase64String(entity.Image);
                    store.Image = await _fileService.SaveFile(entityImage, "jpg", path, store.Code);
                }
                var newEntity = await _entityRepository.InsertAsync(store);

                var storeId = newEntity.Id;
                foreach (var item in entity.Tags)
                {
                    StoreTags x = new()
                    {
                        StoreId = storeId,
                        TagId   = item
                    };
                    await _context.StoreTags.AddAsync(x);
                }
                foreach (var item in entity.Countries)
                {
                    StoreCountries x = new()
                    {
                        StoreId   = storeId,
                        CountryId = item
                    };
                    await _context.StoreCountries.AddAsync(x);
                }
                foreach (var item in entity.Cities)
                {
                    StoreCities x = new()
                    {
                        StoreId = storeId,
                        CityId  = item
                    };
                    await _context.StoreCities.AddAsync(x);
                }
                foreach (var item in entity.Users)
                {
                    StoreUsers x = new()
                    {
                        StoreId = storeId,
                        UserCId = item
                    };
                    await _context.StoreUsers.AddAsync(x);
                }
                foreach (var item in entity.PaymentMethods)
                {
                    StorePaymentMethods x = new()
                    {
                        StoreId         = storeId,
                        PaymentMethodId = item.PaymentMethodId,
                        Details         = item.Details
                    };
                    await _context.StorePaymentMethods.AddAsync(x);
                }
                await _context.SaveChangesAsync();

                StoreUsers storeUser = new();
                storeUser.StoreId = newEntity.Id;
                storeUser.UserCId = user.Id;
                storeUser.Role    = "[0,1,2,3]";
                storeUser.Status  = StoreUserStatus.Accepted;
                _storeUserRepository.Insert(storeUser);
                return(ApiResponse.Create(HttpStatusCode.OK, storeId));
            }
            catch (Exception)
            {
                return(ApiResponse.Create(HttpStatusCode.InternalServerError, null, "InternalServerError_Error"));
            }
        }