//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)); }
//Add Device to Database public IActionResult AddDeviceToDB(CreateDeviceModel deviceData) { //initializing DB managers DBManagerDevice dbManager = new DBManagerDevice(configuration); DBManagerShared dbsharedManager = new DBManagerShared(configuration); int modelID = dbsharedManager.GetModelID(deviceData.Device.Model.ModelName); //check if image exists // string filename = $"Capture_{modelID}.png"; string imagepath = deviceData.Image; string _webroot = (string)AppDomain.CurrentDomain.GetData("webRootPath"); if (!imagepath.Contains(_webroot)) { //convet image source to byte array string sourceimage = deviceData.Image; string base64 = sourceimage.Substring(sourceimage.IndexOf(',') + 1); byte[] datastream = Convert.FromBase64String(base64); //convert byte array to image file using (MemoryStream m = new MemoryStream(datastream)) { using (Image image = Image.FromStream(m)) { string root = (string)AppDomain.CurrentDomain.GetData("webRootPath"); string webroot = root + "\\DeviceContent"; string filename = "\\Capture.png"; if (Directory.Exists(webroot)) { // save image to directory image.Save(webroot + filename, ImageFormat.Png); m.Dispose(); image.Dispose(); datastream = null; //get filename deviceData.Image = filename; } } } } //Add device to database DeviceModel data = deviceData.Device; data.ChangedBy = HttpContext.Session.GetString("uniLogin"); int deviceID = dbManager.CreateDevice(data); //return device info to Edit view data = dbManager.GetDeviceInfoWithLocation(deviceID); List <DeviceModel> logs = dbManager.GetDeviceLogs(deviceID); List <string> categories = dbsharedManager.GetCategories(); List <string> modelNames = dbsharedManager.GetModelNames(); modelID = dbsharedManager.GetModelID(data.Model.ModelName); EditDeviceModel editdata = new EditDeviceModel(); editdata.Device = data; editdata.Logs = logs; editdata.Categories = categories; editdata.ModelNames = modelNames; 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}"); editdata.ImagePath = $"Capture_{modelID}.png"; return(View("EditView", editdata)); }
/// <summary> /// Contextively processes what to do with this device id for this booking id. Be it create BookedDevice, return BookedDevice, or even delete. /// </summary> /// <param name="deviceID"></param> /// <param name="bookingID"></param> /// <returns></returns> public IActionResult ProcessDeviceForBooking(string deviceID, string bookingID) { HttpContext.Session.SetString("bookedDeviceError", ""); // FIrst, try and see if it's possible to convert and then do so. if (int.TryParse(deviceID, out int deviceIDInteger)) { DBManagerBooking dBManager = new DBManagerBooking(configuration); DBManagerDevice dBManagerDevice = new DBManagerDevice(configuration); // Second, find out if the deviceID is a valid one (if it exists and is Enabled (status = 1)) DeviceModel device = dBManagerDevice.GetDeviceInfoWithLocation(deviceIDInteger); BookingModel booking = dBManager.GetBooking(int.Parse(bookingID)); booking.Devices = dBManager.GetBookedDevices(int.Parse(bookingID)); if (device.Model.ModelName != null && device.Status == 1 && booking.BookingStatus == 1) { // Find out if you're supposed to Create the bookedDevice, or undo the bookedDevice or return the device bool deviceInBookingAlready = false; foreach (DeviceModel device1 in booking.Devices) { if (device1.DeviceID == device.DeviceID) { if (booking.DeliveredBy == null) { // The delivery for this booking has not been made yet, ergo the bookedDevice may be Deleted. An Undo. // "DeleteBookedDevice" dBManager.DeleteBookedDevice(device.DeviceID, booking.BookingID); } else if (device1.ReturnedBy == null || device1.ReturnedBy == "") { // Delivery has already been made. Update BookedDevice to be Returned. // "ReturnBookedDevice" dBManager.ReturnBookedDevice(device.DeviceID, booking.BookingID, HttpContext.Session.GetString("uniLogin")); } else { // This device has already been returned HttpContext.Session.SetString("bookedDeviceError", "Denne Enhed er allerede blevet returneret."); } deviceInBookingAlready = true; break; } } if (!deviceInBookingAlready) { // THe device does not already exist for this booking, therefore it should be created.. if the device is available. // CreateBookedDevice() will perform an availability check on its own. if (!dBManager.CreateBookedDevice(device.DeviceID, booking.BookingID)) { // The requested device is not available! Perhaps not returned from another Booking, or being repaired. HttpContext.Session.SetString("bookedDeviceError", "Denne enhed er allerede udlånt til en anden bestilling, eller er på værkstedet."); } } } else { // This device does not exist or is Disabled HttpContext.Session.SetString("bookedDeviceError", "Denne enhed eksisterer ikke, eller er slået fra. Eller denne booking er deaktiveret."); } } else { // It is not possible to convert the input to an integer, therefore we can do nothing with it. HttpContext.Session.SetString("bookedDeviceError", "Input kunne ikke konverteres til et helt tal."); } return(GoToScanDevices(bookingID)); }