예제 #1
0
        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);
                }
            }
        }
예제 #2
0
        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));
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
        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);
        }