// GET api/trip/5 - get trip by tripId public TripSM Get(int id) { //Get Id of current User var userId = User.Identity.GetUserId(); //Get Data From DB var query = from t in db.Trips where t.Id == id select t; var result = query.FirstOrDefault <Trip>(); TripSM trip = null; if (result != null) { //Check User Credentials IdentityHelper helper = new IdentityHelper(); //check if private if (result.IsPrivate == true && helper.isOwner(userId, result.UserId) == false) { return(null); } var isAuthorized = helper.isFriendOrOwner(userId, result.UserId); if (isAuthorized == false) { return(null); } //Bind TrimSM Object and return trip = new TripSM(); trip.Title = result.Title; trip.TripId = result.Id; trip.UserId = result.UserId; trip.isPrivate = result.IsPrivate; //Group Coordinates List <Photo> groupPhotos = new List <Photo>(result.Photos); CoordinateHelper cHelper = new CoordinateHelper(); result.Photos = cHelper.GroupPhotosByCoordinates(groupPhotos); foreach (var item in result.Photos) { trip.Coordinates.Add(new Coordinate { Latitude = item.Latitude, Longitude = item.Longitude }); } } return(trip); }
// GET api/trip - Get latest Trip of active user public TripSM Get() { //Get Id of current User var userId = User.Identity.GetUserId(); //Get Data From DB var query = from t in db.Trips where t.UserId == userId orderby t.CreationDate descending select t; var result = query.FirstOrDefault <Trip>(); TripSM lastTrip = null; if (result != null) { //Bind TrimSM Object and return lastTrip = new TripSM(); lastTrip.Title = result.Title; lastTrip.TripId = result.Id; lastTrip.UserId = result.UserId; lastTrip.isPrivate = result.IsPrivate; //Group Coordinates List <Photo> groupPhotos = new List <Photo>(result.Photos); CoordinateHelper cHelper = new CoordinateHelper(); result.Photos = cHelper.GroupPhotosByCoordinates(groupPhotos); foreach (var item in result.Photos) { lastTrip.Coordinates.Add(new Coordinate { Latitude = item.Latitude, Longitude = item.Longitude }); } } return(lastTrip); }
// GET api/trips/abcd-dfdf-dfdf-dfd - get trips by userId public IEnumerable <TripSM> Get(string id) { //Get Id of current User var requestUserId = User.Identity.GetUserId(); //Fixy Frosy if (id.Equals("xxx")) { id = requestUserId; } //Get Data From DB var query = from t in db.Trips where t.UserId == id select t; var result = query.ToList <Trip>(); List <TripSM> trips = null; if (result != null) { //Check User Credentials IdentityHelper helper = new IdentityHelper(); var isAuthorized = helper.isFriendOrOwner(requestUserId, id); if (isAuthorized == false) { return(null); } //Bind TrimSM Object and return trips = new List <TripSM>(); foreach (var item in result) { //Check if trip is private if (item.IsPrivate == true && helper.isOwner(requestUserId, item.UserId) == false) { continue; } TripSM trip = new TripSM(); trip.Title = item.Title; trip.TripId = item.Id; trip.UserId = item.UserId; trip.isPrivate = item.IsPrivate; //Group Coordinates List <Photo> groupPhotos = new List <Photo>(item.Photos); CoordinateHelper cHelper = new CoordinateHelper(); item.Photos = cHelper.GroupPhotosByCoordinates(groupPhotos); foreach (var photo in item.Photos) { trip.Coordinates.Add(new Coordinate { Latitude = photo.Latitude, Longitude = photo.Longitude }); } trips.Add(trip); } } return(trips); }