public IActionResult GetLocations(EditDeviceModel data) { //initializing DB managers DBManagerDevice dbManager = new DBManagerDevice(configuration); DBManagerShared shared = new DBManagerShared(configuration); //get the logs back again List <DeviceModel> logs = dbManager.GetDeviceLogs(data.Device.DeviceID); int modelID = shared.GetModelID(data.Device.Model.ModelName); data.Logs = logs; EditDeviceModel newdata = data; //check if image exists string filename = $"Capture_{modelID}.png"; string imagepath = (string)AppDomain.CurrentDomain.GetData("webRootPath") + "\\DeviceContent\\" + filename; if (System.IO.File.Exists(imagepath)) { newdata.ImagePath = filename; } //fetch storage locations if user has typed a valid room if (data.Room != null) { //prep data for database string[] splittedRoom = data.Room.Split('.'); //prep data model EditDeviceModel editData = new EditDeviceModel(); DeviceModel device = new DeviceModel(); BuildingModel building = new BuildingModel(splittedRoom[0], Convert.ToByte(splittedRoom[1])); StorageLocationModel storageLocation = new StorageLocationModel(); storageLocation.Location = building; device.Location = storageLocation; editData.Device = device; //get storagelocations EditDeviceModel locations = dbManager.GetStorageLocations(editData); newdata.Shelfs = locations.Shelfs; newdata.Shelf = null; } //return the same data without having selected anything else { EditDeviceModel storagelocation = dbManager.GetStorageLocations(null); newdata.Rooms = storagelocation.Rooms; newdata.Shelf = null; } // clear model ModelState.Clear(); return(View("EditView", newdata)); }
//getting data from database & return model to view public IActionResult EditView(string submit) { //initializing DB managers DBManagerDevice dbManager = new DBManagerDevice(configuration); DBManagerShared dbsharedManager = new DBManagerShared(configuration); //return device info to Edit view int ID = int.Parse(submit); DeviceModel data = new DeviceModel(); data = dbManager.GetDeviceInfoWithLocation(ID); List <DeviceModel> logs = dbManager.GetDeviceLogs(ID); List <string> categories = dbsharedManager.GetCategories(); List <string> modelNames = dbsharedManager.GetModelNames(); EditDeviceModel storagelocation = dbManager.GetStorageLocations(null); int modelID = dbsharedManager.GetModelID(data.Model.ModelName); EditDeviceModel editdata = new EditDeviceModel(); editdata.Device = data; editdata.Room = new string($"{data.Location.Location.Building}.{data.Location.Location.RoomNumber.ToString()}"); editdata.Shelf = new string($"{data.Location.ShelfName}.{data.Location.ShelfLevel}.{data.Location.ShelfSpot}"); //check if image exists string filename = $"Capture_{modelID}.png"; string imagepath = (string)AppDomain.CurrentDomain.GetData("webRootPath") + "\\DeviceContent\\" + filename; if (System.IO.File.Exists(imagepath)) { editdata.ImagePath = filename; } editdata.Logs = logs; editdata.Categories = categories; editdata.ModelNames = modelNames; editdata.Rooms = storagelocation.Rooms; //test to get all rooms and shelves storagelocation = dbManager.GetStorageLocations(editdata); editdata.Rooms = storagelocation.Rooms; editdata.Shelfs = storagelocation.Shelfs; return(View(editdata)); }
//saves all edits on device to database public IActionResult EditDevice(EditDeviceModel data) { //initializing DB managers DBManagerDevice dbManager = new DBManagerDevice(configuration); EditDeviceModel storagelocation = dbManager.GetStorageLocations(null); //check categories bool Category_validated = checkUserInput(data.Categories, data.Device.Model.Category.Category); //check modelNames bool ModelName_validated = checkUserInput(data.ModelNames, data.Device.Model.ModelName); //check BuildingNames bool Building_validated = checkUserInput(storagelocation.Rooms, data.Room); //check RoomNames bool Room_validated = checkUserInput(storagelocation.Shelfs, data.Shelf); if (Category_validated && ModelName_validated && Building_validated && Room_validated) { Debug.WriteLine("success"); } else { //set view bags if (!Category_validated) { ViewBag.Error = "indtast en gyldig kategori"; } else if (!ModelName_validated) { ViewBag.Error = "indtast et gyldigt model navn"; } else if (!Building_validated) { ViewBag.LocationError = "indtast en gyldig lokation"; } else if (!Room_validated) { ViewBag.LocationError = "indtast en gyldig hylde"; } return(View("EditView", data)); } data.Device.ChangedBy = HttpContext.Session.GetString("uniLogin"); data.Device.Notes = "Enhed redigeret"; #region saving new location //prep data for database string[] splittedRoom = data.Room.Split('.'); string[] splittedShelf = data.Shelf.Split('.'); //set data models BuildingModel building = new BuildingModel(splittedRoom[0], Convert.ToByte(splittedRoom[1])); StorageLocationModel storageLocation = new StorageLocationModel(splittedShelf[0], Convert.ToByte(splittedShelf[1]), Convert.ToByte(splittedShelf[2]), building); data.Device.Location = storageLocation; //data.Device.Notes = "Placering redigeret"; //send data to database data = dbManager.EditDeviceLocation(data); #endregion //get the logs again List <DeviceModel> logs = dbManager.GetDeviceLogs(data.Device.DeviceID); data.Logs = logs; //send data to database int success = dbManager.EditDevice(data); //set message to be shown in view if (success > 0) { ViewBag.edit = "Enhed Gemt"; } else { ViewBag.edit = "Enhed ikke Gemt"; } return(View("EditView", data)); }