private async Task DeleteStoreHoliday(Store store)
 {
     var storeHolidays = await db.StoreHolidays.Where(p => p.StoreNo == store.StoreNo).ToListAsync();
     db.StoreHolidays.RemoveRange(storeHolidays);
 }
        private async Task<List<StoreHoliday>> AddstoreHoliday(Store store, string holiday)
        {
            List<StoreHoliday> storeHolidays = new List<StoreHoliday>();
            var arr = HttpUtility.UrlDecode(holiday.ToString()).Split(',');

            for (int i = 0; i < arr.Length; i++)
            {
                StoreHoliday storeHoliday = new StoreHoliday();

                storeHoliday.StoreNo = store.StoreNo;
                storeHoliday.CodeID = arr[i].ToString().Trim();

                storeHolidays.Add(storeHoliday);
            }

            db.StoreHolidays.AddRange(storeHolidays);
            await db.SaveChangesAsync();

            return storeHolidays;
        }
        private async Task<List<StoreService>> AddStoreService(Store store, string service)
        {
            List<StoreService> storeServices = new List<StoreService>();
            var arr = HttpUtility.UrlDecode(service.ToString()).Split(',');

            for (int i = 0; i < arr.Length; i++)
            {
                StoreService storeservice = new StoreService();

                storeservice.StoreNo = store.StoreNo;
                storeservice.CodeID = arr[i].ToString().Trim();

                storeServices.Add(storeservice);
            }

            db.StoreServices.AddRange(storeServices);
            await db.SaveChangesAsync();

            return storeServices;
        }
 private async Task DeleteStoreService(Store store)
 {
     var storeServices = await db.StoreServices.Where(p => p.StoreNo == store.StoreNo).ToListAsync();
     db.StoreServices.RemoveRange(storeServices);
 }
        public async Task<IHttpActionResult> PostStore()
        {
            PetterResultType<Store> petterResultType = new PetterResultType<Store>();
            List<Store> stores = new List<Store>();
            List<StoreService> storeServices = new List<StoreService>();
            List<StoreHoliday> storeHolidays = new List<StoreHoliday>();
            Store store = new Store();
            string storeService = string.Empty;
            string storeHoliday = string.Empty;

            if (Request.Content.IsMimeMultipartContent())
            {
                string folder = HostingEnvironment.MapPath(UploadPath.StorePath);
                Utilities.CreateDirectory(folder);

                var provider = await Request.Content.ReadAsMultipartAsync();

                foreach (var content in provider.Contents)
                {
                    string fieldName = content.Headers.ContentDisposition.Name.Trim('"');
                    if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))
                    {
                        var file = await content.ReadAsByteArrayAsync();
                        string oldFileName = HttpUtility.UrlDecode(content.Headers.ContentDisposition.FileName.Trim('"'));
                        string fileName = Utilities.additionFileName(oldFileName);

                        if (!FileExtension.StoreExtensions.Any(x => x.Equals(Path.GetExtension(fileName.ToLower()), StringComparison.OrdinalIgnoreCase)))
                        {
                            petterResultType.IsSuccessful = false;
                            petterResultType.JsonDataSet = null;
                            petterResultType.ErrorMessage = ResultErrorMessage.FileTypeError;
                            return Ok(petterResultType);
                        }

                        string fullPath = Path.Combine(folder, fileName);
                        File.WriteAllBytes(fullPath, file);
                        string thumbnamil = Path.GetFileNameWithoutExtension(fileName) + "_thumbnail" + Path.GetExtension(fileName);

                        Utilities.ResizeImage(fullPath, thumbnamil, FileSize.StoreWidth, FileSize.StoreHeight, ImageFormat.Png);
                        store.FileName = fileName;
                        store.FilePath = UploadPath.StorePath.Replace("~", "");
                    }
                    else
                    {
                        string str = await content.ReadAsStringAsync();
                        string item = HttpUtility.UrlDecode(str);

                        #region switch case
                        switch (fieldName)
                        {
                            case "CompanyNo":
                                store.CompanyNo = int.Parse(item);
                                break;
                            case "CodeID":
                                store.CodeID = item;
                                break;
                            case "StoreName":
                                store.StoreName = item;
                                break;
                            //case "StoreID":
                            //    store.StoreID = item;
                            //    break;
                            case "Phone":
                                store.Phone = item;
                                break;
                            case "StoreAddress":
                                store.StoreAddress = item;
                                break;
                            case "FileName":
                                store.FileName = item;
                                break;
                            case "FilePath":
                                store.FilePath = item;
                                break;
                            case "StartTime":
                                store.StartTime = item;
                                break;
                            case "EndTime":
                                store.EndTime = item;
                                break;
                            case "Introduction":
                                store.Introduction = item;
                                break;
                            case "Latitude":
                                store.Latitude = Convert.ToDouble(item);
                                break;
                            case "Longitude":
                                store.Longitude = Convert.ToDouble(item);
                                break;
                            case "StoreServices":
                                storeService = item;
                                break;
                            case "StoreHolidays":
                                storeHoliday = item;
                                break;
                            default:
                                break;
                        }
                        #endregion switch case
                    }
                }

                string point = string.Format("POINT({0} {1})", store.Longitude, store.Latitude);
                store.Coordinate = DbGeography.FromText(point);
                store.StateFlag = "U";
                //store.WriteIP = "2222.2.22.22";
                store.WriteIP = Request.GetClientIpAddress();
                store.DateCreated = DateTime.Now;
                store.DateModified = DateTime.Now;
                db.Stores.Add(store);
                int num = await this.db.SaveChangesAsync();

                if (!string.IsNullOrWhiteSpace(storeService))
                {
                    //List<StoreService> list = await AddStoreService(store, storeService);
                    List<StoreService> list = await AddStoreService(store, storeService);
                    store.StoreServices = list;
                }

                if (!string.IsNullOrWhiteSpace(storeHoliday))
                {
                    List<StoreHoliday> list = await AddstoreHoliday(store, storeHoliday);
                    store.StoreHolidays = list;
                }

                stores.Add(store);
                petterResultType.AffectedRow = stores.Count();
                petterResultType.IsSuccessful = true;
                petterResultType.JsonDataSet = stores;
            }
            else
            {
                petterResultType.IsSuccessful = false;
                petterResultType.JsonDataSet = null;
            }

            return Ok(petterResultType);
        }