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
        public IActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }
            CleaningPlan plan = db.CleaningPlans.FirstOrDefault(p => p.Id == id);

            if (plan != null)
            {
                if (plan.CreatorId != userManager.GetUserId(HttpContext.User))
                {
                    return(BadRequest());
                }
                else
                {
                    string path     = Path.Combine(webHostEnvironment.WebRootPath, "images");
                    string filePath = Path.Combine(path, plan.MainPhoto);
                    System.IO.File.Delete(filePath);

                    db.CleaningPlans.Remove(plan);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            return(NotFound());
        }
Example #3
0
        public IActionResult Show(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

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

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

            ViewBag.CurrectUserId = userManager.GetUserId(HttpContext.User);
            return(View(cleaningPlan));
        }
Example #4
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 #5
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));
        }