コード例 #1
0
ファイル: CarModelsProvider.cs プロジェクト: bmtuaf/Challenge
        public async Task <(bool IsSuccess, Models.ViewModels.CarModel CarModel, string ErrorMessage)> PostCarModelAsync(Models.RequestModels.CarModelRequest carModel)
        {
            try
            {
                var newCarModel = new DB.CarModel()
                {
                    MakeID             = carModel.MakeID,
                    Name               = carModel.Name,
                    VehicleCategoryID  = carModel.VehicleCategoryID,
                    FuelTypeID         = carModel.FuelTypeID,
                    TrunkLimit         = carModel.TrunkLimit,
                    RentalPricePerHour = carModel.RentalPricePerHour
                };
                dBContext.Add(newCarModel);
                if (await dBContext.SaveChangesAsync() > 0)
                {
                    newCarModel = await dBContext.CarModels.Include(m => m.Make).Include(c => c.VehicleCategory).Include(f => f.FuelType).FirstOrDefaultAsync(m => m.ID == newCarModel.ID);

                    var result = mapper.Map <DB.CarModel, Models.ViewModels.CarModel>(newCarModel);
                    return(true, result, null);
                }
                return(false, null, "Failed to insert record.");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex.ToString());
                return(false, null, ex.Message);
            }
        }
コード例 #2
0
ファイル: VehiclesProvider.cs プロジェクト: bmtuaf/Challenge
        public async Task <(bool IsSuccess, Models.ViewModels.Vehicle Vehicle, string ErrorMessage)> PostVehicleAsync(VehicleRequest vehicle)
        {
            try
            {
                var newVehicle = new DB.Vehicle()
                {
                    CarModelID   = vehicle.CarModelID,
                    LicensePlate = vehicle.LicensePlate,
                    ModelYear    = vehicle.ModelYear
                };
                dBContext.Add(newVehicle);
                if (await dBContext.SaveChangesAsync() > 0)
                {
                    newVehicle = await dBContext.Vehicles.Include(m => m.CarModel).Include(m => m.CarModel.Make).Include(m => m.CarModel.VehicleCategory).Include(c => c.CarModel.FuelType).FirstOrDefaultAsync(v => v.ID == newVehicle.ID);

                    var result = mapper.Map <DB.Vehicle, Models.ViewModels.Vehicle>(newVehicle);
                    return(true, result, null);
                }
                return(false, null, "Failed to insert record.");
            }
            catch (Exception ex)
            {
                logger?.LogError(ex.ToString());
                return(false, null, ex.Message);
            }
        }
コード例 #3
0
ファイル: MakesProvider.cs プロジェクト: bmtuaf/Challenge
 public async Task <(bool IsSuccess, Models.ViewModels.Make Make, string ErrorMessage)> PostMakeAsync(Models.RequestModels.MakeRequest make)
 {
     try
     {
         var newMake = new DB.Make()
         {
             Name = make.Name
         };
         dBContext.Add(newMake);
         if (await dBContext.SaveChangesAsync() > 0)
         {
             var result = mapper.Map <DB.Make, Models.ViewModels.Make>(newMake);
             return(true, result, null);
         }
         return(false, null, "Failed to insert record.");
     }
     catch (Exception ex)
     {
         logger?.LogError(ex.ToString());
         return(false, null, ex.Message);
     }
 }