private async Task DeletePetSitterHoliday(PetSitter petSitter)
 {
     var petSitterHolidays = await db.PetSitterHolidays.Where(p => p.PetSitterNo == petSitter.PetSitterNo).ToListAsync();
     db.PetSitterHolidays.RemoveRange(petSitterHolidays);
 }
        private async Task<List<PetSitterHoliday>> AddPetSitterHoliday(PetSitter petSitter, string holiday)
        {
            List<PetSitterHoliday> petSitterHolidays = new List<PetSitterHoliday>();

            var arr = HttpUtility.UrlDecode(holiday.ToString()).Split(',');

            for (int i = 0; i < arr.Length; i++)
            {
                PetSitterHoliday petSitterHoliday = new PetSitterHoliday();
                petSitterHoliday.PetSitterNo = petSitter.PetSitterNo;
                petSitterHoliday.PetSitterHolidayCode = int.Parse(arr[i].ToString());
                petSitterHolidays.Add(petSitterHoliday);
            }

            db.PetSitterHolidays.AddRange(petSitterHolidays);
            await db.SaveChangesAsync();

            return petSitterHolidays;
        }
        private async Task<List<PetSitterService>> AddPetSitterService(PetSitter petSitter, string service)
        {
            List<PetSitterService> petSitterServices = new List<PetSitterService>();
            var arr = HttpUtility.UrlDecode(service.ToString()).Split(',');

            for (int i = 0; i < arr.Length; i++)
            {
                PetSitterService petSitterService = new PetSitterService();
                petSitterService.PetSitterNo = petSitter.PetSitterNo;
                petSitterService.PetSitterServiceCode = int.Parse(arr[i].ToString());
                petSitterServices.Add(petSitterService);
            }

            db.PetSitterServices.AddRange(petSitterServices);
            await db.SaveChangesAsync();

            return petSitterServices;
        }
 private async Task DeletePetSitterService(PetSitter petSitter)
 {
     var petSitterServices = await db.PetSitterServices.Where(p => p.PetSitterNo == petSitter.PetSitterNo).ToListAsync();
     db.PetSitterServices.RemoveRange(petSitterServices);
 }
        public async Task<IHttpActionResult> PostPetSitter()
        {
            PetterResultType<PetSitter> petterResultType = new PetterResultType<PetSitter>();
            List<PetSitter> petSitters = new List<PetSitter>();
            List<PetSitterService> petSitterServices = new List<PetSitterService>();
            List<PetSitterHoliday> petSitterHolidays = new List<PetSitterHoliday>();
            PetSitter petSitter = new PetSitter();
            string pensionService = string.Empty;
            string pensionHoliday = string.Empty;

            if (Request.Content.IsMimeMultipartContent())
            {
                string folder = HostingEnvironment.MapPath(UploadPath.PetSitterPath);
                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 fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"'));

                        if (!FileExtension.PetSitterExtensions.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.PensionWidth, FileSize.PensionHeight, ImageFormat.Png);
                        petSitter.FileName = fileName;
                        petSitter.FilePath = UploadPath.PetSitterPath.Replace("~", "");
                    }
                    else
                    {
                        string str = await content.ReadAsStringAsync();
                        string item = HttpUtility.UrlDecode(str);

                        #region switch case
                        switch (fieldName)
                        {
                            case "PetSitterNo":
                                petSitter.PetSitterNo = int.Parse(item);
                                break;
                            case "CompanyNo":
                                petSitter.CompanyNo = int.Parse(item);
                                break;
                            case "PetSitterName":
                                petSitter.PetSitterName = item;
                                break;
                            case "PetSitterAddr":
                                petSitter.PetSitterAddr = item;
                                break;
                            case "FileName":
                                petSitter.FileName = item;
                                break;
                            case "FilePath":
                                petSitter.FilePath = item;
                                break;
                            case "StartTime":
                                petSitter.StartTime = item;
                                break;
                            case "EndTime":
                                petSitter.EndTime = item;
                                break;
                            case "Introduction":
                                petSitter.Introduction = item;
                                break;
                            case "Latitude":
                                petSitter.Latitude = Convert.ToDouble(item);
                                break;
                            case "Longitude":
                                petSitter.Longitude = Convert.ToDouble(item);
                                break;
                            //case "Grade":
                            //    petSitter.Grade = Convert.ToDouble(item);
                            //    break;
                            //case "ReviewCount":
                            //    petSitter.ReviewCount = int.Parse(item);
                            //    break;
                            //case "Bookmark":
                            //    petSitter.Bookmark = int.Parse(item);
                            //    break;
                            case "PetSitterServices":
                                pensionService = item;
                                break;
                            case "PetSitterHolidays":
                                pensionHoliday = item;
                                break;
                            default:
                                break;
                        }
                        #endregion switch case
                    }
                }

                string point = string.Format("POINT({0} {1})", petSitter.Longitude, petSitter.Latitude);
                petSitter.Coordinate = DbGeography.FromText(point);
                petSitter.DateCreated = DateTime.Now;
                petSitter.DateModified = DateTime.Now;
                db.PetSitters.Add(petSitter);
                int num = await this.db.SaveChangesAsync();

                if (!string.IsNullOrWhiteSpace(pensionService))
                {
                    List<PetSitterService> list = await AddPetSitterService(petSitter, pensionService);
                    petSitter.PetSitterServices = list;
                }

                if (!string.IsNullOrWhiteSpace(pensionHoliday))
                {
                    List<PetSitterHoliday> list = await AddPetSitterHoliday(petSitter, pensionHoliday);
                    petSitter.PetSitterHolidays = list;
                }

                petSitters.Add(petSitter);
                petterResultType.IsSuccessful = true;
                petterResultType.JsonDataSet = petSitters;
            }
            else
            {
                petterResultType.IsSuccessful = false;
                petterResultType.JsonDataSet = null;
            }

            return Ok(petterResultType);
        }