Ejemplo n.º 1
0
        public async Task <IActionResult> CreateAsync([FromBody] CreateCarRequest request)
        {
            if (request.Brand == Brand.Undefined)
            {
                return(BadRequest(new BadRequestObjectResult($"Undefined brand. Select a valid brand.")));
            }

            if (request.Type == CarType.Undefined)
            {
                return(BadRequest(new BadRequestObjectResult($"Undefined type. Select a valid type.")));
            }

            if (string.IsNullOrEmpty(request.Registration) || request.Registration.Length != 10)
            {
                return(BadRequest(new BadRequestObjectResult($"Registration must have 10 characters.")));
            }

            Car createdCar = await _fleetService.CreateAsync(_mapper.Map <Car>(request));

            if (createdCar == null)
            {
                return(NotFound(new NotFoundObjectResult($"Car was not created.")));
            }

            var locationUri = _uriService.GetCarUri(Request.Path.Value.Substring(1) + "/" + createdCar.Id.ToString());

            return(Created(locationUri, new Response <CarResponse>(_mapper.Map <CarResponse>(createdCar))));
        }