Пример #1
0
        //
        // GET: /Repairs/Create
        public ActionResult Create()
        {
            var model = new RepairCardModel();
            IEnumerable<Core.Model.Mechanic> mechanics = Core.Managers.MechanicsManager.GetManager().Get();
            model.Mechanics = MappingManager.Instance.Map<IEnumerable<Core.Model.Mechanic>, IEnumerable<Mechanic>>(mechanics);

            return View(model);
        }
Пример #2
0
        public ActionResult Create(RepairCardModel repairCardModel)
        {
            try
            {
                if (Core.Managers.RepairsManager.GetManager().GetCars().Any(c => c.RegistrationNumber == repairCardModel.Car.RegistrationNumber))
                {
                    ModelState.AddModelError("", "Car with this registration number already exist.");
                    return View(repairCardModel);
                }
                if (Core.Managers.RepairsManager.GetManager().GetCars().Any(c => c.FrameNumber == repairCardModel.Car.FrameNumber))
                {
                    ModelState.AddModelError("", "Car with this frame number already exist.");
                    return View(repairCardModel);
                }

                // TODO: Add insert logic here
                var repairCard = MappingManager.Instance.Map<RepairCardModel, Core.Model.RepairCard>(repairCardModel);

                var currentUserId = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;
                if (currentUserId == null)
                {
                    throw new InvalidOperationException("Invalid user");
                }

                repairCard.UserId = currentUserId;
                repairCard.AcceptedDate = DateTime.Now;
                repairCard.Mechanic = MechanicsManager.GetManager().Get(repairCardModel.MechanicId);

                RepairsManager.GetManager().Add(repairCard);

                return RedirectToAction("Index", "Repairs");
            }
            catch(Exception ex)
            {
                HandleErrorInfo error = new HandleErrorInfo(ex, "Repairs", "Create");
                return View("Error", error);
            }
        }
Пример #3
0
        public ActionResult Edit(RepairCardModel repairCardModel)
        {
            try
            {
                var originalData = RepairsManager.GetManager().Get()
                    .Where(x => x.Id == repairCardModel.Id).SingleOrDefault();

                var userGUID = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;

                if (originalData == null || (!originalData.UserId.Equals(userGUID) && !Roles.IsUserInRole(User.Identity.Name, "Administrator")))
                {
                    return RedirectToAction("Index", "Repairs");
                }

                var editedRepairCard = MappingManager.Instance.Map<RepairCardModel, Core.Model.RepairCard>(repairCardModel);

                var originalRepairCard = RepairsManager.GetManager().Get(repairCardModel.Id);

                if (!originalRepairCard.Completed && editedRepairCard.Completed)
                {
                    editedRepairCard.Completed = repairCardModel.Completed;
                    editedRepairCard.FinishedDate = DateTime.Now;
                }

                if (repairCardModel.RepairPrice < editedRepairCard.SparePartsPrice)
                {
                    ModelState.AddModelError("", "The repair cost must be greater than the price of all used parts.");
                    return View(repairCardModel);
                }

                RepairsManager.GetManager().Update(editedRepairCard);

                return RedirectToAction("Index", "Repairs");
            }
            catch(Exception e)
            {
                return View(repairCardModel);
            }
        }