コード例 #1
0
        /// <summary>
        /// 将照片保存到指定的路径中,并返回唯一的路径名
        /// </summary>
        /// <returns></returns>
        private string ProcessUploadedFile(DeviceCreateViewModel model)
        {
            string uniqueFileName = null;

            if (model.Photos.Count > 0)
            {
                foreach (var photo in model.Photos)
                {
                    //必须将图像上传到wwwroot中的images文件夹
                    //而要获取wwwroot文件夹的路径,我们需要注入ASP.Net Core提供的webHostEnvironment.WebRootPath
                    string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images");

                    //为了确保文件名是唯一的,我们在文件名后附加一个新的GUID值和一个下划线
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);

                    //因为使用了非托管资源,所以需要手动进行释放
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        //使用IFormFile接口提供的CopyTo()方法见文件赋值到wwwroot/images文件夹
                        photo.CopyTo(fileStream);
                    }
                }
            }

            return(uniqueFileName);
        }
コード例 #2
0
        public async Task <IActionResult> Create(DeviceCreateViewModel deviceViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            /*if (true)
             * {
             *  ModelState.AddModelError("OwnerId", "Hack attempt!");
             *  return View(deviceViewModel);
             * }*/
            if (ModelState.IsValid)
            {
                AppUser newOwner = null;
                if (appUser.Id.Equals(deviceViewModel.OwnerId))
                {
                    newOwner = appUser;
                }
                else
                {
                    AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(deviceViewModel.OwnerId));
                    if (probablyNewOwner == null)
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(deviceViewModel));
                    }

                    if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                    {
                        newOwner = probablyNewOwner;
                    }
                    else
                    {
                        ModelState.AddModelError("OwnerId", "Hack attempt!");
                        ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                        return(View(deviceViewModel));
                    }
                }

                Device device = new Device
                {
                    Id                  = deviceViewModel.Id,
                    Name                = deviceViewModel.Name,
                    Comment             = deviceViewModel.Comment,
                    Owner               = newOwner,
                    IsServiceObligatory = deviceViewModel.IsServiceObligatory,
                    LastPlaceLeft       = deviceViewModel.LastPlaceLeft,
                    LastServiceDate     = deviceViewModel.LastServiceDate,
                    LastUsedDate        = deviceViewModel.LastUsedDate
                };

                _context.Add(device);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
            return(View(deviceViewModel));
        }
コード例 #3
0
        public IActionResult Create(DeviceCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                Device newDevice = new Device
                {
                    GUID      = Guid.NewGuid().ToString(),
                    Name      = model.Name,
                    ClassName = model.ClassName,
                    City      = model.City,
                    PhotoPath = ProcessUploadedFile(model),
                };

                _deviceRepository.Add(newDevice);
                return(RedirectToAction("Details", new { id = newDevice.Id }));
            }

            return(View());
        }
コード例 #4
0
        // GET: Devices/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (id == null)
            {
                return(NotFound());
            }

            Device device = await _context.Devices.FindAsync(id);

            if (device == null)
            {
                return(NotFound());
            }

            await _context.Entry(device).Reference(p => p.Owner).LoadAsync();

            if (!device.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(device.Owner) && p.IsAccepted.Equals(true)))
            {
                return(NotFound());
            }

            ViewBag.UserList = MakeAllowedUsersList(appUser, device.Owner.Id);

            DeviceCreateViewModel deviceViewModel = new DeviceCreateViewModel
            {
                Id                  = device.Id,
                Name                = device.Name,
                Comment             = device.Comment,
                OwnerId             = device.Owner.Id,
                IsServiceObligatory = device.IsServiceObligatory.GetValueOrDefault(),
                LastPlaceLeft       = device.LastPlaceLeft,
                LastServiceDate     = device.LastServiceDate,
                LastUsedDate        = device.LastUsedDate
            };

            return(View(deviceViewModel));
        }
コード例 #5
0
        public async Task <IActionResult> Edit(long id, DeviceCreateViewModel deviceViewModel)
        {
            AppUser appUser = AuthExtender.GetLoggedInUser(this, _context);

            if (id != deviceViewModel.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Device device = await _context.Devices.FindAsync(id);

                await _context.Entry(device).Reference(p => p.Owner).LoadAsync();

                if (!device.Owner.Equals(appUser) & !_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(device.Owner) && p.IsAccepted.Equals(true)))
                {
                    return(NotFound());
                }

                if (!device.Owner.Id.Equals(deviceViewModel.OwnerId))
                {
                    AppUser newOwner = null;
                    if (appUser.Id.Equals(deviceViewModel.OwnerId))
                    {
                        newOwner = appUser;
                    }
                    else
                    {
                        AppUser probablyNewOwner = _context.Users.FirstOrDefault(p => p.Id.Equals(deviceViewModel.OwnerId));
                        if (probablyNewOwner == null)
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(deviceViewModel));
                        }

                        if (_context.SickWardenRelations.Any(p => p.Warden.Equals(appUser) && p.Sick.Equals(probablyNewOwner) && p.IsAccepted.Equals(true)))
                        {
                            newOwner = probablyNewOwner;
                        }
                        else
                        {
                            ModelState.AddModelError("OwnerId", "Hack attempt!");
                            ViewBag.UserList = MakeAllowedUsersList(appUser, null);
                            return(View(deviceViewModel));
                        }
                    }
                    device.Owner = newOwner;
                }

                device.Name                = deviceViewModel.Name;
                device.Comment             = deviceViewModel.Comment;
                device.LastUsedDate        = deviceViewModel.LastUsedDate;
                device.LastPlaceLeft       = deviceViewModel.LastPlaceLeft;
                device.IsServiceObligatory = deviceViewModel.IsServiceObligatory;
                device.LastServiceDate     = deviceViewModel.LastServiceDate;

                try
                {
                    _context.Update(device);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DeviceExists(device.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.UserList = MakeAllowedUsersList(appUser, deviceViewModel.OwnerId);
            return(View(deviceViewModel));
        }