Ejemplo n.º 1
0
        public async Task <int> AddCarAsync(Car car)
        {
            //check if car exists.
            var existingCar = await _carRepository.GetCarByMakeModelAndYearAsync(car.Make, car.Model, car.Year);

            if (existingCar == null)
            {
                var carId = await _carRepository.CreateCarAsync(car);

                return(carId);
            }
            return(0);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <CarReadDto> > CreateCarAsync([Bind("Make,Model,Year,Color,VIN")] CarCreateDto carCreateDto)
        {
            if (ModelState.IsValid)
            {
                var carModel = _mapper.Map <Car>(carCreateDto);

                await _repository.CreateCarAsync(carModel);

                await _repository.SaveChangesAsync();

                var carReadDto = _mapper.Map <CarReadDto>(carModel);

                return(CreatedAtRoute(nameof(GetCarByIdAsync), new { Id = carReadDto.Id }, carReadDto));
            }

            return(BadRequest());
        }
Ejemplo n.º 3
0
        public async Task <ServiceResult> CreateCarAsync(Guid userId, Car car)
        {
            if (car.MainName == null)
            {
                GenerateMainName(car);
            }

            if (!await _carRepository.CreateCarAsync(car))
            {
                return(FailResult(400, "Couldn't create a car"));
            }

            var userCar = new UserCar {
                UserId = userId, CarId = car.CarId, UserCarRoleId = "owner"
            };

            if (!await _userCarRepository.CreateUserCarAsync(userCar))
            {
                return(FailResult(400, "Couldn't create a car"));
            }

            return(SuccessResult(201, _mapper.Map <CreateCarResponse>(car)));
        }