public IHttpActionResult UpdatePhotos(UpdatePhotosRequest photoDataRequest) { var root = HttpContext.Current.Server.MapPath("~/FileUploads"); string allGirlsFilePath = Path.Combine(root, "allGirls.json"); if (!File.Exists(allGirlsFilePath)) { using (StreamWriter w = File.AppendText(allGirlsFilePath)) { } } string allGirlsText = File.ReadAllText(allGirlsFilePath); IList <PhotoDataGirlName> photoDataGirlNameList = JsonConvert.DeserializeObject <List <PhotoDataGirlName> >(allGirlsText) ?? new List <PhotoDataGirlName>(); PhotoDataGirlName findGirl = photoDataGirlNameList.SingleOrDefault(m => m.Id == photoDataRequest.Id); if (findGirl != null) { findGirl.Image = photoDataRequest.PhotoDataList.Where(m => m.IsMain == true).Select(m => m.Image).FirstOrDefault() ?? photoDataRequest.PhotoDataList.Select(m => m.Image).FirstOrDefault() ?? string.Empty; } string allGirlsJson = JsonConvert.SerializeObject(photoDataGirlNameList); File.WriteAllText(allGirlsFilePath, allGirlsJson); //--------------------------------------------------------- string filePath = Path.Combine(root, photoDataRequest.Id + "_photos.json"); string json = JsonConvert.SerializeObject(photoDataRequest.PhotoDataList); File.WriteAllText(filePath, json); return(this.Ok(true)); }
public IHttpActionResult DeleteGirl(RequestDeleteGirl requestDeleteGirl) { var root = HttpContext.Current.Server.MapPath("~/FileUploads"); string allGirlsFilePath = Path.Combine(root, "allGirls.json"); if (!File.Exists(allGirlsFilePath)) { using (StreamWriter w = File.AppendText(allGirlsFilePath)) { } } string allGirlsText = File.ReadAllText(allGirlsFilePath); IList <PhotoDataGirlName> photoDataGirlNameList = JsonConvert.DeserializeObject <List <PhotoDataGirlName> >(allGirlsText) ?? new List <PhotoDataGirlName>(); PhotoDataGirlName findGirl = photoDataGirlNameList.SingleOrDefault(m => m.Id == requestDeleteGirl.Id); if (findGirl != null) { photoDataGirlNameList.Remove(findGirl); } string allGirlsJson = JsonConvert.SerializeObject(photoDataGirlNameList); File.WriteAllText(allGirlsFilePath, allGirlsJson); //-------------------------------------- string filePath = Path.Combine(root, requestDeleteGirl.Id + ".json"); if (File.Exists(filePath)) { string girlText = File.ReadAllText(filePath); RequestSaveGirl girl = JsonConvert.DeserializeObject <RequestSaveGirl>(girlText) ?? new RequestSaveGirl(); // todo: remove //Application string rootPage = HttpContext.Current.Server.MapPath("~/Application"); string htmlGirlPagePath = Path.Combine(rootPage, girl.GirlPageName); if (File.Exists(htmlGirlPagePath)) { File.Delete(htmlGirlPagePath); } File.Delete(filePath); } //-------------------------------------- string filePathPhotos = Path.Combine(root, requestDeleteGirl.Id + "_photos.json"); if (File.Exists(filePathPhotos)) { string girlsPhotos = File.ReadAllText(filePathPhotos); IList <PhotoData> photoDataList = JsonConvert.DeserializeObject <List <PhotoData> >(girlsPhotos) ?? new List <PhotoData>(); foreach (PhotoData temp in photoDataList) { string imageFilePath = Path.Combine(root, temp.Image); if (File.Exists(imageFilePath)) { File.Delete(imageFilePath); } string imageMediumFilePath = Path.Combine(root, "medium_" + temp.Image); if (File.Exists(imageMediumFilePath)) { File.Delete(imageMediumFilePath); } } File.Delete(filePathPhotos); } //-------------------------------------- return(this.Ok(true)); }
public IHttpActionResult SaveGirl(RequestSaveGirl requestSaveGirl) { var root = HttpContext.Current.Server.MapPath("~/FileUploads"); string allGirlsFilePath = Path.Combine(root, "allGirls.json"); if (!File.Exists(allGirlsFilePath)) { using (StreamWriter w = File.AppendText(allGirlsFilePath)) { } } string allGirlsText = File.ReadAllText(allGirlsFilePath); IList <PhotoDataGirlName> photoDataGirlNameList = JsonConvert.DeserializeObject <List <PhotoDataGirlName> >(allGirlsText) ?? new List <PhotoDataGirlName>(); if (requestSaveGirl.Id != Guid.Empty) { PhotoDataGirlName findGirl = photoDataGirlNameList.SingleOrDefault(m => m.Id == requestSaveGirl.Id); if (findGirl != null) { findGirl.Girl = requestSaveGirl.Girl; findGirl.GirlPageName = requestSaveGirl.GirlPageName = !string.IsNullOrEmpty(requestSaveGirl.Girl) ? "team_" + requestSaveGirl.Girl.Trim().Replace(" ", "_") + ".html" : requestSaveGirl.Id.ToString() + ".html"; } else { findGirl = new PhotoDataGirlName(); findGirl.Id = requestSaveGirl.Id = Guid.NewGuid(); findGirl.Girl = requestSaveGirl.Girl; findGirl.GirlPageName = requestSaveGirl.GirlPageName = !string.IsNullOrEmpty(requestSaveGirl.Girl) ? "team_" + requestSaveGirl.Girl.Trim().Replace(" ", "_") + ".html" : requestSaveGirl.Id.ToString() + ".html"; photoDataGirlNameList.Add(findGirl); } } else { PhotoDataGirlName findGirl = new PhotoDataGirlName(); findGirl.Id = requestSaveGirl.Id = Guid.NewGuid(); findGirl.Girl = requestSaveGirl.Girl; findGirl.GirlPageName = requestSaveGirl.GirlPageName = !string.IsNullOrEmpty(requestSaveGirl.Girl) ? "team_" + requestSaveGirl.Girl.Trim().Replace(" ", "_") + ".html" : requestSaveGirl.Id.ToString() + ".html"; photoDataGirlNameList.Add(findGirl); } string allGirlsJson = JsonConvert.SerializeObject(photoDataGirlNameList); File.WriteAllText(allGirlsFilePath, allGirlsJson); //-------------------------------------- string filePath = Path.Combine(root, requestSaveGirl.Id + ".json"); string json = JsonConvert.SerializeObject(requestSaveGirl); File.WriteAllText(filePath, json); //-------------------------------------- // todo: remove //Application string rootPage = HttpContext.Current.Server.MapPath("~/Application"); string htmlGirlPagePath = Path.Combine(rootPage, requestSaveGirl.GirlPageName); if (!File.Exists(htmlGirlPagePath) && !string.IsNullOrEmpty(requestSaveGirl.Girl)) { string templateGirlPagePath = Path.Combine(root, "TemplateGirlPage.html"); string templateGirlPage = File.ReadAllText(templateGirlPagePath); var replacedTitle = Regex.Replace(templateGirlPage, "{title}", requestSaveGirl.Girl); var replacedId = Regex.Replace(replacedTitle, "{id}", requestSaveGirl.Id.ToString()); File.WriteAllText(htmlGirlPagePath, replacedId); } //-------------------------------------- SaveGirlResponse saveGirlResponse = new SaveGirlResponse(); saveGirlResponse.success = true; saveGirlResponse.id = requestSaveGirl.Id; return(this.Ok(saveGirlResponse)); }
public IHttpActionResult AddPhoto(PhotoDataRequest photoDataRequest) { var root = HttpContext.Current.Server.MapPath("~/FileUploads"); string allGirlsFilePath = Path.Combine(root, "allGirls.json"); if (!File.Exists(allGirlsFilePath)) { using (StreamWriter w = File.AppendText(allGirlsFilePath)) { } } string allGirlsText = File.ReadAllText(allGirlsFilePath); IList <PhotoDataGirlName> photoDataGirlNameList = JsonConvert.DeserializeObject <List <PhotoDataGirlName> >(allGirlsText) ?? new List <PhotoDataGirlName>(); if (photoDataRequest.Id != Guid.Empty) { PhotoDataGirlName findGirl = photoDataGirlNameList.SingleOrDefault(m => m.Id == photoDataRequest.Id); if (findGirl != null) { findGirl.Image = photoDataRequest.Image; } else { findGirl = new PhotoDataGirlName(); findGirl.Id = photoDataRequest.Id = Guid.NewGuid(); findGirl.Image = photoDataRequest.Image; photoDataGirlNameList.Add(findGirl); } } else { PhotoDataGirlName findGirl = new PhotoDataGirlName(); findGirl.Id = photoDataRequest.Id = Guid.NewGuid(); findGirl.Image = photoDataRequest.Image; photoDataGirlNameList.Add(findGirl); } string allGirlsJson = JsonConvert.SerializeObject(photoDataGirlNameList); File.WriteAllText(allGirlsFilePath, allGirlsJson); //--------------------------------------------------------- string filePath = Path.Combine(root, photoDataRequest.Id + "_photos.json"); if (!File.Exists(filePath)) { using (StreamWriter w = File.AppendText(filePath)) { } } string girlsText = File.ReadAllText(filePath); IList <PhotoData> photoDataList = JsonConvert.DeserializeObject <List <PhotoData> >(girlsText) ?? new List <PhotoData>(); PhotoData photoData = new PhotoData { Image = photoDataRequest.Image, IsMain = true }; photoDataList.Add(photoData); string json = JsonConvert.SerializeObject(photoDataList); File.WriteAllText(filePath, json); PhotoDataResponse photoDataResponse = new PhotoDataResponse(); photoDataResponse.success = true; photoDataResponse.photos = photoDataList; photoDataResponse.id = photoDataRequest.Id; return(Ok(photoDataResponse)); }