Esempio n. 1
0
        public async Task <Garage> CreateAsync(Garage garage)
        {
            if (garage == null)
            {
                throw new ArgumentException();
            }

            if (!garage.AreaId.HasValue)
            {
                throw new ArgumentException(nameof(garage.AreaId));
            }

            if (_garageRepository.GetByIdAsync(garage.Id).Result != null)
            {
                throw new Exception("Bad Request: garage already exists");
            }

            if (!_areaRepository.AnyAsync(a => a.Id == garage.AreaId).Result)
            {
                throw new Exception("Bad Request: area does not exist");
            }

            if (garage.Cars != null)
            {
                foreach (var car in garage.Cars)
                {
                    if (!car.CategoryId.HasValue || !_carCategoryRepository.AnyAsync(c => c.Id == car.CategoryId.Value).Result)
                    {
                        throw new Exception($"Bad Request: carCategory of {car.Title} does not exist");
                    }
                }
            }

            var garageEntity = Mapper.Map <GarageEntity>(garage);

            _garageRepository.Add(garageEntity);

            try
            {
                await _garageRepository.SaveChangesAsync();

                return(Mapper.Map <Garage>(garageEntity));
            }
            catch (Exception e)
            {
                // log error
                throw e;
            }
        }
Esempio n. 2
0
        public async Task <Car> CreateAsync(Car car)
        {
            if (car == null)
            {
                throw new ArgumentException(nameof(car));
            }

            if (!car.CategoryId.HasValue)
            {
                throw new ArgumentException(nameof(car.CategoryId));
            }

            if (!car.GarageId.HasValue)
            {
                throw new ArgumentException(nameof(car.GarageId));
            }

            if (!_garageRepository.AnyAsync(g => g.Id == car.GarageId).Result)
            {
                throw new Exception("Bad Request: garage is not exists");
            }

            if (!_carCategoryRepository.AnyAsync(c => c.Id == car.CategoryId).Result)
            {
                throw new Exception("Bad Request: car category is not exists");
            }

            var carEntity = new CarEntity
            {
                DateCreated = DateTime.Now
            };

            PopulateCarData(carEntity, car);

            _carRepository.Add(carEntity);
            await _carRepository.SaveChangesAsync();

            return(Mapper.Map <Car>(carEntity));
        }