public async Task <IActionResult> Create(Equipment equipment)
        {
            if (ModelState.IsValid)
            {
                db.Equipment.Add(equipment);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(equipment));
        }
        public async Task <IActionResult> Create(WorkStyle workStyle)
        {
            if (ModelState.IsValid)
            {
                db.WorkStyles.Add(workStyle);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(workStyle));
        }
        public async Task <IActionResult> Checkout([FromBody] CheckinViewModel input)
        {
            var oldCheckin = await db.Checkins.FirstOrDefaultAsync(c => c.Username == input.acid);

            if (oldCheckin != null)
            {
                var _clearCheckins = db.Stations.Where(s => s.LastCheckin == oldCheckin).ForEachAsync(s => s.LastCheckin = null);
                db.Checkins.Remove(oldCheckin);
                await _clearCheckins;
                await db.SaveChangesAsync();
            }

            return(Ok());
        }
        public async Task <IActionResult> Create(FloorViewModel fvm)
        {
            if (ModelState.IsValid)
            {
                // 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 _Folder   = Path.Combine(_hostingEnvironment.WebRootPath, "Floors");
                            string _Path     = Path.Combine(_Folder, _FileName);

                            // make folder if needed
                            if (!Directory.Exists(_Folder))
                            {
                                Directory.CreateDirectory(_Folder);
                            }

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

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

                // commit changes
                _context.Floors.Add(fvm.Floor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(fvm));
        }
Exemple #5
0
        public async Task <IActionResult> Create(StationViewModel stationViewModel)
        {
            if (ModelState.IsValid)
            {
                var station = stationViewModel.Station;

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

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

                station.Type = await db.WorkStyles.FirstOrDefaultAsync(w => w.ID == stationViewModel.selectedWorkStyle);

                station.Floor = await db.Floors.FirstOrDefaultAsync(f => f.ID == stationViewModel.selectedFloor);

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

                        // make folder if needed
                        if (!Directory.Exists(_Folder))
                        {
                            Directory.CreateDirectory(_Folder);
                        }

                        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);

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

                            scaled.Dispose();
                        }
                    }
                }
                catch (Exception e)
                {
                    ViewBag.Message = "File upload failed.";
                }

                db.Stations.Add(station);
                await db.SaveChangesAsync();

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