Exemplo n.º 1
0
        public void Insert(CarModel model, CarMake make)
        {
            var makesRepo = new CarMakesRepositoryADO();

            var makeModel = makesRepo.GetById(make.MakeId);

            if (makeModel == null)
            {
                makesRepo.Insert(make);

                make = makesRepo.GetAll().Where(m => m.Name == make.Name).FirstOrDefault();
            }

            else
            {
                make = makeModel;
            }

            using (var cn = new SqlConnection(Settings.GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand("CarModelsInsert", cn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter param = new SqlParameter("@ModelId", SqlDbType.Int);
                param.Direction = ParameterDirection.Output;

                cmd.Parameters.Add(param);

                cmd.Parameters.AddWithValue("@MakeId", make.MakeId);
                cmd.Parameters.AddWithValue("@Name", model.Name);

                cn.Open();

                cmd.ExecuteNonQuery();

                model.ModelId = (int)param.Value;
            }
        }
Exemplo n.º 2
0
        public void Update(Vehicle vehicle, CarModel model, CarMake make)
        {
            var vehicleRepo = new VehiclesRepositoryADO();
            var modelRepo   = new CarModelsRepositoryADO();
            var makeRepo    = new CarMakesRepositoryADO();

            var submittedModel = modelRepo.GetAll().Where(m => m.Name.ToLower() == model.Name.ToLower()).ToList();

            var submittedMake = makeRepo.GetAll().Where(m => m.Name.ToLower() == make.Name.ToLower()).ToList();

            if (submittedModel.Count() == 0)
            {
                if (submittedMake.Count() == 0)
                {
                    makeRepo.Insert(make);

                    make = makeRepo.GetAll().Where(m => m.Name.ToLower() == make.Name.ToLower()).ToList().FirstOrDefault();
                }

                else
                {
                    model.MakeId = submittedMake.FirstOrDefault().MakeId;
                }

                modelRepo.Insert(model, make);

                model = modelRepo.GetAll().Where(m => m.Name.ToLower() == model.Name.ToLower()).ToList().FirstOrDefault();
            }

            else
            {
                model = submittedModel.FirstOrDefault();
            }

            if (vehicle.ImageFileName == null)
            {
                vehicle.ImageFileName = vehicleRepo.GetById(vehicle.VehicleId).ImageFileName;
            }

            using (var cn = new SqlConnection(Settings.GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand("VehiclesUpdate", cn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@VehicleId", vehicle.VehicleId);
                cmd.Parameters.AddWithValue("@VIN", vehicle.VIN);
                cmd.Parameters.AddWithValue("@ModelId", model.ModelId);
                cmd.Parameters.AddWithValue("@ModelYear", vehicle.ModelYear);
                cmd.Parameters.AddWithValue("@Mileage", vehicle.Mileage);
                cmd.Parameters.AddWithValue("@TransmissionTypeId", vehicle.TransmissionTypeId);
                cmd.Parameters.AddWithValue("@BodyStyleId", vehicle.BodyStyleId);
                cmd.Parameters.AddWithValue("@ColorId", vehicle.ColorId);
                cmd.Parameters.AddWithValue("@InteriorColorId", vehicle.InteriorColorId);
                cmd.Parameters.AddWithValue("@VehicleTypeId", vehicle.VehicleTypeId);
                cmd.Parameters.AddWithValue("@Description", vehicle.Description);
                cmd.Parameters.AddWithValue("@ImageFileName", vehicle.ImageFileName);
                cmd.Parameters.AddWithValue("@MSRP", vehicle.MSRP);
                cmd.Parameters.AddWithValue("@SalePrice", vehicle.SalePrice);
                cmd.Parameters.AddWithValue("@EmployeeId", vehicle.EmployeeId);
                cmd.Parameters.AddWithValue("@IsFeatured", vehicle.IsFeatured);

                cn.Open();

                cmd.ExecuteNonQuery();
            }
        }