Beispiel #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;
            }
        }