Example #1
0
        public IActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            CleaningPlan plan = db.CleaningPlans
                                .Include(p => p.Location).FirstOrDefault(p => p.Id == id);

            if (plan == null)
            {
                return(NotFound());
            }
            if (plan.CreatorId != userManager.GetUserId(HttpContext.User))
            {
                return(BadRequest());
            }

            CleanPlanViewModel model = new CleanPlanViewModel
            {
                Id              = plan.Id,
                Describing      = plan.Describing,
                PlanName        = plan.PlanName,
                PlanDate        = plan.PlanDate,
                Address         = plan.Address,
                Location        = plan.Location,
                MainPhotoString = plan.MainPhoto,
                LocationId      = plan.LocationId,
                CreatorId       = plan.CreatorId
            };

            return(View(model));
        }
Example #2
0
        private string UploadFile(CleanPlanViewModel model)
        {
            string uniqueFileName = null;

            if (model.MainPhoto != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + model.MainPhoto.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.MainPhoto.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
Example #3
0
        public IActionResult Edit(CleanPlanViewModel model)
        {
            Console.WriteLine(model.CreatorId);

            if (model.CreatorId != userManager.GetUserId(HttpContext.User))
            {
                return(BadRequest());
            }

            CleaningPlan plan = new CleaningPlan
            {
                Id         = model.Id,
                PlanName   = model.PlanName,
                Describing = model.Describing,
                Address    = model.Address,
                LocationId = model.LocationId,
                PlanDate   = model.PlanDate,
                CreatorId  = userManager.GetUserId(HttpContext.User)
            };

            if (model.MainPhoto != null)
            {
                if (!string.IsNullOrEmpty(model.MainPhotoString))
                {
                    string path     = Path.Combine(webHostEnvironment.WebRootPath, "images");
                    string filePath = Path.Combine(path, model.MainPhotoString);
                    System.IO.File.Delete(filePath);
                }
                plan.MainPhoto = UploadFile(model);
            }
            else
            {
                plan.MainPhoto = model.MainPhotoString;
            }


            db.CleaningPlans.Update(plan);
            db.SaveChanges();
            return(RedirectToAction("Show", new { id = plan.Id }));
        }
Example #4
0
        public IActionResult Create(CleanPlanViewModel model)
        {
            if (ModelState.IsValid)
            {
                string       userId         = userManager.GetUserId(HttpContext.User);
                string       uniqueFileName = UploadFile(model);
                CleaningPlan cleaningPlan   = new CleaningPlan
                {
                    PlanName   = model.PlanName,
                    Describing = model.Describing,
                    CreatorId  = userId,
                    PlanDate   = model.PlanDate,
                    LocationId = model.LocationId,
                    Address    = model.Address,
                    MainPhoto  = uniqueFileName
                };

                db.CleaningPlans.Add(cleaningPlan);
                db.SaveChanges();
                return(RedirectToAction("Index", "CleaningPlans"));
            }
            return(View(model));
        }