/// <summary> /// Saves the service request to the db and updates the version /// </summary> /// <param name="service_request">service_request to be edited</param> /// <returns>boolean</returns> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="ArgumentNullException"></exception> public Boolean edit(service_request serviceRequest) { ServiceRequestIM im = new ServiceRequestIM(db); service_request dbVersion = im.find(serviceRequest.id); if (dbVersion.version == serviceRequest.version) { ((IObjectContextAdapter)db).ObjectContext.Detach(dbVersion); db.Entry(serviceRequest).State = EntityState.Modified; serviceRequest.version = serviceRequest.version + 1; db.SaveChanges(); return true; } return false; }
public ActionResult CancelConfirmed(service_request service_request) { try { service_request.status = (int)ServiceRequestUtil.ServiceRequestStatus.CANCELLED; if (ModelState.IsValid) { if (om.edit(service_request)) { return RedirectToAction("Index"); } else { ViewBag.Error = Global.ServiceRequestLockError; } } else { ViewBag.Error = Global.ServerError; } } catch (Exception e) { if (e is InvalidOperationException || e is ArgumentNullException) { ViewBag.Error = Global.ServerError; } else { throw e; } } return View(service_request); }
/// <summary> /// This method checks if a service request for a given table is already open. /// </summary> /// <param name="tableID">The table identifier</param> /// <returns>True if a request already exists and false if the request does not exist.</returns> private bool ServiceRequestExists(service_request request) { if(request.status != (int)ServiceRequestUtil.ServiceRequestStatus.OPEN) { return false; } ICollection<service_request> requests = im.FindByOpenRequestByTable((int)request.table_id); if (requests.Count <= 0 || (requests.Count == 1 && requests.ElementAt(0).id == request.id)) { return false; } return true; }
public ActionResult Edit(service_request service_request) { try { if(service_request.table_id == null) { ViewBag.Error = Global.ServiceRequestTableNullError; } else if (!ServiceRequestExists(service_request) && ModelState.IsValid) { if (om.edit(service_request)) { return RedirectToAction("Index"); } else { ViewBag.Error = Global.ServiceRequestLockError; } } else { ViewBag.Error = Global.ServiceRequestDuplicateRequestError; } //Make list of statuses from enum var statuses = from ServiceRequestUtil.ServiceRequestStatus s in Enum.GetValues(typeof(ServiceRequestUtil.ServiceRequestStatus)) select new { Value = (int)s, Name = s.ToString() }; ViewData["status"] = new SelectList(statuses, "Value", "Name", service_request.status); ViewBag.table_id = new SelectList(CreateTableList(), "id", "name", service_request.table_id); } catch (Exception e) { if (e is InvalidOperationException || e is ArgumentNullException) { ViewBag.Error = Global.ServerError; } else { throw e; } } return View(service_request); }
public ActionResult Create(service_request service_request) { try { user authUser = Util.User.UserUtil.getAuthenticatedUser(Request); //Assign values to request attributes if (authUser.user_role == (int)SiteRoles.Customer && authUser.current_table_id != null) { service_request.table_id = authUser.current_table_id; } service_request.created = DateTime.Now; service_request.status = (int)ServiceRequestUtil.ServiceRequestStatus.OPEN; service_request.version = 0; //Table should never be null. if (service_request.table_id == null) { ViewBag.Error = Global.ServiceRequestTableNullError; } else if (!ServiceRequestExists(service_request) && ModelState.IsValid) { if (om.Create(service_request)) { if (authUser.user_role != (int)SiteRoles.Customer) { HttpContext.Session["message"] = Global.ServiceRequestAdminSuccess; return RedirectToAction("Index"); } HttpContext.Session["message"] = Global.ServiceRequestSuccess; return RedirectToAction("Index", "Home"); } } else { ViewBag.Error = Global.ServiceRequestDuplicateRequestError; } ViewBag.table_id = new SelectList(CreateTableList(), "id", "name", service_request.table_id); } catch (Exception e) { if (e is InvalidOperationException || e is ArgumentNullException) { ViewBag.Error = Global.ServerError; } else { throw e; } } return View(service_request); }
public void MyTestInitialize() { //Add test data (order specific) restaurant1 = testDatabase.AddRestaurant(); table1 = testDatabase.AddTable(restaurant1); order1 = testDatabase.AddOrder(table1); serviceRequest1 = testDatabase.AddServiceRequest(table1); }
public void MyTestInitialize() { //Add test data (order specific) restaurant1 = testDatabase.AddRestaurant(); table1 = testDatabase.AddTable(restaurant1); table2 = new table(); user1 = testDatabase.AddUser("*****@*****.**", table1, (int)SiteRoles.Restaurant); order1 = testDatabase.AddOrder(table1); request1 = testDatabase.AddServiceRequest(table1); restaurantUser1 = testDatabase.AddRestaurantUser(user1, restaurant1); //Session db = new touch_for_foodEntities(); target = new TableController(); Session session = new Session(db, target); session.simulateLogin(user1.username, user1.password); }
/// <summary> /// Writes a service_request object to the database /// </summary> /// <param name="sr">The service_request object to write</param> /// <returns>True if successful, false otherwise</returns> /// <exception cref="InvalidOperationException"></exception> public bool Create(service_request sr) { db.service_request.Add(sr); return (db.SaveChanges() == 1); }
/// <summary> /// Removes a service request item from the database. /// </summary> /// <param name="serviceRequest">The service request to be removed.</param> public void RemoveServiceRequest(service_request serviceRequest) { db = new touch_for_foodEntities(); if (db.service_request.Find(serviceRequest.id) != null) { db.service_request.Remove(db.service_request.Find(serviceRequest.id)); db.SaveChanges(); } db.Dispose(); }
/// <summary> /// Creates an entry of type service request in the database. /// </summary> /// <param name="tableEntity">the table the service request is associated to.</param> /// <returns>The created service request entity.</returns> public service_request AddServiceRequest(table tableEntity) { //Initialise db = new touch_for_foodEntities(); service_request testServiceRequest = new service_request(); //Set Attributes testServiceRequest = new service_request(); testServiceRequest.note = "UnitTest"; testServiceRequest.created = DateTime.Now; testServiceRequest.status = (int)ServiceRequestUtil.ServiceRequestStatus.OPEN; testServiceRequest.version = 0; testServiceRequest.table_id = tableEntity.id; //Save db.service_request.Add(testServiceRequest); db.SaveChanges(); db.Dispose(); return testServiceRequest; }
public void MyTestInitialize() { request1 = testDatabase.AddServiceRequest(table1); //Session db = new touch_for_foodEntities(); target = new ServiceRequestController(); Session session = new Session(db, target); session.simulateLogin(user1.username, user1.password); }
public void CreateServiceRequestTest() { //Check Setup db = new touch_for_foodEntities(); int numberOfRequestsBefore = db.service_request.ToList<service_request>().Count(); //Arrange table table2 = testDatabase.AddTable(restaurant1); //Create Second Table //Create second request to be added by method service_request request2 = new service_request(); request2 = new service_request(); request2.note = "ServiceRequest2"; request2.created = DateTime.Now; request2.status = (int)ServiceRequestUtil.ServiceRequestStatus.OPEN; request2.version = 0; request2.table_id = table2.id; //Act target.Create(request2); //Assert db = new touch_for_foodEntities(); Assert.IsTrue(db.service_request.Find(request2.id) != null); int numberOfServiceRequestsAfter = db.service_request.ToList<service_request>().Count(); Assert.IsTrue((numberOfRequestsBefore + 1) == numberOfServiceRequestsAfter); //Cleanup testDatabase.RemoveServiceRequest(request2); testDatabase.RemoveTable(table2); }
public void CreateServiceRequestNullTableErrorTest() { //Check Setup db = new touch_for_foodEntities(); int numberOfRequestsBefore = db.service_request.ToList<service_request>().Count(); //Arrange //Create Second Request service_request request2 = new service_request(); request2 = new service_request(); request2.note = "ServiceRequest2"; request2.created = DateTime.Now; request2.status = (int)ServiceRequestUtil.ServiceRequestStatus.OPEN; request2.version = 0; request2.table_id = null; //Act target.Create(request2); //Assert db = new touch_for_foodEntities(); Assert.IsTrue(db.service_request.Find(request2.id) == null); int numberOfServiceRequestsAfter = db.service_request.ToList<service_request>().Count(); Assert.IsTrue(numberOfRequestsBefore == numberOfServiceRequestsAfter); }