public ActionResult Matching(int id) { MeetUp meetUp = new MeetUp(); List<Match> matches = (List<Match>)TempData["Matches"]; meetUp.Guide = matches[id].Guide; meetUp.Traveler = matches[id].Traveler; meetUp.StartDate = (DateTime)TempData["Start"]; meetUp.FinishDate = (DateTime)TempData["End"]; meetUp.City = new City { Name = (string)TempData["City"] }; meetUp.City_Name = meetUp.City.Name; meetUp.TravelerState = RequestState.Sent; meetUp.GuideState = RequestState.Received; var client = new SMARestClient("MeetUpService.svc"); client.AuthToken = (string)Session["auth_token"]; MeetUp createdMeetUp = client.Post<MeetUp>("meetups/", meetUp); if (createdMeetUp == null) { return View(); } else { return RedirectToAction("Index", "MeetUp", new { area = "" }); } }
public MeetUp Create(MeetUp meetup) { User authUser = auth.Authorize(WebOperationContext.Current.IncomingRequest); if (authUser.Id != meetup.Traveler.Id) throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized); MeetUp result = null; using (var muTransaction = meetUpDA.BeginTransaction()) { try { if (IsGuideAvailable(meetup)) { meetUpDA.Insert(meetup); meetUpDA.SaveChanges(); muTransaction.Commit(); result = meetUpDA.GetOneByID(meetup.Id); } } catch(Exception ex) { Console.WriteLine(ex.ToString()); muTransaction.Rollback(); } } return result; }
public MeetUp Update(string id, MeetUp meetUp) { User authUser = auth.Authorize(WebOperationContext.Current.IncomingRequest); if (authUser.Id != meetUp.Traveler.Id && authUser.Id != meetUp.Guide.Id) throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized); meetUp.Id = Convert.ToInt32(id); meetUpDA.Update(meetUp); meetUpDA.SaveChanges(); return meetUpDA.GetOneByID(meetUp.Id); }
//Utility methods private bool IsGuideAvailable(MeetUp meetup) { var gMeetUps = meetUpDA.GetAll().Where(m => m.Guide.Id == meetup.Guide.Id).ToArray(); if (gMeetUps.Length == 0) return true; bool isAvailable = true; Parallel.For(0, gMeetUps.Length, i => { bool datesOverlap = DatesOverlap(meetup.StartDate, meetup.FinishDate, gMeetUps[i].StartDate, gMeetUps[i].FinishDate); if(datesOverlap) { isAvailable = false; } }); return isAvailable; }