public async Task <WishList> RemoveLocation(int location_id) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot interact with their own wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var wishList = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (wishList == null) { throw new ArgumentException("Something went wrong! This user doesn't have a wishlist!"); } var location = await a.Locations.Where(x => x.Id == location_id && x.WishlistId == wishList.Id && x.TripId == null).FirstOrDefaultAsync(); a.Remove(location); await a.SaveChangesAsync(); return(wishList); } }
public async Task <SimpleLocation[]> GetSimpleWishlistLocations() { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot interact with their own wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var wishlist = await a.WishList .Include(x => x.Locations) .Where(x => x.User.Auth == httpContext.GetCurrentAuth()) .FirstOrDefaultAsync(); List <SimpleLocation> sl = new List <SimpleLocation>(); foreach (var b in wishlist.Locations) { sl.Add(new SimpleLocation() { Latitude = b.Lang, Longitude = b.Long }); } return(sl.ToArray()); } }
public async Task SetOriginDestination(int locationId, string od) { if (od != "ORIGIN" && od != "DESTINATION") { throw new ArgumentException("Origin or destination weren't in the correct format"); } if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot intact with their wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var wishlist = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); var location = wishlist.Locations.Where(x => x.Id == locationId).First(); //Checks if there is already a location assigned as an origin or destination and removes it var locationWithSameStatus = wishlist.Locations.Where(x => x.Origin_Destination == od).FirstOrDefault(); if (locationWithSameStatus != null) { locationWithSameStatus.Origin_Destination = null; } location.Origin_Destination = od; await a.SaveChangesAsync(); } }
public async Task <UserTrips[]> DeleteTrip(int trip_id) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot create tickets!"); } using (var a = contextFactory.CreateDbContext()) { var trip = await a.UserTrips.Include(x => x.User).Where(x => x.Id == trip_id && x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (trip == null) { throw new ArgumentException("There isn't a trip with that id!"); } var locations = await a.Locations.Where(x => x.TripId == trip.Id && x.WishlistId == null).ToListAsync(); a.RemoveRange(locations); a.Remove(trip); await a.SaveChangesAsync(); var trips = await a.UserTrips.Include(x => x.User).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).ToArrayAsync(); return(trips); } }
public async Task <Locations> AddLocation(int trip_id, Locations location) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot add locations!"); } using (var a = contextFactory.CreateDbContext()) { var trip = await a.UserTrips.Where(x => x.Id == trip_id && x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (trip == null) { throw new ArgumentException("There isn't a trip with that id!"); } //Assign values to make a unique location location.WishlistId = null; location.TripId = trip.Id; trip.Locations.Add(location); await a.SaveChangesAsync(); return(location); } }
public async Task <SupportTicket> GetTicket(int id) { int requester_id = await base.GetUserId(httpContext.GetCurrentAuth()); using (var _context = contextFactory.CreateDbContext()) { var ticket = await _context.SupportTicket .Where(x => x.Id == id) .Include(x => x.TicketChat) .FirstOrDefaultAsync(); foreach (var a in ticket.TicketChat) { if (a.CreatorId == requester_id) { a.IsCurrentUser = true; } } if (CurrentExtensions.HasPrivileges(ticket.UserId, httpContext, contextFactory)) { return(ticket); } throw new ArgumentException("Access forbidden!"); } }
public async Task <bool> CheckOriginDestination() { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot intact with their wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var wishlist = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (wishlist == null) { throw new ArgumentException("Something went wrong. User doesn't have a wishlist"); } var origin = wishlist.Locations.Any(x => x.Origin_Destination == "ORIGIN"); var destination = wishlist.Locations.Any(x => x.Origin_Destination == "DESTINATION"); if (origin && destination) { return(true); } throw new ArgumentException("You need to have an origin and destination location before you proceed!"); } }
public async Task <UserTrips> CreateTrip(string name, string transportation) { if (name == null) { throw new ArgumentException("The name of the tirp can't be empty!"); } transportation.CheckTransportation(); if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot intact with their wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var currentAuth = httpContext.GetCurrentAuth(); var duplicateName = await a.UserTrips.Include(x => x.User).Where(x => x.Name == name && x.User.Auth == currentAuth).FirstOrDefaultAsync(); if (duplicateName != null) { throw new ArgumentException("There is already a trip with that name. Please choose another."); } var wishList = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == currentAuth).FirstOrDefaultAsync(); if (wishList == null) { throw new ArgumentException("Something went wrong! This user doesn't have a wishlist!"); } if (wishList.Locations.Count < 2) { throw new ArgumentException("You must have at least 2 locations in your wishlist!"); } var trip = new UserTrips() { Transportation = transportation, Name = name, UserId = wishList.UserId }; await a.AddAsync(trip); await a.SaveChangesAsync(); foreach (var l in wishList.Locations) { l.TripId = trip.Id; l.WishlistId = null; } await a.SaveChangesAsync(); return(trip); } }
public async Task <SupportTicket> CreateTicket(SupportTicket entity) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot create tickets!"); } var id = await base.GetUserId(httpContext.GetCurrentAuth()); entity.UserId = id; return(await base.Create(entity)); }
public async Task <WishList> GetWishlist() { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot interact with their own wishlist!"); } using (var a = contextFactory.CreateDbContext()) { return(await a.WishList .Include(x => x.Locations) .Where(x => x.User.Auth == httpContext.GetCurrentAuth()) .FirstOrDefaultAsync()); } }
public async Task <List <UserTrips> > GetUserTrips() { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot interact with their own trips!"); } using (var a = contextFactory.CreateDbContext()) { var z = await a.UserTrips .Include(x => x.Locations) .Include(x => x.User) .Where(x => x.User.Auth == httpContext.GetCurrentAuth()) .ToListAsync(); return(z); } }
public async Task <Users> ChangeAddress(Users entity) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot change their address!"); } using (var _context = contextFactory.CreateDbContext()) { var result = await _context.Users.Where(x => x.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); result.City = entity.City; result.Country = entity.Country; await _context.SaveChangesAsync(); return(result); } }
public async Task <Users> GetUserInfo(int id) { using (var _context = contextFactory.CreateDbContext()) { var user = await _context.Users .Include(x => x.SupportTicket) .Include(x => x.WishList) .Include(x => x.UserKeywords) .Include(x => x.UserTrips) .FirstOrDefaultAsync(x => x.Id == id); if (CurrentExtensions.HasPrivileges(user.Id, httpContext, contextFactory)) { return(user); } throw new ArgumentException("Access forbidden!"); } }
public async Task <WishList> AddLocation(Locations location) { if (location.Lang == 0 || location.Long == 0 || location.Name == null || location.PlaceId == null) { throw new ArgumentException("The location data was empty"); } if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot intact with their wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var duplicate = await a.Locations.Where(x => x.PlaceId == location.PlaceId && x.TripId == null).FirstOrDefaultAsync(); if (duplicate != null) { throw new ArgumentException("This location is already in your wishlist"); } var wishList = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (wishList == null) { throw new ArgumentException("Something went wrong! This user doesn't have a wishlist!"); } //Assign values to make a unique location location.WishlistId = wishList.Id; location.TripId = null; wishList.Locations.Add(location); await a.SaveChangesAsync(); return(wishList); } }
public async Task <GoogleDirectionsObject[]> GetWaypointsFromWishlist() { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot intact with their wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var wishlist = await a.WishList.Include(x => x.User).Include(x => x.Locations).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (wishlist == null) { throw new ArgumentException("Something went wrong. User doesn't have a wishlist"); } var waypoints = await googleService.DirectionsServiceTest("Moscow", "Sofia", wishlist.Locations.Select(x => x.PlaceId).ToArray()); return(waypoints); } }
public async Task <SimpleLocation[]> GetSimpleTripLocations(int tripId) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot interact with their own wishlist!"); } using (var a = contextFactory.CreateDbContext()) { var trip = await a.UserTrips .Include(x => x.Locations) .Where(x => x.User.Auth == httpContext.GetCurrentAuth() && x.Id == tripId) .FirstOrDefaultAsync(); if (trip == null) { throw new ArgumentException("There isn't a trip with that id for this user"); } List <SimpleLocation> sl = new List <SimpleLocation>(); foreach (var b in trip.Locations) { if (b.Origin_Destination == "ORIGIN" || b.Origin_Destination == "DESTINATION") { sl.Add(new SimpleLocation() { Latitude = b.Lang, Longitude = b.Long, Origin_Destination = b.Origin_Destination }); continue; } sl.Add(new SimpleLocation() { Latitude = b.Lang, Longitude = b.Long }); } return(sl.ToArray()); } }
public async Task <Locations> RemoveLocation(int trip_id, int location_id) { if (await CurrentExtensions.RestrictAdministratorResource(contextFactory, httpContext)) { throw new ArgumentException("Administrators cannot create tickets!"); } using (var a = contextFactory.CreateDbContext()) { var trip = await a.UserTrips.Where(x => x.Id == trip_id).Where(x => x.User.Auth == httpContext.GetCurrentAuth()).FirstOrDefaultAsync(); if (trip == null) { throw new ArgumentException("There isn't a trip with that id!"); } var location = await a.Locations.Where(x => x.Id == location_id && x.WishlistId == null && x.TripId == trip_id).FirstOrDefaultAsync(); a.Remove(location); return(location); } }