コード例 #1
0
        public async Task <IActionResult> LikePhoto(int userId, int photoId)
        {
            var like = await this.repo.GetLikePhoto(userId, photoId);

            if (like != null)
            {
                return(BadRequest("You already like this photo"));
            }

            if (await this.repo.GetPhoto(photoId) == null)
            {
                return(NotFound());
            }

            like = new PhotoLike
            {
                PhotoId = photoId,
                LikerId = userId
            };

            this.repo.Add <PhotoLike>(like);

            if (await this.repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to like photo"));
        }
コード例 #2
0
        public async Task <Like> LikePhoto(string photoId, string albumId, User requestor)
        {
            if (!await _permissionsService.CanLike(requestor, albumId))
            {
                return(null);
            }
            var photo = await _photoService.GetPhoto(photoId);

            var like = new Like
            {
                ReactionId   = _guid.NewGuid().ToString(),
                Reactor      = requestor,
                ReactionDate = _clock.UtcNow
            };
            var photoLike = new PhotoLike
            {
                Photo = photo,
                Like  = like
            };

            photo.PhotoLikes.Add(photoLike);
            await _unitOfWork.Photos.Update(photo);

            return(like);
        }
コード例 #3
0
        public void DeleteUser_AndPhotoAndPhotoLike_Cascade()
        {
            // Arrange
            Photo photo = new Photo()
            {
                Path = "1/54/23.jpg"
            };
            User user = new User()
            {
                NickName = "Saimon", Password = "******"
            };
            PhotoLike photoLike = new PhotoLike()
            {
                IsLiked = true, Photo = photo
            };

            user.Photos.Add(photo);
            user.PhotoLikes.Add(photoLike);

            // Act
            dbContext.Users.Add(user);
            dbContext.Users.Remove(user);
            dbContext.SaveChanges();

            // Assert
            CollectionAssert.DoesNotContain(dbContext.Users.ToArray(), user);
            CollectionAssert.DoesNotContain(dbContext.Photos.ToArray(), photo);
            CollectionAssert.DoesNotContain(dbContext.PhotoLike.ToArray(), photoLike);
        }
コード例 #4
0
        public void Equals_SamePhoto_True()
        {
            // Arrange
            Photo photo1 = new Photo {
                Path = "photo name"
            };
            Photo photo2 = new Photo {
                Path = "photo name"
            };

            PhotoLike photoLike1 = new PhotoLike()
            {
                IsLiked = true, Photo = photo1
            };
            PhotoLike photoLike2 = new PhotoLike()
            {
                IsLiked = true, Photo = photo2
            };

            // Act
            // Assert
            Assert.IsTrue(photoLike1.Equals(photoLike2));
            Assert.AreEqual(photoLike1, photoLike2);
            Assert.AreNotSame(photoLike1, photoLike2);
        }
コード例 #5
0
        public void Equals_DifferentLikes_False()
        {
            // Arrange
            Photo photo1 = new Photo {
                Path = "photo name 1"
            };
            Photo photo2 = new Photo {
                Path = "photo name 2"
            };

            PhotoLike photoLike1 = new PhotoLike()
            {
                IsLiked = true, Photo = photo1
            };
            PhotoLike photoLike2 = new PhotoLike()
            {
                IsLiked = true, Photo = photo2
            };

            // Act
            // Assert
            Assert.IsFalse(photoLike1.Equals(photoLike2));
            Assert.AreNotEqual(photoLike1, photoLike2);
            Assert.AreNotSame(photoLike1, photoLike2);
        }
コード例 #6
0
ファイル: PhotoBinder.cs プロジェクト: shiyibin/wojilu
        public static List <int> GetLikedIds(List <PhotoPost> list, int userId)
        {
            List <int> ids = new List <int>();

            if (list.Count == 0)
            {
                return(ids);
            }

            String postIds = "";

            foreach (PhotoPost x in list)
            {
                postIds += x.Id + ",";
            }
            postIds = postIds.TrimEnd(',');

            List <PhotoLike> likeList = PhotoLike.find("UserId=" + userId + " and PostId in (" + postIds + ")").list();

            foreach (PhotoLike x in likeList)
            {
                ids.Add(x.Post.Id);
            }

            return(ids);
        }
コード例 #7
0
 public virtual PhotoLike GetOne(long userId, long postId)
 {
     return(PhotoLike.find("PostId=:pid and UserId=:uid")
            .set("pid", postId)
            .set("uid", userId)
            .first());
 }
コード例 #8
0
        public void Equals_SameUser_True()
        {
            // Arrange
            User user1 = new User {
                NickName = "User"
            };
            User user2 = new User {
                NickName = "User"
            };

            PhotoLike photoLike1 = new PhotoLike()
            {
                IsLiked = true, User = user1
            };
            PhotoLike photoLike2 = new PhotoLike()
            {
                IsLiked = true, User = user2
            };

            // Act
            // Assert
            Assert.IsTrue(photoLike1.Equals(photoLike2));
            Assert.AreEqual(photoLike1, photoLike2);
            Assert.AreNotSame(photoLike1, photoLike2);
        }
コード例 #9
0
        public void DeletePhotoLike()
        {
            // Arrange
            Photo photo = new Photo()
            {
                Path = "1/54/23.jpg"
            };
            User user = new User()
            {
                NickName = "John", Password = "******"
            };
            PhotoLike photoLike = new PhotoLike()
            {
                IsLiked = true, Photo = photo
            };

            user.Photos.Add(photo);
            user.PhotoLikes.Add(photoLike);

            // Act
            dbContext.Users.Add(user);
            dbContext.PhotoLike.Remove(photoLike);
            dbContext.SaveChanges();

            // Assert
            CollectionAssert.Contains(dbContext.Users.ToArray(), user);
            CollectionAssert.Contains(dbContext.Photos.ToArray(), photo);
            CollectionAssert.DoesNotContain(dbContext.PhotoLike.ToArray(), photoLike);

            CollectionAssert.DoesNotContain(dbContext.Users.First(u => u.NickName == user.NickName).PhotoLikes.ToArray(), photoLike);
            CollectionAssert.DoesNotContain(dbContext.Photos.First(p => p.Path == photo.Path).Likes.ToArray(), photoLike);
        }
コード例 #10
0
        public async Task <IActionResult> PutPhotoLike(Guid id, PhotoLike photoLike)
        {
            if (id != photoLike.PhotoId)
            {
                return(BadRequest());
            }

            _context.Entry(photoLike).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PhotoLikeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public IHttpActionResult LikePost(int photoId)
        {
            var currentUserId = this.UserIdProvider.GetUserId();
            var currentUser   = this.Data.Users.Find(currentUserId);
            var photo         = this.Data.Photos.Find(photoId);

            if (photo == null)
            {
                return(this.NotFound());
            }

            if (!this.HasAuthorization(currentUser, photo))
            {
                return(this.BadRequest("Unable to like that photo. You can like photos of your friends only."));
            }

            if (photo.Likes.Any(p => p.UserId == currentUserId))
            {
                return(this.BadRequest("You have already like this photo."));
            }

            PhotoLike photoLike = new PhotoLike
            {
                PhotoId = photo.Id,
                UserId  = currentUserId
            };

            this.Data.PhotoLikes.Add(photoLike);
            this.Data.SaveChanges();

            return(this.Ok());
        }
コード例 #12
0
        public void Equals_DifferentUser_False()
        {
            // Arrange
            User user1 = new User {
                NickName = "User 1"
            };
            User user2 = new User {
                NickName = "User 2"
            };

            PhotoLike photoLike1 = new PhotoLike()
            {
                IsLiked = true, User = user1
            };
            PhotoLike photoLike2 = new PhotoLike()
            {
                IsLiked = true, User = user2
            };

            // Act
            // Assert
            Assert.IsFalse(photoLike1.Equals(photoLike2));
            Assert.AreNotEqual(photoLike1, photoLike2);
            Assert.AreNotSame(photoLike1, photoLike2);
        }
コード例 #13
0
 public PhotoLike GetOne(int userId, int postId)
 {
     return(PhotoLike.find("PostId=:pid and UserId=:uid")
            .set("pid", postId)
            .set("uid", userId)
            .first());
 }
コード例 #14
0
        public async Task <IActionResult> Edit(Guid id, [Bind("PhotoId,LikeId")] PhotoLike photoLike)
        {
            if (id != photoLike.PhotoId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(photoLike);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PhotoLikeExists(photoLike.PhotoId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["LikeId"]  = new SelectList(_context.Like, "Id", "Id", photoLike.LikeId);
            ViewData["PhotoId"] = new SelectList(_context.Photos, "Id", "Id", photoLike.PhotoId);
            return(View(photoLike));
        }
コード例 #15
0
        public void DeleteUser_AndPhotoAndPhotoLikeAndCommentAndCommentLike_Cascade()
        {
            // Arrange
            Photo photo1 = new Photo()
            {
                Path = "1/54/23.jpg"
            };
            Photo photo2 = new Photo()
            {
                Path = "1/54/24.jpg"
            };

            User user1 = new User()
            {
                NickName = "John",
                Password = "******",
                Photos   = new List <Photo> {
                    photo1, photo2
                }
            };
            Comment comment = new Comment()
            {
                Text  = "Comment text",
                Date  = DateTime.Now,
                Photo = photo1,
                User  = user1
            };
            CommentLike commentLike = new CommentLike()
            {
                IsLiked = true, Comment = comment
            };
            PhotoLike photoLike = new PhotoLike()
            {
                IsLiked = true, Photo = photo1
            };
            User user2 = new User()
            {
                NickName = "Adam", Password = "******", CommentLikes = new List <CommentLike> {
                    commentLike
                }
            };

            // Act
            dbContext.Users.Add(user1);
            dbContext.Users.Add(user2);
            dbContext.Users.Remove(user1);
            dbContext.SaveChanges();

            // Assert
            CollectionAssert.Contains(dbContext.Users.ToArray(), user2);
            CollectionAssert.DoesNotContain(dbContext.Users.ToArray(), user1);
            CollectionAssert.DoesNotContain(dbContext.Photos.ToArray(), photo2);
            CollectionAssert.DoesNotContain(dbContext.Photos.ToArray(), photo1);
            CollectionAssert.DoesNotContain(dbContext.PhotoLike.ToArray(), photoLike);
            CollectionAssert.DoesNotContain(dbContext.Comments.ToArray(), comment);
            CollectionAssert.DoesNotContain(dbContext.CommentLike.ToArray(), commentLike);

            CollectionAssert.DoesNotContain(dbContext.Users.First(u => u.NickName == user2.NickName).CommentLikes.ToArray(), commentLike);
        }
コード例 #16
0
 public void MapPhotoLikeRequestToPhotoLikeEntity(PhotoLikeRequest source, out PhotoLike destination)
 {
     destination = new PhotoLike()
     {
         LikerId = source.UserId,
         PhotoId = source.PhotoId
     };
 }
コード例 #17
0
        public void Equals_NullValue_Exception()
        {
            // Arrange
            PhotoLike photoLike1 = new PhotoLike();
            PhotoLike photoLike2 = null;

            // Act
            // Assert
            Assert.ThrowsException <System.ArgumentNullException>(() => photoLike1.Equals(photoLike2));
        }
コード例 #18
0
        public void Equals_TheSameInstance_True()
        {
            // Arrange
            PhotoLike photoLike = new PhotoLike();

            // Act
            // Assert
            Assert.IsTrue(photoLike.Equals(photoLike));
            Assert.AreEqual(photoLike, photoLike);
            Assert.AreSame(photoLike, photoLike);
        }
コード例 #19
0
        public void Equals_DifferentType_False()
        {
            // Arrange
            Photo     photo     = new Photo();
            PhotoLike photoLike = new PhotoLike();

            // Act
            // Assert
            Assert.IsFalse(photo.Equals(photoLike));
            Assert.AreNotEqual(photo, photoLike);
            Assert.AreNotSame(photo, photoLike);
        }
コード例 #20
0
        public void Equals_TheSameReference_True()
        {
            // Arrange
            PhotoLike photoLike1 = new PhotoLike();
            PhotoLike photoLike2 = photoLike1;

            // Act
            // Assert
            Assert.IsTrue(photoLike1.Equals(photoLike2));
            Assert.AreEqual(photoLike1, photoLike2);
            Assert.AreSame(photoLike1, photoLike2);
        }
コード例 #21
0
        public void UnLike(User user, PhotoPost post)
        {
            PhotoLike p = this.GetOne(user.Id, post.Id);

            p.delete();

            post.Likes = PhotoLike.count("PostId=" + post.Id);
            post.update();

            user.Likes = PhotoLike.count("UserId=" + user.Id);
            user.update("Likes");
        }
コード例 #22
0
        public void TryGetUserLike()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository     = new PhotoLikeRepository(dbContext);
            PhotoLike           expectedPhotoLikeFromDb = dbContext.PhotoLike.First();

            // Act
            PhotoLike actualPhotoLikeFromDb = photoLikeRepository.TryGetUserLike(expectedPhotoLikeFromDb.Photo, expectedPhotoLikeFromDb.User);

            // Assert
            Assert.AreEqual(expectedPhotoLikeFromDb, actualPhotoLikeFromDb);
        }
コード例 #23
0
        public void Equals_DifferentType_False()
        {
            // Arrange
            PhotoLike photoLike = new PhotoLike();
            Subject   subject   = new Subject();

            // Act
            // Assert
            Assert.IsFalse(photoLike.Equals(subject));
            Assert.AreNotEqual(photoLike, subject);
            Assert.AreNotSame(photoLike, subject);
        }
コード例 #24
0
ファイル: UserTest.cs プロジェクト: Lenkelheit/Coursework
        public void Equals_DifferentType_False()
        {
            // Arrange
            User      user      = new User();
            PhotoLike photoLike = new PhotoLike();

            // Act
            // Assert
            Assert.IsFalse(user.Equals(photoLike));
            Assert.AreNotEqual(user, photoLike);
            Assert.AreNotSame(user, photoLike);
        }
コード例 #25
0
        public void GetById()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository = new PhotoLikeRepository(dbContext);
            Guid      idToSearch        = dbContext.PhotoLike.First().Id;
            PhotoLike expectedPhotoLike = dbContext.PhotoLike.Find(idToSearch);

            // Act
            PhotoLike photoLikeFromDb = photoLikeRepository.Get(idToSearch);

            // Assert
            Assert.AreEqual(expectedPhotoLike, photoLikeFromDb);
        }
コード例 #26
0
        public void GetByWrongId_Null()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository = new PhotoLikeRepository(dbContext);
            Guid      wrongId = default(Guid);
            PhotoLike expectedPhotoLikeFromDb = null;

            // Act
            PhotoLike actualPhotoLikeFromDb = photoLikeRepository.Get(wrongId);

            // Assert
            Assert.AreEqual(expectedPhotoLikeFromDb, actualPhotoLikeFromDb);
        }
コード例 #27
0
        public void DeleteByValue()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository = new PhotoLikeRepository(dbContext);
            PhotoLike           photoLikeToDelete   = dbContext.PhotoLike.First();

            // Act
            photoLikeRepository.Delete(photoLikeToDelete);
            dbContext.SaveChanges();

            // Assert
            CollectionAssert.DoesNotContain(dbContext.PhotoLike.ToArray(), photoLikeToDelete);
        }
コード例 #28
0
ファイル: PhotoLikeService.cs プロジェクト: Boshin/wojilu
        public void Like( User user, PhotoPost post )
        {
            PhotoLike x = new PhotoLike();
            x.Post = post;
            x.User = user;

            x.insert();

            post.Likes = PhotoLike.count( "PostId=" + post.Id );
            post.update();

            user.Likes = PhotoLike.count( "UserId=" + user.Id );
            user.update( "Likes" );
        }
コード例 #29
0
        public void DeleteByKey()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository      = new PhotoLikeRepository(dbContext);
            PhotoLike           expectedDeletedPhotoLike = dbContext.PhotoLike.First();
            Guid idToDelete = expectedDeletedPhotoLike.Id;

            // Act
            photoLikeRepository.Delete(idToDelete);
            dbContext.SaveChanges();

            // Assert
            CollectionAssert.DoesNotContain(dbContext.PhotoLike.ToArray(), expectedDeletedPhotoLike);
        }
コード例 #30
0
        public async Task <IActionResult> Create([Bind("PhotoId,LikeId")] PhotoLike photoLike)
        {
            if (ModelState.IsValid)
            {
                photoLike.PhotoId = Guid.NewGuid();
                _context.Add(photoLike);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["LikeId"]  = new SelectList(_context.Like, "Id", "Id", photoLike.LikeId);
            ViewData["PhotoId"] = new SelectList(_context.Photos, "Id", "Id", photoLike.PhotoId);
            return(View(photoLike));
        }
コード例 #31
0
        public void TryGetUserLike_Null()
        {
            // Arrange
            PhotoLikeRepository photoLikeRepository     = new PhotoLikeRepository(dbContext);
            PhotoLike           photoLikeFromDb         = dbContext.PhotoLike.First();
            PhotoLike           expectedPhotoLikeFromDb = null;
            string userWithNoLikes = Resources.Classes.DbFiller.Instance.UserWithoutPhotoLike;
            User   user            = dbContext.Users.First(u => u.NickName == userWithNoLikes);

            // Act
            PhotoLike actualPhotoLikeFromDb = photoLikeRepository.TryGetUserLike(photoLikeFromDb.Photo, user);

            // Assert
            Assert.AreEqual(expectedPhotoLikeFromDb, actualPhotoLikeFromDb);
        }