public HttpResponseMessage AnswerOpenEndedQuestion(int userId, int routeId, string answer) { UnitOfWork myUnitOfWork = new UnitOfWork(new VoucherWorldContext()); var user = myUnitOfWork.Repository <NormalUser>().Find(userId); var question = myUnitOfWork.Repository <OpenEndedQuestion>().Find(routeId); var openEndedAnswer = new OpenEndedAnswer() { NormalUser = user, OpenEndedQuestion = question, Content = answer, ObjectState = ObjectState.Added }; myUnitOfWork.Repository <OpenEndedAnswer>().InsertGraph(openEndedAnswer); myUnitOfWork.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, answer)); }
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()); }