Ejemplo n.º 1
0
 public async Task <ActionResult <ResponseCRUDMessage> > Post([FromBody] Cars car)
 {
     if (ModelState.IsValid)
     {
         return(Ok(await carService.Add(car)));
     }
     return(BadRequest());
 }
Ejemplo n.º 2
0
        public string EditCarInfo([FromBody] EditCarInfoRequest request)
        {
            var result = new CommonBaseInfo();

            if (request.id.PackInt() > 0)
            {
                var model = _repository.GetById(request.id.PackInt());
                if (model == null)
                {
                    result.ResultMsg = "未查询到车辆信息";
                    result.StateCode = 404;
                    return(JsonConvert.SerializeObject(result));
                }
                Car newCar = new Car
                {
                    id            = Convert.ToInt32(request.id),
                    carBrand      = request.carBrand,
                    carColor      = request.carColor,
                    carLicenseImg = request.carLicenseImg,
                    carMasterId   = request.carMasterId.PackInt(),
                    carNo         = request.carNo,
                    carSeatNum    = request.carSeatNum.PackInt(),
                    carType       = request.carType.PackInt()
                };
                var update = _repository.Edit(newCar);
                if (update)
                {
                    result.ResultMsg = "编辑成功";
                    result.StateCode = 200;
                }
            }
            else
            {
                Car newCar = new Car
                {
                    carBrand      = request.carBrand,
                    carColor      = request.carColor,
                    carLicenseImg = request.carLicenseImg,
                    carMasterId   = request.carMasterId.PackInt(),
                    carNo         = request.carNo,
                    carSeatNum    = request.carSeatNum.PackInt(),
                    carType       = request.carType.PackInt()
                };
                var add = _repository.Add(newCar);
                if (add == null)
                {
                    result.ResultMsg = "新增失败";
                    result.StateCode = 201;
                    return(JsonConvert.SerializeObject(result));
                }
                result.ResultMsg = "新增成功";
                result.StateCode = 200;
            }
            return(JsonConvert.SerializeObject(result));
        }
        private void submitButton_Click(object sender, EventArgs e)
        {
            FuelEconomy milage = new FuelEconomy();

            milage.milesPerGallon = Convert.ToDecimal(milageTextBox.Text);

            Weight weight = new Weight();

            weight.kilograms = Convert.ToDecimal(weightTextBox.Text);

            Volume fuel = new Volume();

            fuel.litres = Convert.ToDecimal(maxFuelTextBox.Text);

            Car.CarType carType = new Car.CarType();
            carType = Car.CarType.Unknown;

            switch (carTypeComboBox.Text)
            {
            case "Convertible":
                carType = Car.CarType.Convertible;
                break;

            case "Coupe":
                carType = Car.CarType.Coupe;
                break;

            case "Hatchback":
                carType = Car.CarType.Hatchback;
                break;

            case "Sedan":
                carType = Car.CarType.Sedan;
                break;

            case "SUV":
                carType = Car.CarType.SUV;
                break;
            }

            carServicesObj.Add(
                vehicleNameTextBox.Text,
                numberPlateTextBox.Text,
                carType,
                milage,
                weight,
                fuel,
                Convert.ToInt32(maxPassengersTextBox.Text)
                );

            (Owner as CarFormView).FillCarList();
            Close();
        }
Ejemplo n.º 4
0
            public void AddTwoCarsWithDuplicateName_ShouldNotBeAdded()
            {
                var user = new User {
                    DisplayName = "User 1"
                };
                var mockUser = new Mock <IUserService>();

                mockUser.Setup(u => u.CurrentUser).Returns(user);

                var mock = new Mock <IRepository <Car> >();

                var collection = new HashSet <Car>();

                mock.Setup(repo => repo.Add(It.IsAny <Car>())).Callback <Car>((c) => {
                    collection.Add(c);
                });

                mock.Setup(repo => repo.Query(It.IsAny <Func <Car, bool> >())).Returns(collection.AsQueryable());

                var s = new CarServices(mock.Object, mockUser.Object);

                var c1 = new Car("Jazz")
                {
                    Owner = user
                };
                var c2 = new Car("Jazz")
                {
                    Owner = user
                };

                s.Add(c1);
                var ex = Assert.Throws <Exception>(() => {
                    s.Add(c2);
                });

                ex.Message.ShouldEqual("This car name has been used already.");//False because mock wasn't keep value in HashSet
                mock.Verify(r => r.Add(c1), Times.Once);
            }
        protected void submitBtn_Click(object sender, EventArgs e)
        {
            Car.CarType carTypeOfNewCar = Car.CarType.Unknown;
            switch (carTypeDdl.Text)
            {
            case "Unknown":
                carTypeOfNewCar = Car.CarType.Unknown;
                break;

            case "Hatchback":
                carTypeOfNewCar = Car.CarType.Hatchback;
                break;

            case "Sedan":
                carTypeOfNewCar = Car.CarType.Sedan;
                break;

            case "SUV":
                carTypeOfNewCar = Car.CarType.SUV;
                break;

            case "Coupe":
                carTypeOfNewCar = Car.CarType.Coupe;
                break;

            case "Convertible":
                carTypeOfNewCar = Car.CarType.Convertible;
                break;
            }

            FuelEconomy milageOfNewCar = new FuelEconomy();

            milageOfNewCar.milesPerGallon = Convert.ToDecimal(milageTxtBox.Text);

            Weight weightOfNewCar = new Weight();

            weightOfNewCar.kilograms = Convert.ToDecimal(weightTxtBox.Text);

            Volume fuelCapacityOfNewCar = new Volume();

            fuelCapacityOfNewCar.litres = Convert.ToDecimal(fuelCapacityTxtBox.Text);

            carServicesObj.Add(vehicleNameTxtBox.Text, numberPlateTxtBox.Text, carTypeOfNewCar,
                               milageOfNewCar, weightOfNewCar, fuelCapacityOfNewCar,
                               Convert.ToInt32(passengerCapacityTxtBox.Text));

            Response.Redirect("CarWebPageView.aspx");
        }
Ejemplo n.º 6
0
            public void AddFirstCar_Success()
            {
                var user = new User {
                    DisplayName = "User 1"
                };
                var mockUser = new Mock <IUserService>();

                mockUser.Setup(u => u.CurrentUser).Returns(user);

                var mock = new Mock <IRepository <Car> >();

                var s = new CarServices(mock.Object, mockUser.Object);

                //mock.Setup(m => m.Add(It.IsAny<Car>()));

                var c = new Car("Toyota");

                c.Owner = user;
                s.Add(c);

                //mock.Verify(r => r.Add(It.IsAny<Car>()), Times.Once);//Check repository was called only 1 time.
                mock.Verify(r => r.Add(c), Times.Once);//Check repository was called only 1 time with this parameter.
            }
Ejemplo n.º 7
0
        public void Add()
        {
            Console.WriteLine("Add car");
            Console.Write("Name: ");
            string vehicleName = Console.ReadLine();

            string numberPlateEntry, numberPlate = null;

            while (numberPlate == null)
            {
                Console.Write("Number plate: ");
                numberPlateEntry = Console.ReadLine().Replace(" ", "");
                if (numberPlateEntry.Length != 7)
                {
                    Console.WriteLine("Invalid numberplate length");
                }
                else
                {
                    numberPlate = numberPlateEntry;
                }
            }

            Console.WriteLine("1. Hatchback");
            Console.WriteLine("2. Sedan");
            Console.WriteLine("3. SUV");
            Console.WriteLine("4. Coupe");
            Console.WriteLine("5. Convertible");

            Car.CarType carType = Car.CarType.Unknown;
            while (carType == Car.CarType.Unknown)
            {
                Console.Write("Car type: ");
                switch (Console.ReadKey(true).KeyChar)
                {
                case '1':
                    carType = Car.CarType.Hatchback;
                    Console.WriteLine("Hatchback");
                    break;

                case '2':
                    carType = Car.CarType.Sedan;
                    Console.WriteLine("Sedan");
                    break;

                case '3':
                    carType = Car.CarType.SUV;
                    Console.WriteLine("SUV");
                    break;

                case '4':
                    carType = Car.CarType.Coupe;
                    Console.WriteLine("Coupe");
                    break;

                case '5':
                    carType = Car.CarType.Convertible;
                    Console.WriteLine("Convertible");
                    break;

                default:
                    Console.WriteLine("Invalid input");
                    break;
                }
            }
            Console.Write("Milage (Miles/Gallon): ");
            FuelEconomy milage = new FuelEconomy();

            milage.milesPerGallon = Convert.ToDecimal(Console.ReadLine());

            Console.Write("Weight (kg): ");
            Weight weight = new Weight();

            weight.kilograms = Convert.ToDecimal(Console.ReadLine());

            Console.Write("Maximum fuel (litres): ");
            Volume maximumFuel = new Volume();

            maximumFuel.litres = Convert.ToDecimal(Console.ReadLine());

            Console.Write("Maximum passengers: ");
            int maximumPassengers = Convert.ToInt32(Console.ReadLine());

            Car addedCar = carServicesObj.Add(vehicleName, numberPlate, carType, milage, weight, maximumFuel,
                                              maximumPassengers);

            SelectById(addedCar.Id);
        }