public ActionResult CreateRoute() { UnitOfWork myUnitOfWork = new UnitOfWork(new VoucherWorldContext()); var user = myUnitOfWork.Repository <MerchantManager>() .Query(u => u.UserName == User.Identity.Name) .Include(u => u.Merchant) .Select() .SingleOrDefault(); var routeModel = new MvcCreateRouteModel(); routeModel.Places = myUnitOfWork.Repository <Place>() .Query(p => p.MerchantId == user.Merchant.Id) .Select() .ToList(); return(View(routeModel)); }
public ActionResult CreateRoute(MvcCreateRouteModel routeModel) { UnitOfWork myUnitOfWork = new UnitOfWork(new VoucherWorldContext()); if (ModelState.IsValid) { var user = myUnitOfWork.Repository <MerchantManager>() .Query(u => u.UserName == User.Identity.Name) .Include(u => u.Merchant) .Select() .SingleOrDefault(); var route = new Route(); route.Name = routeModel.Name; route.IsHidden = route.IsHidden; route.Category = routeModel.Category; route.PlaceIcon = routeModel.PlaceIcon; route.Merchant = user.Merchant; route.ObjectState = ObjectState.Added; myUnitOfWork.Repository <Route>().InsertGraph(route); myUnitOfWork.SaveChanges(); var gift = new Gift(); gift.GiftName = routeModel.Gift.GiftName; gift.StockAmount = routeModel.Gift.StockAmount; gift.Route = route; gift.ObjectState = ObjectState.Added; myUnitOfWork.Repository <Gift>().InsertGraph(gift); myUnitOfWork.SaveChanges(); var question = new OpenEndedQuestion(); question.Content = routeModel.OpenEndedQuestion.Content; question.Route = route; question.ObjectState = ObjectState.Added; myUnitOfWork.Repository <OpenEndedQuestion>().InsertGraph(question); myUnitOfWork.SaveChanges(); foreach (var placeId in routeModel.PlaceIds) { var place = myUnitOfWork.Repository <Place>().Find(placeId); var routePlace = new RoutePlace() { Place = place, Route = route, ObjectState = ObjectState.Added }; myUnitOfWork.Repository <RoutePlace>().InsertGraph(routePlace); } myUnitOfWork.SaveChanges(); int count = myUnitOfWork.Repository <OpenEndedAnswer>() .Query(a => a.NormalUserId == 8 && a.Content == "strawberry") .Select() .ToList() .Count; if (count == 0) { var auser = myUnitOfWork.Repository <NormalUser>().Find(8); var answer = new OpenEndedAnswer { NormalUser = auser, OpenEndedQuestion = question, Content = "strawberry", ObjectState = ObjectState.Added }; myUnitOfWork.Repository <OpenEndedAnswer>().InsertGraph(answer); myUnitOfWork.SaveChanges(); var enrollment = new Enrollment { NormalUser = auser, Route = route, EnrollStatus = EnrollStatus.OnGoing, GiftCodeStatus = GiftCodeStatus.Available, GiftCode = "y5eaimzjg", ObjectState = ObjectState.Added }; myUnitOfWork.Repository <Enrollment>().InsertGraph(enrollment); myUnitOfWork.SaveChanges(); } return(RedirectToAction("RouteList")); } return(View()); }