Exemple #1
0
        public IActionResult Advertise(VehicleDto dto)
        {
            var existingVehicle = db.FindVehicle(dto.Registration);

            if (existingVehicle != default)
            {
                ModelState.AddModelError(nameof(dto.Registration),
                                         "That registration is already listed in our database.");
            }
            var vehicleModel = db.FindModel(dto.ModelCode);

            if (vehicleModel == default)
            {
                ModelState.AddModelError(nameof(dto.ModelCode),
                                         $"Sorry, {dto.ModelCode} is not a valid model code.");
            }
            if (!ModelState.IsValid)
            {
                return(View(dto));
            }
            var vehicle = new Vehicle()
            {
                Registration = dto.Registration,
                Color        = dto.Color,
                VehicleModel = vehicleModel,
                Year         = dto.Year
            };

            db.CreateVehicle(vehicle);
            return(RedirectToAction("Details", new { id = vehicle.Registration }));
        }
        public IActionResult Post([FromBody] VehicleDto dto)
        {
            var vehicleModel = db.FindModel(dto.ModelCode);
            var vehicle      = new Vehicle {
                Registration = dto.Registration,
                Color        = dto.Color,
                Year         = dto.Year,
                VehicleModel = vehicleModel
            };

            db.CreateVehicle(vehicle);
            return(Ok(dto));
        }
Exemple #3
0
        public async Task <IActionResult> Post([FromBody] VehicleDto dto)
        {
            // If the vehicle already exists, return a 409 Conflict
            var existing = db.FindVehicle(dto.Registration);

            if (existing != default)
            {
                return(Conflict($"Sorry - we already have a car with registration {dto.Registration} in our database!"));
            }
            var vehicleModel = db.FindModel(dto.ModelCode);
            var vehicle      = new Vehicle {
                Registration = dto.Registration,
                Color        = dto.Color,
                Year         = dto.Year,
                VehicleModel = vehicleModel
            };

            db.CreateVehicle(vehicle);
            bus.PublishNewVehicleMessage(vehicle);
            return(Created($"/api/vehicles/{vehicle.Registration}", dto));
        }