public async Task <HttpResult> ToggleGymStatus([FromRoute] int gymID) { try { if (!Functions.AdminLoggedIn(Request, out _)) { throw new Exception("Not logged in!"); } GymFinderGym updating = db.GymFinderGym.Find(gymID); if (updating == null) { throw new Exception("No gym found!"); } updating.Status = updating.Status == (int)Enums.GymStatus.Live ? (int)Enums.GymStatus.Pending : (int)Enums.GymStatus.Live; updating.ModifiedDate = DateTime.Now; db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await db.SaveChangesAsync(); return(new HttpResult(true, updating.Status == (int)Enums.GymStatus.Live, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> DeleteGymFinderGym([FromRoute] int gymID) { try { if (!Functions.AdminLoggedIn(Request, out _)) { throw new Exception("Not logged in!"); } GymFinderGym deleting = await db.GymFinderGym.FindAsync(gymID); DeleteGymLogo(deleting.Id); DeleteAllGymImages(deleting.Id); //Delete db entry db.GymFinderGym.Remove(deleting); await db.SaveChangesAsync(); return(new HttpResult(true, null, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> AddEditGymReview([FromBody] GymReview review) { try { if (!Functions.UserLoggedIn(Request, out User user) && !Functions.AdminLoggedIn(Request, out _)) { throw new Exception("Not logged in!"); } GymFinderGym gym = await db.GymFinderGym.FindAsync(review.GymId); if (gym == null) { throw new Exception("Gym not found!"); } DateTime now = DateTime.Now; if (review.Id > 0) { GymReview updating = db.GymReview.Find(review.Id); Functions.CheckNull(updating); updating.ModifiedDate = now; updating.Title = review.Title; updating.MainReview = review.MainReview; updating.GymId = review.GymId; updating.GoodPoints = review.GoodPoints; updating.BadPoints = review.BadPoints; updating.Rating = review.Rating; db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await db.SaveChangesAsync(); } else { review.CreationDate = now; review.ModifiedDate = now; review.ReviewerId = user.Id; db.GymReview.Add(review); await db.SaveChangesAsync(); } //updating gym rating. gym.AverageRating = db.GymReview.Where(x => x.GymId == gym.Id).Average(g => g.Rating); db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await db.SaveChangesAsync(); return(new HttpResult(true, review, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> DeleteGymReview([FromRoute] int postID) { try { //if (!Functions.AdminLoggedIn(Request, out _)) // throw new Exception("Not logged in!"); GymReview deleting = await db.GymReview.FindAsync(postID); if (deleting == null) { throw new Exception("Post not found!"); } bool adminLoggedIn = Functions.AdminLoggedIn(Request, out _); bool userLoggedIn = Functions.UserLoggedIn(Request, out User editor); if ((!adminLoggedIn && !userLoggedIn) || //no user logged in (userLoggedIn && editor.Id != deleting.ReviewerId)) //or user logged in but does not own the review { throw new Exception("Not logged in!"); } //update coach rating GymFinderGym gym = await db.GymFinderGym.FindAsync(deleting.GymId); IQueryable <GymReview> remainingReviews = db.GymReview.Where(x => x.GymId == gym.Id && x.Id != deleting.Id); if (remainingReviews.Count() > 0) { gym.AverageRating = remainingReviews.Average(c => c.Rating); } else { gym.AverageRating = 0d; } db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; db.GymReview.Remove(deleting); await db.SaveChangesAsync(); return(new HttpResult(true, null, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <IActionResult> GymShareImage([FromQuery] int gymid) { GymFinderGym gym = await db.GymFinderGym.FindAsync(gymid); string logoPath = string.IsNullOrEmpty(gym.ImageLocationLogo) ? Path.Combine(Environment.CurrentDirectory, "wwwroot", "dist", "images", "gymfinder", "default-gym.svg") : Path.Combine(Environment.CurrentDirectory, "wwwroot", "dist", "uploads", "images", "gymfinder", "logos", gym.ImageLocationLogo.Split('/').Last()); string backgroundPath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "dist", "images", "blank-background.png"); using (MagickImage logoImage = new MagickImage(logoPath)) { using (MagickImage backgroundIamge = new MagickImage(backgroundPath)) { logoImage.Resize(250, 250); //backgroundIamge.Resize(680, 400); backgroundIamge.Composite(logoImage, 230, 75, CompositeOperator.Over); return(File(backgroundIamge.ToByteArray(), "image/jpg")); } } }
public async Task <HttpResult> AddUpdateGymLogo([FromQuery] int gymID, [FromQuery] UserController.Crop crop) { DateTime now = DateTime.Now; GymFinderGym gym = await db.GymFinderGym.FindAsync(gymID); if (Request.Form.Files.Count > 0 && Request.Form.Files[0].FileName != gym.ImageLocationLogo && Request.Form.Files[0].Length > 0) { var imageFile = Request.Form.Files[0]; string appPath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "dist", "uploads", "images", "gymfinder", "logos"); byte[] postedImg = Functions.ConvertToBytes(imageFile); if (crop.Width > 0 && crop.Height > 0) { postedImg = Functions.CropImage(postedImg, (int)crop.X, (int)crop.Y, (int)crop.Width, (int)crop.Height); } Functions.ReduceImageQuality(ref postedImg, 50000); MagickImage finalImage = new MagickImage(postedImg); finalImage.Format = MagickFormat.Jpg; string fileName = ""; using (MemoryStream ms = new MemoryStream()) { finalImage.Write(ms); if (!Directory.Exists(appPath)) { Directory.CreateDirectory(appPath); } fileName = string.Format("{0}_gymlogo_{1}.jpg", gym.Id, now.Ticks); string filePath = Path.Combine(appPath, fileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { ms.Position = 0; ms.CopyTo(fileStream); } } gym.ImageLocationLogo = string.Format("/dist/uploads/images/gymfinder/logos/{0}", fileName); db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _ = await db.SaveChangesAsync(); //update to latest entry, otherwise logo/images may not be correct as the two upload almost simultaneously gym = db.GymFinderGym.Find(gym.Id); } else if (Request.Form.Files[0].FileName == "delete") { DeleteGymLogo(gym.Id); gym.ImageLocationLogo = string.Empty; db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _ = await db.SaveChangesAsync(); } return(new HttpResult(true, gym, "")); }
public async Task <HttpResult> AddUpdateGymFinderGym([FromBody] GymFinderGym gym) { try { //User editor = db.User.Find(gym.OwnerId); bool userLoggedIn = Functions.UserLoggedIn(Request, out User editor); bool adminLoggedIn = Functions.AdminLoggedIn(Request, out _); //if ((gym.Id > 0 && !adminLoggedIn ) //gym belongs to someone // && ( // (!adminLoggedIn && !userLoggedIn) //no user logged in // || (userLoggedIn && editor.Id != gym.OwnerId) ) //or user logged in but does not own the gym // ) //{ // throw new Exception("Not logged in!"); //} if (gym.Id > 0 && !userLoggedIn && !adminLoggedIn) { //gym belongs to someone but no user logged in throw new Exception("Not logged in!"); } else if (gym.Id > 0 && !adminLoggedIn && userLoggedIn && editor.Id != gym.OwnerId) { //user is not admin, and gym does not belong to this user throw new Exception("Not logged in!"); } //all okay, gym belongs to user, or user is admin. DateTime now = DateTime.Now; CityGeo city = db.CityGeo.Find(gym.LocationCityId); gym.LocationCountryId = db.CountryGeo.FirstOrDefault(x => x.CountryName == city.CountryName).Id; if (gym.Id > 1) { GymFinderGym updating = db.GymFinderGym.Find(gym.Id); Functions.CheckNull(updating); if (!string.IsNullOrEmpty(gym.Website) && gym.Website.StartsWith("www")) { gym.Website = "https://" + gym.Website; } if (!string.IsNullOrEmpty(gym.Youtube) && gym.Youtube.StartsWith("www")) { gym.Youtube = "https://" + gym.Youtube; } if (!string.IsNullOrEmpty(gym.Facebook) && gym.Facebook.StartsWith("www")) { gym.Facebook = "https://" + gym.Facebook; } if (!string.IsNullOrEmpty(gym.Twitter) && gym.Twitter.StartsWith("www")) { gym.Twitter = "https://" + gym.Twitter; } if (!string.IsNullOrEmpty(gym.Instagram) && gym.Instagram.StartsWith("www")) { gym.Instagram = "https://" + gym.Instagram; } if (!string.IsNullOrEmpty(gym.Linkedin) && gym.Linkedin.StartsWith("www")) { gym.Linkedin = "https://" + gym.Instagram; } updating.Phone = gym.Phone; updating.Website = gym.Website; updating.Facebook = gym.Facebook; updating.Twitter = gym.Twitter; updating.Instagram = gym.Instagram; updating.AverageRating = gym.AverageRating; updating.Cafe = gym.Cafe; updating.CardioMachines = gym.CardioMachines; updating.ChangingRooms = gym.ChangingRooms; updating.ClassesAvailable = gym.ClassesAvailable; updating.Crossfit = gym.Crossfit; updating.Description = gym.Description; updating.FreeWeightsBarsPlates = gym.FreeWeightsBarsPlates; updating.FreeWeightsDumbbells = gym.FreeWeightsDumbbells; updating.LocationCityId = gym.LocationCityId; updating.LocationCountryId = gym.LocationCountryId; updating.LocationLat = (double)city.Latitude; updating.LocationLong = (double)city.Longitude; updating.MembersOnly = gym.MembersOnly; updating.Name = gym.Name; updating.NoMembershipRequired = gym.NoMembershipRequired; updating.OlympicLifting = gym.OlympicLifting; updating.Other = gym.Other; updating.Physio = gym.Physio; updating.Powerlifting = gym.Powerlifting; updating.ResistanceMachines = gym.ResistanceMachines; updating.Sauna = gym.Sauna; updating.OwnerId = gym.OwnerId; updating.StreetAddress = gym.StreetAddress; updating.SwimmingPool = gym.SwimmingPool; updating.Toilets = gym.Toilets; updating.Lockers = gym.Lockers; updating.Strongman = gym.Strongman; updating.TwentyFourHour = gym.TwentyFourHour; updating.VendingMachine = gym.VendingMachine; updating.LocationCityName = city.CityName; updating.LocationCountryName = city.CountryName; updating.Status = gym.Status; updating.ModifiedDate = now; updating.Whatsapp = gym.Whatsapp; updating.Linkedin = gym.Linkedin; updating.GooglePlus = gym.GooglePlus; updating.Snapchat = gym.Snapchat; updating.Skype = gym.Skype; updating.Youtube = gym.Youtube; db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await db.SaveChangesAsync(); } else { gym.CreationDate = now; gym.ModifiedDate = now; gym.LocationLat = (double)city.Latitude; gym.LocationLong = (double)city.Longitude; gym.LocationCityName = city.CityName; gym.LocationCountryName = city.CountryName; gym.Status = (int)Enums.GymStatus.Pending; gym.AverageRating = 5d; db.GymFinderGym.Add(gym); await db.SaveChangesAsync(); } return(new HttpResult(true, gym, "")); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } }
public async Task <HttpResult> AddUpdateGymImages([FromQuery] int gymID) { GymFinderGym gym = await db.GymFinderGym.FindAsync(gymID); if (gym == null) { return(new HttpResult(false, null, "Gym not found!")); } if (Request.Form.Files.Count > 0) { try { DateTime now = DateTime.Now; string appPath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "dist", "uploads", "images", "gymfinder", "gyms"); for (int i = 0; i < Request.Form.Files.Count(); i++) { var imageFile = Request.Form.Files[i]; List <string> currentImgLocations = new List <string> { gym.ImageLocation1, gym.ImageLocation2, gym.ImageLocation3 }; if (currentImgLocations.Any(x => !string.IsNullOrEmpty(x) && x == imageFile.Name)) { continue; } //skip files that are unchanged int imageNumber = i + 1; // image number 1/2/3 etc matches client side image store if (imageFile.Length == 0) {//delete files that are gone DeleteSingleGymImage(gym.Id, imageNumber); switch (imageNumber) { case 1: gym.ImageLocation1 = string.Empty; break; case 2: gym.ImageLocation2 = string.Empty; break; case 3: gym.ImageLocation3 = string.Empty; break; } db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _ = await db.SaveChangesAsync(); continue; } byte[] postedImg = Functions.ConvertToBytes(imageFile); Functions.ReduceImageQuality(ref postedImg, 50000); MagickImage finalImage = new MagickImage(postedImg); finalImage.Format = MagickFormat.Jpg; string fileName = ""; using (MemoryStream ms = new MemoryStream()) { finalImage.Write(ms); if (!Directory.Exists(appPath)) { Directory.CreateDirectory(appPath); } fileName = string.Format("{0}_gymimage_{1}_{2}.jpg", gym.Id, imageNumber, now.Ticks);//add ticks so image name is changed upon replacement (so no issue with browser caching) string filePath = Path.Combine(appPath, fileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { ms.Position = 0; ms.CopyTo(fileStream); } } switch (imageNumber) { case 1: gym.ImageLocation1 = string.Format("/dist/uploads/images/gymfinder/gyms/{0}", fileName); break; case 2: gym.ImageLocation2 = string.Format("/dist/uploads/images/gymfinder/gyms/{0}", fileName);; break; case 3: gym.ImageLocation3 = string.Format("/dist/uploads/images/gymfinder/gyms/{0}", fileName);; break; } } db.Entry(gym).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _ = await db.SaveChangesAsync(); //update to latest entry, otherwise logo/images may not be correct as the two upload simultaneously gym = db.GymFinderGym.Find(gym.Id); } catch (Exception e) { return(new HttpResult(false, null, Functions.ErrorMessage(e))); } } return(new HttpResult(true, gym, "")); }