private IHttpActionResult ValidatePicture(PictureModel p) { var dbContext = new ApplicationDbContext(); if (p.ApplicationUserId == null || !dbContext.Users.Any(x => x.Id == p.ApplicationUserId)) { return BadRequest("ApplicationUserId NOT FOUND"); } if (p.CategoryId == null || !dbContext.Categories.Any(x => x.CategoryId == p.CategoryId)) { return BadRequest("CategoryId NOT FOUND"); } if (p.Link == null || !p.Link.StartsWith("https://")) { return BadRequest("Invalid Url"); } return Ok(); }
public IHttpActionResult PutPicture(PictureModel picture) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // TODO Add validations this.data.Pictures.Find(picture.Id).Link = picture.Link; try { this.data.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PictureExists(picture.Id)) { return NotFound(); } else { throw new ArgumentException("The picture Id wasn't valid"); } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostPicture(PictureModel p) { if (!ModelState.IsValid) { return BadRequest(ModelState); } this.data.Pictures .Add(new Picture() { CategoryId = p.CategoryId, ApplicationUserId = p.ApplicationUserId, Link = p.Link }); this.data.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = p.Id }, p); }