// GET: Regions/Create public ActionResult Create() { //get all relay IPs getAllIps(); var vModel = new RegionsRelaysStoresViewModel(); return(View(vModel)); }
public ActionResult Create([Bind(Include = "regionID,regionName,gatewayIP")] Regions regions, [Bind(Include = "relayID, relayName, relayIP, regionID, isActive, relayQueue, isGateway")] Relay relay, [Bind(Include = "storeID, storeName, storeWeight, relayID, storeIP, regionID")] Store store, [Bind(Include = "relayToProcessCenterConnectionID, relayID, processCenterID, isActive, relayToProcessCenterConnectionWeight")] RelayToProcessCenterConnection relayToProcessCenterConnection, string ip, int?gatewayWeight) { var vModel = new RegionsRelaysStoresViewModel(); // Boolean thereIsAnError = false; string gateway_duplicate_error = ""; string gateway_input_error = ""; Boolean gateway_correctIP = true; //check if Gateway IP field is empty: if (string.IsNullOrEmpty(regions.gatewayIP))//check for empty input { ModelState.AddModelError("regions.gatewayIP", "The Region Gateway IP field is required"); thereIsAnError = true; } else { //check gateway IP if it the correct format: 192.168.0.XXX; XXX: 0 -> 255 gateway_correctIP = checkForCorrectIP(regions.gatewayIP, out gateway_input_error); if (!gateway_correctIP) { ModelState.AddModelError("regions.gatewayIP", gateway_input_error); thereIsAnError = true; } } if (gateway_correctIP && !thereIsAnError)//this is to avoid input error with special characters that would crash the application { //check gateway IP if it matches another IP Boolean gateway_duplicate = checkDuplicateIP(regions.gatewayIP, out gateway_duplicate_error); if (gateway_duplicate) { ModelState.AddModelError("regions.gatewayIP", gateway_duplicate_error); thereIsAnError = true; } } //regions.regionName = regions.regionID.ToString(); ////check if regions.regionName == NULL: //if(string.IsNullOrWhiteSpace(regions.regionName)) //{ // ModelState.AddModelError("regions.regionName", "The Region Name field is required"); // thereIsAnError = true; //} ////check if relay.relayName == NULL: (relay is the gateway) //if (string.IsNullOrWhiteSpace(relay.relayName)) //{ // ModelState.AddModelError("relay.relayName", "The Gateway Name field is required"); // thereIsAnError = true; //} //check if relay.relayQueue == NULL if (string.IsNullOrWhiteSpace(relay.relayQueue.ToString())) { ModelState.AddModelError("relay.relayQueue", "The Gateway Queue Limit field is required"); thereIsAnError = true; } //check if relay.relayQueue <1 || > 500 else if (relay.relayQueue < 1 || relay.relayQueue > 500) { ModelState.AddModelError("relay.relayQueue", "The Gateway Queue Limit must be from 1 to 500"); thereIsAnError = true; } //check for store.storeIP input string store_duplicate_error = ""; string store_input_error = ""; Boolean store_correctIP = true; //check if Store IP field is empty: if (string.IsNullOrEmpty(store.storeIP))//check for empty input { ModelState.AddModelError("store.storeIP", "The Store IP field is required"); thereIsAnError = true; } //check if the store IP is duplicate to the input of gateway IP: else if (store.storeIP.Equals(regions.gatewayIP)) { ModelState.AddModelError("store.storeIP", "The Store IP must be different from the Gateway IP"); thereIsAnError = true; } else { //check store IP if it the correct format: 192.168.0.XXX; XXX: 0 -> 255 store_correctIP = checkForCorrectIP(store.storeIP, out store_input_error); if (!store_correctIP) { ModelState.AddModelError("store.storeIP", store_input_error); thereIsAnError = true; } } if (store_correctIP && !thereIsAnError)//this is to avoid input error with special characters that would crash the application { //check store IP if it matches another IP Boolean store_duplicate = checkDuplicateIP(store.storeIP, out store_duplicate_error); if (store_duplicate) { ModelState.AddModelError("store.storeIP", store_duplicate_error); thereIsAnError = true; } } //check if store.storeName == NULL if (string.IsNullOrEmpty(store.storeName))//check for empty input { ModelState.AddModelError("store.storeName", "The Store Name field is required"); thereIsAnError = true; } //check if store.storeWeight == NULL if (string.IsNullOrWhiteSpace(store.storeWeight.ToString())) { ModelState.AddModelError("store.storeWeight", "The Store Weight field is required"); thereIsAnError = true; } //check if store.storeWeight < 1 || > 500 else if (store.storeWeight < 1 || store.storeWeight > 500) { ModelState.AddModelError("store.storeWeight", "Input Error: The Weight must be from 1 to 500"); thereIsAnError = true; } ////check if relay to PC Weight == NULL //if (string.IsNullOrWhiteSpace(relayToProcessCenterConnection.relayToProcessCenterConnectionWeight.ToString())) //{ // ModelState.AddModelError("relayToProcessCenterConnection.relayToProcessCenterConnectionWeight", "The Relay to PC Weight field is required"); // thereIsAnError = true; //} //check if relay to PC Weight < 1 || > 500 //else if (relayToProcessCenterConnection.relayToProcessCenterConnectionWeight < 1 || relayToProcessCenterConnection.relayToProcessCenterConnectionWeight > 500) //{ // ModelState.AddModelError("store.storeWeight", "Input Error: The Weight must be from 1 to 500"); // thereIsAnError = true; //} //check if gateway weight is null: if (gatewayWeight == null) { ModelState.AddModelError("gatewayWeight", "Input Error: The Weight must be from 1 to 500"); thereIsAnError = true; } //check if there is an error anywhere. If there is, return the viewModel: if (thereIsAnError) { //get all relay IPs getAllIps(); return(View(vModel)); } // db.Regions.Add(regions); db.SaveChanges(); // //get the last RegionID which was just created: int lastRegionId = getLastRegionId(); //update region name and set it to region ID: updateRegionName(lastRegionId); //get the last existing RelayID in the database then add 1 to it: string lastRelayId = (Convert.ToInt32(getLastRelayId()) + 1).ToString(); //add the new gateway in the relays table: saveRelay(lastRelayId, relay.relayName, regions.gatewayIP, lastRegionId, relay.isActive, relay.relayQueue, relay.isGateway); //get the last RelayID that was just created: lastRelayId = getLastRelayId(); //add a connection from gateway to process center: //saveRelayToProcessCenter(lastRelayId, 1, true, relayToProcessCenterConnection.relayToProcessCenterConnectionWeight); connectGatewayToIP(regions.gatewayIP, ip, gatewayWeight); //get the last existing Store in the database then add 1 to it: string lastStoreId = (Convert.ToInt32(getLastStoreId()) + 1).ToString(); //add the new store in the stores table: saveStore(lastStoreId, store.storeName, store.storeWeight, lastRelayId, store.storeIP, lastRegionId); //get the last StoreID that was just created: lastStoreId = getLastStoreId(); //add storeID & relayID to StoreToRelays table: saveStoresToRelays(lastRelayId, lastStoreId, true, store.storeWeight); // //return RedirectToAction("Index"); ViewBag.SuccessMessage = "The new region has been successfully added to the network!"; //get all relay IPs getAllIps(); return(View()); }