コード例 #1
0
        public async Task <ActionResult> Add(VehicleModelViewModel model)
        {
            try
            {
                var result = await _modelRepo.GetModelByName(model.Description);

                if (result != null)
                {
                    return(BadRequest("Bilmodellen existerar redan i systemet!"));
                }

                var newModel = new VehicleModel
                {
                    Description = model.Description
                };

                _modelRepo.Add(newModel);
                if (await _modelRepo.SaveAllAsync())
                {
                    return(StatusCode(201, newModel));
                }
                return(StatusCode(500, "Det gick inte att spara ner ny bilmodell"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
コード例 #2
0
        public async Task <ActionResult> AddVehicle(AddVehicleViewModel model)
        {
            var manufacturer = await _makeRepo.GetManufacturerByName(model.Make);

            if (manufacturer == null)
            {
                return(BadRequest($"Tillverkaren {model.Make} finns ej i systemet"));
            }

            var vehicleModel = await _modelRepo.GetModelByName(model.Model);

            if (vehicleModel == null)
            {
                return(BadRequest($"Model beteckning {model.Model} finns ej i systemet"));
            }

            var vehicle = new Vehicle
            {
                VehicleName        = model.Name,
                RegistrationNumber = model.RegNumber,
                FuelType           = model.FuelType,
                GearType           = model.GearType,
                Mileage            = model.Mileage,
                ModelYear          = model.ModelYear,
                MakeId             = manufacturer.Id,
                ModelId            = vehicleModel.Id
            };

            _vehicleRepo.Add(vehicle);
            if (await _vehicleRepo.SaveAllAsync())
            {
                var newVehicle = _mapper.Map <VehicleViewModel>(vehicle);
                return(StatusCode(201, newVehicle));
            }

            return(StatusCode(500, "Gick inte att spara fordonet"));
        }