public async Task <IActionResult> Edit(int id, FloorViewModel fvm)
        {
            if (id != fvm.Floor.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                // get old entry
                var oldEntry = _context.Floors.Find(fvm.Floor.ID);
                var ogImage  = oldEntry.FilePath;

                // update values
                _context.Entry(oldEntry).CurrentValues.SetValues(fvm.Floor);

                // store file
                try
                {
                    if (fvm.File.Length > 0)
                    {
                        if (Path.GetExtension(fvm.File.FileName).ToLower() == ".svg")
                        {
                            // get file name
                            string _FileName = $@"{Guid.NewGuid()}.svg";
                            string _Path     = Path.Combine(_hostingEnvironment.WebRootPath, "Floors", _FileName);

                            using (var stream = new FileStream(_Path, FileMode.Create))
                            {
                                await fvm.File.CopyToAsync(stream);

                                oldEntry.FilePath = "/Floors/" + _FileName;
                            }
                        }
                        else
                        {
                            ViewBag.Message   = "Invalid file uploaded. Please select an SVG file.";
                            oldEntry.FilePath = ogImage;
                        }
                    }
                    else
                    {
                        // no file uploaded
                        oldEntry.FilePath = ogImage;
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Message   = "Unable to upload file.";
                    oldEntry.FilePath = ogImage;
                }

                // commit changes
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(fvm));
        }
        public async Task <IActionResult> Edit(Equipment equipment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(equipment).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(equipment));
        }
        public async Task <IActionResult> Edit(WorkStyle workStyle)
        {
            if (ModelState.IsValid)
            {
                db.Entry(workStyle).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(workStyle));
        }
Exemple #4
0
        public async Task <IActionResult> Edit(StationViewModel stationViewModel)
        {
            if (ModelState.IsValid)
            {
                await db.StationEquipments
                .Where(se => se.StationId == stationViewModel.Station.ID)
                .ForEachAsync(old => db.StationEquipments.Remove(old));

                await db.SaveChangesAsync();

                var station = stationViewModel.Station;

                var oldEntry = await db.Stations.Include(s => s.StationEquipments).FirstOrDefaultAsync(s => s.ID == stationViewModel.Station.ID);

                var ogImage = oldEntry.FilePath;

                // update old row
                db.Entry(oldEntry).CurrentValues.SetValues(station);

                // handle image
                try
                {
                    if (stationViewModel.File.Length > 0)
                    {
                        // get file name
                        string _FileName = $@"{Guid.NewGuid()}.jpg";
                        string _Path     = Path.Combine(_hostingEnvironment.WebRootPath, "Uploaded", _FileName);

                        using (var memoryStream = new MemoryStream())
                        {
                            stationViewModel.File.OpenReadStream().CopyTo(memoryStream);
                            Image scaled = Resize(Image.FromStream(memoryStream), 600, 600);

                            // convert image
                            scaled.Save(_Path, ImageFormat.Jpeg);

                            oldEntry.FilePath = "/Uploaded/" + _FileName;

                            scaled.Dispose();

                            // delete old image
                            string delTarget = !String.IsNullOrEmpty(ogImage) ? _hostingEnvironment.WebRootPath + ogImage.Replace("/", "\\") : String.Empty;
                            if (!String.IsNullOrEmpty(delTarget) && System.IO.File.Exists(delTarget))
                            {
                                System.IO.File.Delete(delTarget);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Message = "File upload failed.";
                    // restore previous image
                    oldEntry.FilePath = ogImage;
                }

                //oldEntry.Equipment = db.Equipment.Where(o => stationViewModel.SelectedEquipment.Contains(o.ID)).ToList();
                station.StationEquipments = new List <StationEquipment>();

                foreach (var equip in stationViewModel.SelectedEquipment)
                {
                    oldEntry.StationEquipments.Add(new StationEquipment
                    {
                        StationId   = station.ID,
                        EquipmentId = equip
                    });
                }

                var _type  = db.WorkStyles.FirstOrDefaultAsync(t => t.ID == stationViewModel.selectedWorkStyle);
                var _floor = db.Floors.FirstOrDefaultAsync(f => f.ID == stationViewModel.selectedFloor);

                oldEntry.Type  = await _type;
                oldEntry.Floor = await _floor;

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(stationViewModel));
        }