public IEnumerable <RouteResource> GetRoutes() { List <RouteResource> routeResList = new List <RouteResource>(); foreach (Route r in usercntxt.Routes .Include("Propietary") .Include("Users") .Include("Car") .Include("StartPoint") .Include("FinishPoint") .ToList()) { List <int> ocupantesIds = new List <int>(); foreach (User u in r.Users) { ocupantesIds.Add(u.UserId); } RouteResource rres = new RouteResource { Id = r.RouteId, CarId = r.Car.CarId, CreatorId = r.Propietary.UserId, StartLocationId = r.StartPoint.LocationId, StartName = r.StartPoint.LocationName, FinishLocationId = r.FinishPoint.LocationId, FinishName = r.FinishPoint.LocationName, Ocupantes = ocupantesIds, }; routeResList.Add(rres); } return(routeResList); }
public void PostRoute([FromBody] RouteResource routeRes, int idUser) { User propietary = usercntxt.Users.Find(idUser); List <User> routeUsers = new List <User>(); foreach (int uId in routeRes.Ocupantes) { routeUsers.Add(usercntxt.Users.Find(uId)); } Route route = new Route { Propietary = propietary, Car = usercntxt.Cars.Find(routeRes.CarId), StartPoint = usercntxt.Locations.Find(routeRes.StartLocationId), FinishPoint = usercntxt.Locations.Find(routeRes.FinishLocationId), Users = routeUsers }; usercntxt.Routes.Add(route); usercntxt.SaveChanges(); }