Exemplo n.º 1
0
        private InvestPartBm PrepareInvestPartModel()
        {
            var investPart = new InvestPartBm();

            investPart.VehicleServices.Add("By Me");

            var serviceNames = this.carManager.GetVehicleServiceNames();

            foreach (var serviceName in serviceNames)
            {
                investPart.VehicleServices.Add(serviceName);
            }

            var userId = this.User.Identity.GetUserId();

            investPart.CarNames = this.carManager.GetCarNames(userId);

            return(investPart);
        }
        public JsonResult Cost([Bind(Prefix = "InvestPart")] InvestPartBm bm)
        {
            var result = new CostDto();

            var carId = bm.CarId;

            if (carId < 1)
            {
                this.Response.StatusCode = 400;
                return(new JsonResult()
                {
                    Data = new CostDto()
                    {
                        ErrorMessage = "Cannot process request!"
                    }
                });
            }

            Cost investmentEntity = null;

            if (!string.IsNullOrWhiteSpace(bm.Name) && bm.Price > 0)// && (bm.DistanceTraveled > 0 || bm.MountedOnMi > 0 || bm.MountedOnKm > 0))
            {
                var newInvestment = Mapper.Map <InvestPartBm, CreateInvestmentVm>(bm);

                investmentEntity = this.carManager.AddNewInvestment(newInvestment, carId, this.GetAppUserId);

                var isInvestAdded = investmentEntity != null;

                if (isInvestAdded)
                {
                    result.HasInvest     = isInvestAdded;
                    result.InvestMessage = this.GetMessage(newInvestment.Name, isInvestAdded);
                }
                else
                {
                    result.HasInvest    = isInvestAdded;
                    result.HasError     = !isInvestAdded;
                    result.ErrorMessage = this.GetMessage(newInvestment.Name, isInvestAdded);
                }
            }
            else if ((string.IsNullOrWhiteSpace(bm.Name) && bm.Price > 0) || (!string.IsNullOrWhiteSpace(bm.Name) && bm.Price <= 0) || (string.IsNullOrWhiteSpace(bm.Name) && bm.Price < 0))
            {
                result.HasError      = true;
                result.HasInvest     = false;
                result.InvestMessage = string.Format("Cost/Investment contain invalid data!\r\nCost/Investment name: {0}\r\nCost/Investment Price: {1}", bm.Name, bm.Price);
            }

            if (!string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice > 0)
            {
                var quantity = 1;

                if (bm.Quantity > 1)
                {
                    quantity = bm.Quantity;
                }

                var carPart = Mapper.Map <InvestPartBm, CreateCarPartVm>(bm);

                var isCarPartAdded = true;

                for (int i = 0; i < quantity; i++)
                {
                    isCarPartAdded = this.carManager.AddReplacedPart(carPart, carId, this.GetAppUserId);

                    if (!isCarPartAdded)
                    {
                        break;
                    }
                }

                if (isCarPartAdded)
                {
                    result.HasInvest     = isCarPartAdded;
                    result.InvestMessage = this.GetMessage(carPart.PartName, isCarPartAdded, quantity);
                }
                else
                {
                    this.Response.StatusCode = 400;

                    result.HasInvest    = isCarPartAdded;
                    result.HasError     = !isCarPartAdded;
                    result.ErrorMessage = this.GetMessage(carPart.PartName, isCarPartAdded);

                    return(new JsonResult()
                    {
                        Data = result
                    });
                }
            }
            else if ((string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice > 0) || (!string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice <= 0) || (string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice < 0))
            {
                result.NewPartMessage = string.Format("New Part contain invalid data!\r\nPart name: {0}\r\nPart Price: {1}", bm.PartName, bm.PartPrice);
            }

            return(new JsonResult()
            {
                Data = result
            });
        }