Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("CarModelId,Manufacturer,Model,AddInformations")] CarModel carModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(carModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(carModel));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("CarId,ProductionYear,Mileage,AccidentFree,Price,CarModelId, Discount")] Car car)
        {
            if (ModelState.IsValid)
            {
                _context.Add(car);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CarModelId"] = new SelectList(_context.CarModels, "CarModelId", "Manufacturer", car.CarModelId);
            return(View(car));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            vehicle.LastUpdated = DateTime.Now;
            context.Vehicles.Add(vehicle);
            await context.SaveChangesAsync();

            var result = mapper.Map <Vehicle, VehicleResource>(vehicle);

            return(Ok(result));
        }
        private static CarDealerDbContext AddToContext <T>(CarDealerDbContext context,
                                                           T entity, int count, int commitCount, bool recreateContext, int totalElements, DateTime startTime) where T : class
        {
            Task.Run(async() =>
            {
                await context.Set <T>().AddRangeAsync(entity);
            }).Wait();

            if (count % commitCount == 0)
            {
                Task.Run(async() =>
                {
                    await context.SaveChangesAsync();
                }).Wait();
                if (recreateContext)
                {
                    var percent = ((double)count / totalElements) * 100;
                    context.Dispose();
                    context = new CarDealerDbContext();
                    if (count == 500)
                    {
                        Console.WriteLine("Data is being seeded... Please wait!");
                    }
                    if (count % 3000 == 0)
                    {
                        var timeSpent = DateTime.Now - startTime;
                        var percentForCalculatingRemainingTime = (double)count / totalElements;
                        var totalTime     = (1.0 / percentForCalculatingRemainingTime) * timeSpent;
                        var remainingTime = totalTime - timeSpent;

                        if (remainingTime.Hours > 0)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%h} hours {1:%m} minutes and {1:%s} seconds.", percent, remainingTime);
                        }
                        if (remainingTime.Hours == 0 && remainingTime.Minutes > 1 && remainingTime.Seconds > 1)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%m} minutes and {1:%s} seconds.", percent, remainingTime);
                        }

                        switch (remainingTime.Minutes)
                        {
                        case 1 when(remainingTime.Hours == 0 && remainingTime.Seconds > 1):
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%m} minute and {1:%s} seconds.",
                                              percent, remainingTime);
                            break;

                        case 0 when(remainingTime.Hours == 0 && remainingTime.Seconds > 1):
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%s} seconds.", percent,
                                              remainingTime);
                            break;
                        }

                        if (remainingTime.Seconds == 1 && remainingTime.Hours == 0 && remainingTime.Minutes == 0)
                        {
                            Console.WriteLine("Completed: {0:f2}%. Remaining time: {1:%s} second.", percent, remainingTime);
                        }
                    }
                    context.ChangeTracker.AutoDetectChangesEnabled = false;
                }
            }

            return(context);
        }