public async Task <ApiResponse <Guid> > Post([FromBody] CarEditDto newCar) =>
 await ValidateCarDto(newCar)
 .Map(CarMapper.MapToNewCar)
 .BindAsync(car => Storage.Create(car))
 .MapAsync(car => car.Id)
 .DoWhenErrorAsync(e => Logger.Warn(e.Message, e.Exception))
 .ToResponseAsync();
 public static Result <CarEditDto> ValidateCarDto(CarEditDto Car) =>
 Car
 .ToResult(() => Errors.Error("Car cannot be null"))
 .Bind(f => string.IsNullOrEmpty(f.Name) ? Error <CarEditDto>(Errors.Error("Name must be filled in")) : Ok(f))
 .Bind(f => string.IsNullOrEmpty(f.Make) ? Error <CarEditDto>(Errors.Error("Make must be filled in")) : Ok(f))
 .Bind(f => string.IsNullOrEmpty(f.Model) ? Error <CarEditDto>(Errors.Error("Model must be filled in")) : Ok(f))
 .Bind(f => string.IsNullOrEmpty(f.PrimaryFuel) ? Error <CarEditDto>(Errors.Error("PrimaryFuel must be filled in")) : Ok(f))
 .Bind(f => !Enum.TryParse <FuelType>(f.PrimaryFuel, out var _) ? Error <CarEditDto>(Errors.Error("PrimaryFuel must have correct value")) : Ok(f))
Beispiel #3
0
 public static Car MapToCar(Guid id, CarEditDto dto) =>
 new Car
 {
     Id            = id,
     Name          = dto.Name,
     Make          = dto.Make,
     Model         = dto.Model,
     PrimaryFuel   = Enum.Parse <FuelType>(dto.PrimaryFuel),
     SecondaryFuel = dto.SecondaryFuel.ToOption().Map(f => Enum.Parse <FuelType>(f))
 };
 public async Task <ApiResponse <Unit> > Put(string carId, [FromBody] CarEditDto updatedCar) =>
 await GuidParser.ParseGuid(carId)