public byte[] GetMemberPhoto(PhotoRequestModel model) { using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString)) { var qry = dbc.Members.AsQueryable(); OrgComm.Data.Models.Member member = null; if (model.Id.HasValue) { member = qry.Where(r => (!r.DelFlag) && (r.Id == model.Id.Value)).FirstOrDefault(); } else { throw new OrgException("Invalid id"); } if (member == null) { throw new OrgException("Member not found"); } else { if (member.Photo == null) { throw new OrgException("Photo not found"); } return(member.Photo); } } }
public IHttpActionResult Post(PhotoRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var album = this.albums .All() .FirstOrDefault(a => a.Id == model.AlbumId); if (album == null) { return(this.NotFound()); } var newPhoto = new Photo { Title = model.Title, ImageUrl = model.ImageUrl, FileExtension = model.FileExtension, UploadDate = DateTime.Now, Likes = 0, AlbumId = model.AlbumId }; this.photos.Add(newPhoto); this.photos.SaveChanges(); return(this.Ok(newPhoto.Id)); }
public void PostWithInvalidModelStateShouldReturnInvalidModelStateResult() { var controller = ControllerMockFactory.GetPhotosController(this.photos, this.albums); controller.ModelState.AddModelError("url", "Invalid url!"); var photo = new PhotoRequestModel(); var result = controller.Post(photo); var expectedSaveChanges = 0; var expectedNumberOfPhotos = TestConstants.DefaultNumberOfModels; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(InvalidModelStateResult)); Assert.AreEqual(expectedNumberOfPhotos, this.photos.NumberOfModels); Assert.AreEqual(expectedSaveChanges, this.photos.NumberOfSaves); }
public void PostWithInvalidAlbumIdShouldReturnNotFoundResult() { var controller = ControllerMockFactory.GetPhotosController(this.photos, this.albums); var photo = new PhotoRequestModel { Title = TestConstants.ValidTitle, ImageUrl = TestConstants.ValidUrl, FileExtension = TestConstants.ValidFileExtension, AlbumId = TestConstants.DefaultNonExistingModelId }; var result = controller.Post(photo); var expectedSaveChanges = 0; var expectedNumberOfPhotos = TestConstants.DefaultNumberOfModels; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); Assert.AreEqual(expectedNumberOfPhotos, this.photos.NumberOfModels); Assert.AreEqual(expectedSaveChanges, this.photos.NumberOfSaves); }
public void PostWithValidDataShouldReturnOkResult() { var controller = ControllerMockFactory.GetPhotosController(this.photos, this.albums); var photo = new PhotoRequestModel { Title = TestConstants.ValidTitle, ImageUrl = TestConstants.ValidUrl, FileExtension = TestConstants.ValidFileExtension, AlbumId = 0 }; var result = controller.Post(photo); var expectedNumberOfPhotos = 16; var expectedSaveChanges = 1; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <int>)); Assert.AreEqual(expectedNumberOfPhotos, this.photos.NumberOfModels); Assert.AreEqual(expectedSaveChanges, this.photos.NumberOfSaves); }
public IHttpActionResult Post(PhotoRequestModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var album = this.albums .All() .FirstOrDefault(a => a.Id == model.AlbumId); if (album == null) { return this.NotFound(); } var newPhoto = new Photo { Title = model.Title, ImageUrl = model.ImageUrl, FileExtension = model.FileExtension, UploadDate = DateTime.Now, Likes = 0, AlbumId = model.AlbumId }; this.photos.Add(newPhoto); this.photos.SaveChanges(); return this.Ok(newPhoto.Id); }
public void PostWithValidDataShouldReturnOkResult() { var controller = ControllerMockFactory.GetPhotosController(this.photos, this.albums); var photo = new PhotoRequestModel { Title = TestConstants.ValidTitle, ImageUrl = TestConstants.ValidUrl, FileExtension = TestConstants.ValidFileExtension, AlbumId = 0 }; var result = controller.Post(photo); var expectedNumberOfPhotos = 16; var expectedSaveChanges = 1; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<int>)); Assert.AreEqual(expectedNumberOfPhotos, this.photos.NumberOfModels); Assert.AreEqual(expectedSaveChanges, this.photos.NumberOfSaves); }