Skip to content

Instantly share code, notes, and snippets.

@TheFo2sh
Created February 23, 2020 19:50
Show Gist options
  • Save TheFo2sh/4f1638eca01f4bd45f5788923fd6df64 to your computer and use it in GitHub Desktop.
Save TheFo2sh/4f1638eca01f4bd45f5788923fd6df64 to your computer and use it in GitHub Desktop.
public Option<ReservationViewModel> AddItemToReservation(string id, string itemId)
{
var reservationOptinal = _restaurant.GetReservation(id);
var itemOptional = _inventory.GetItem(itemId);
return reservationOptinal.Intersect(itemOptional)
.Select(AddItemFunc)
.Select(reservation => (reservation, _ClientsManger.GetUser(reservation.UserId)))
.Select(MapToViewModel);
Reservation AddItemFunc(( Reservation , Item ) tuple)
{
var (reservation, item) = tuple;
reservation.Items.Add(item);
return reservation;
}
ReservationViewModel MapToViewModel((Reservation , Option<User>) tuple)
{
var (reservation, userOption) = tuple;
return new ReservationViewModel()
{
From = reservation.From,
User = userOption.Select(user => $"{user.FirstName} {user.SecondName}").ValueOr("Not a client"),
Id = reservation.Id,
Name = reservation.Name,
To = reservation.To
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment