Jack Moylan scored a first-half hat-trick to put Cardiff in control but the Hammers hit back in fine style after the break.