public async Task TestPostChannel() { var gen = new RandomGenerator(); var model = new ChannelCreateViewModel { Name = gen.Phrase(10), Owner = gen.Phrase(10), HostIpAddress = "127.0.0.1", HostMacAddress = "23:3c:30:e4" }; var result = (CreatedAtRouteNegotiatedContentResult<ChannelResultViewModel>)await controller.PostChannel(model); Assert.IsNotNull(result); Assert.IsNotNull(result.Content); Assert.IsTrue(result.Content.Id > 0); }
public async Task<IHttpActionResult> PostChannel(ChannelCreateViewModel model) { try { model.HostIpAddress = Request.GetClientIpAddress(); model.AuthorId = User.Identity.GetUserId(); if (!ModelState.IsValid) { throw new ValidationException(); } var channel = await _channels.Add((Channel)model); return CreatedAtRoute("DefaultApi", new { id = channel.Id }, (ChannelResultViewModel)channel); } catch (ValidationException) { // All validations errors are already in ModelState. } catch (Exception e) { ModelState.AddModelError(e.GetType().ToString(), e.Message); } return BadRequest(ModelState); }