Ejemplo n.º 1
0
        public async Task <ActionResult> EditCar(string id, [FromBody] CarInputViewModel car)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(new { Message = "Ooopppsss! Something wrong!", this.ModelState, car }));
            }

            var editCar = await this._carService.EditCar(id, car);

            return(this.Created("", new { Message = "Car edited!", editCar }));
        }
Ejemplo n.º 2
0
        public async Task <CarOutputViewModel> EditCar(string carId, CarInputViewModel car)
        {
            var currentCar = await this._dbContext.Cars.FirstOrDefaultAsync(x => x.Id == carId);

            if (currentCar != null)
            {
                var extras = await this._dbContext.Extras.Where(x => x.CarId == currentCar.Id).ToListAsync();

                var images = await this._dbContext.Images.Where(x => x.CarId == currentCar.Id).ToListAsync();

                this._dbContext.Extras.RemoveRange(extras);
                this._dbContext.Images.RemoveRange(images);
                this._dbContext.Cars.Remove(currentCar);

                currentCar = new Car
                {
                    Id          = carId,
                    Make        = car.Make,
                    Model       = car.Model,
                    Description = car.Description,
                    Price       = car.Price,
                    CreatedOn   = DateTime.UtcNow,
                    UserId      = car.UserId,
                };

                foreach (var extra in car.Extras)
                {
                    extras.Add(new Extra
                    {
                        Name  = extra.Name,
                        CarId = currentCar.Id,
                    });
                }

                foreach (var image in car.Images)
                {
                    images.Add(new Image
                    {
                        ImageSrc = image.ImageSrc,
                        CarId    = currentCar.Id,
                    });
                }

                currentCar.Extras = extras;

                await this._dbContext.Cars.AddAsync(currentCar);

                await this._dbContext.SaveChangesAsync();
            }

            return(this._mapper.Map <CarOutputViewModel>(currentCar));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> CreateNewCar(CarInputViewModel car)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(new { Message = "Ooopppsss! Something wrong!", this.ModelState, car }));
            }

            //  var userId = this._userManager.GetUserAsync(this.User).Id.ToString();

            var newCar = await this._carService.CreateNewCar(car, car.UserId);

            return(this.Created("", new { Message = "Car created!", newCar }));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create(CarInputViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                input.AllTypes   = this.carsService.GetFuelType();
                input.AllDrivers = this.usersService.GetAll(input.CompanyId);

                return(this.View(input));
            }

            var car = await this.carsService.CreateAsync(input.Model, input.LicensePlate, input.FuelType, input.StartKilometers, input.AverageConsumption, input.TankCapacity, input.InitialFuel, input.CompanyId);

            await this.carUsersService.UpdateAsync(car.Id, car.CompanyId, input.FullName);

            return(this.RedirectToAction("All", "Cars"));
        }
Ejemplo n.º 5
0
        public IActionResult Create()
        {
            var companyId = this.userManager.GetUserAsync(this.User).Result?.CompanyId;

            var viewModel = new CarInputViewModel
            {
                AllTypes   = this.carsService.GetFuelType(),
                AllDrivers = this.usersService.GetAll(companyId),
            };

            if (viewModel == null)
            {
                return(this.NotFound());
            }

            return(this.View(viewModel));
        }
Ejemplo n.º 6
0
        public async Task <CarOutputViewModel> CreateNewCar(CarInputViewModel car, string userId)
        {
            var newCar = new Car
            {
                Make        = car.Make,
                Model       = car.Model,
                Description = car.Description,
                Price       = car.Price,
                CreatedOn   = DateTime.UtcNow,
                UserId      = userId,
            };

            foreach (var extra in car.Extras)
            {
                newCar.Extras.Add(new Extra
                {
                    Name  = extra.Name,
                    CarId = newCar.Id,
                });
            }

            foreach (var image in car.Images)
            {
                newCar.Images.Add(new Image
                {
                    ImageSrc = image.ImageSrc,
                    CarId    = newCar.Id,
                });
            }

            await this._dbContext.Cars.AddAsync(newCar);

            await this._dbContext.SaveChangesAsync();

            return(this._mapper.Map <CarOutputViewModel>(newCar));
        }