public ActionResult AddTrip(Trip t) { TripContext ts = new TripContext(); if (t.carAvailable == false) { t.vacant_seats = -1; } AspNetUsersContext us = new AspNetUsersContext(); AspNetUsers asp = us.AspNetUsers.Single(a => a.Email == User.Identity.Name); t.created_by = asp.Id; ts.Trips.Add(t); ts.SaveChanges(); TripGroupContext t_gc = new TripGroupContext(); TripGroup tg = new TripGroup(); tg.Id = t.id; tg.People = User.Identity.Name; tg.TripAdmin = t.carAvailable; t_gc.TripGroups.Add(tg); t_gc.SaveChanges(); //if similar trip exists send mail to those people List <Trip> similarTrips = new List <Trip>(); similarTrips = ts.Trips.Where(s => s.source == t.source && s.destination == t.destination).ToList(); foreach (var s in similarTrips) { AspNetUsers aspnetusers = us.AspNetUsers.Single(u => u.Id == s.created_by); if (aspnetusers.Email != User.Identity.Name) { TripController tp = new TripController(); tp.SMTPSendEmail(aspnetusers.Email, User.Identity.Name + " has created a new trip similar to yours", "A trip similar to yours has been created on CarpoolManagementSystem"); } } return(RedirectToAction("TripDetails/" + t.id, "Trip")); }
public string JoinTrip(string id) { if (User.Identity.IsAuthenticated) { try { TripGroupContext g = new TripGroupContext(); var tg = g.TripGroups.Where(b => b.Id.ToString() == id).ToArray(); foreach (var d in tg) { if (d.People == User.Identity.Name) { return("2"); } } TripGroup t_g = new TripGroup(); t_g.Id = Int32.Parse(id); t_g.People = User.Identity.Name; t_g.TripAdmin = false; g.TripGroups.Add(t_g); g.SaveChanges(); TripContext t_s = new TripContext(); Trip t = t_s.Trips.Single(d => d.id.ToString() == id); t.vacant_seats = t.vacant_seats - 1; t_s.SaveChanges(); return("3"); } catch { return("3"); } } else { return("1"); } }