示例#1
0
        public IActionResult Add(CameraFormViewModel cameraModel)
        {
            if (cameraModel.LightMetering == null || !cameraModel.LightMetering.Any())
            {
                ModelState.AddModelError(nameof(cameraModel.LightMetering), "Please Select at least one Light Metering");
            }

            if (!ModelState.IsValid)
            {
                return(View(cameraModel));
            }

            this.cameraService.Create(
                cameraModel.Make,
                cameraModel.Model,
                cameraModel.Price,
                cameraModel.Quantity,
                cameraModel.MinShutterSpeed,
                cameraModel.MaxShutterSpeed,
                cameraModel.MinISO,
                cameraModel.MaxISO,
                cameraModel.IsFullFrame,
                cameraModel.VideoResolution,
                cameraModel.LightMetering,
                cameraModel.Description,
                cameraModel.ImageUrl,
                userManager.GetUserId(this.User));

            return(RedirectToAction(nameof(HomeController.Index), "Home"));
        }
        public IActionResult Edit(int id, CameraFormViewModel cameraModel)
        {
            string username = User.Identity.Name;

            if (!this.cameraService.IsUserOwner(username, id))
            {
                return(RedirectToAction(nameof(All)));
            }

            if (!ModelState.IsValid)
            {
                return(View(cameraModel));
            }

            this.cameraService.Edit(
                id,
                cameraModel.Make,
                cameraModel.Model,
                cameraModel.Price,
                cameraModel.Quantity,
                cameraModel.MinShutterSpeed,
                cameraModel.MaxShutterSpeed,
                cameraModel.MinISO,
                cameraModel.MaxISO,
                cameraModel.IsFullFrame,
                cameraModel.VideoResolution,
                cameraModel.LightMeterings,
                cameraModel.Description,
                cameraModel.ImageUrl);

            return(this.RedirectToAction(nameof(Details), new { id = id }));
        }
        public IActionResult Create(CameraFormViewModel cameraModel)
        {
            bool isUserAllowedToCreateCameras = this.userService.IsUserAllowedToCreateCameras(this.userManager.GetUserId(User));

            if (!isUserAllowedToCreateCameras)
            {
                return(RedirectToAction(nameof(AccountController.AccessDenied), "Account"));
            }

            if (!ModelState.IsValid)
            {
                return(View(cameraModel));
            }

            this.cameraService.Create(
                cameraModel.Make,
                cameraModel.Model,
                cameraModel.Price,
                cameraModel.Quantity,
                cameraModel.MinShutterSpeed,
                cameraModel.MaxShutterSpeed,
                cameraModel.MinISO,
                cameraModel.MaxISO,
                cameraModel.IsFullFrame,
                cameraModel.VideoResolution,
                cameraModel.LightMeterings == null ? null : cameraModel.LightMeterings,
                cameraModel.Description,
                cameraModel.ImageUrl,
                this.userManager.GetUserId(User)
                );

            return(RedirectToAction(nameof(HomeController.Index), "Home"));
        }
        public IActionResult Create(CameraFormViewModel cameraModel)
        {
            if (!cameraModel.LightMeteringSelectList.Any())
            {
                this.ModelState.AddModelError(nameof(cameraModel.LightMetering), LightMeteringRequired);
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(CameraFormView, cameraModel));
            }

            this.cameraService.Create(
                cameraModel.Make,
                cameraModel.Model,
                cameraModel.Price,
                cameraModel.Quantity,
                cameraModel.MinShutterSpeed,
                cameraModel.MaxShutterSpeed,
                cameraModel.MinIso,
                cameraModel.MaxIso,
                cameraModel.IsFullFrame,
                cameraModel.VideoResolution,
                cameraModel.LightMeteringSelectList,
                cameraModel.Description,
                cameraModel.ImageUrl,
                this.userManager.GetUserId(this.User));

            return(this.RedirectToAction(nameof(All)));
        }
        public IActionResult Create()
        {
            bool isUserAllowedToCreateCameras = this.userService.IsUserAllowedToCreateCameras(this.userManager.GetUserId(User));

            if (!isUserAllowedToCreateCameras)
            {
                return(RedirectToAction(nameof(AccountController.AccessDenied), "Account"));
            }

            CameraFormViewModel model = new CameraFormViewModel();

            return(View(model));
        }
        public IActionResult Edit(int id, CameraFormViewModel cameraModel)
        {
            // Camera does not exist
            if (!this.cameraService.Exists(id))
            {
                return(this.RedirectToAction(nameof(All)));
            }

            // Current user is not camera owner
            var currentUserId = this.userManager.GetUserId(this.User);

            if (currentUserId == null ||
                !this.cameraService.Exists(id, currentUserId))
            {
                return(this.RedirectToAction(nameof(Details), new { id }));
            }

            if (!cameraModel.LightMeteringSelectList.Any())
            {
                this.ModelState.AddModelError(nameof(cameraModel.LightMetering), LightMeteringRequired);
            }

            // Model is not valid
            if (!this.ModelState.IsValid)
            {
                return(this.View(CameraFormView, cameraModel));
            }

            this.cameraService.Update(
                id,
                cameraModel.Make,
                cameraModel.Model,
                cameraModel.Price,
                cameraModel.Quantity,
                cameraModel.MinShutterSpeed,
                cameraModel.MaxShutterSpeed,
                cameraModel.MinIso,
                cameraModel.MaxIso,
                cameraModel.IsFullFrame,
                cameraModel.VideoResolution,
                cameraModel.LightMeteringSelectList,
                cameraModel.Description,
                cameraModel.ImageUrl,
                currentUserId);

            return(this.RedirectToAction(nameof(Details), new { id }));
        }
        public IActionResult Delete(int id, CameraFormViewModel cameraModel)
        {
            // Camera does not exist
            if (!this.cameraService.Exists(id))
            {
                return(this.RedirectToAction(nameof(All)));
            }

            // Current user is not camera owner
            var currentUserId = this.userManager.GetUserId(this.User);

            if (currentUserId == null ||
                !this.cameraService.Exists(id, currentUserId))
            {
                return(this.RedirectToAction(nameof(Details), new { id }));
            }

            this.cameraService.Remove(id, currentUserId);

            return(this.RedirectToAction(nameof(All)));
        }
        public IActionResult Edit(int id)
        {
            string username = User.Identity.Name;

            if (!this.cameraService.IsUserOwner(username, id))
            {
                return(RedirectToAction(nameof(All)));
            }

            CameraFormServiceModel camera = this.cameraService.GetCameraFormById(id);

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

            CameraFormViewModel model = new CameraFormViewModel
            {
                Make            = camera.Make,
                Model           = camera.Model,
                Price           = camera.Price,
                Quantity        = camera.Quantity,
                MinShutterSpeed = camera.MinShutterSpeed,
                MaxShutterSpeed = camera.MaxShutterSpeed,
                MinISO          = camera.MinISO,
                MaxISO          = camera.MaxISO,
                IsFullFrame     = camera.IsFullFrame,
                VideoResolution = camera.VideoResolution,
                LightMeterings  = camera.LightMeterings,
                Description     = camera.Description,
                ImageUrl        = camera.ImageUrl,
                IsEdit          = true
            };

            return(View(model));
        }