Beispiel #1
0
        public void JoinTest()
        {
            User testUser = new User
            {
                FirstName = "Test",
                LastName  = "User",
                Email     = "*****@*****.**"
            };

            WikiArticle testArticle = new WikiArticle
            {
                Url          = "http://test",
                ArticleDate  = DateTime.Now,
                ArticleBody  = "Test Body",
                ArticleTitle = "Test Title"
            };

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                var postType = db.PostType.First();

                Post testPost = new Post
                {
                    Date        = DateTime.Now,
                    Title       = "Urzababa The Great",
                    Description = "The Life of Urzababa",
                    PostTypeId  = postType.PostTypeId
                };

                Favourite testFavourite = new Favourite
                {
                    Date = DateTime.Now
                };

                db.User.Add(testUser);
                db.WikiArticle.Add(testArticle);
                db.SaveChanges();

                WikiArticle wikiArticle = db.WikiArticle.First(wa => wa.ArticleDate == testArticle.ArticleDate);
                testPost.WikiArticleId = wikiArticle.WikiArticleId;

                User user = db.User.First(u => u.Email == testUser.Email);
                testPost.UserId = user.UserId;

                db.Post.Add(testPost);
                db.SaveChanges();

                Post post = db.Post.First(p => p.Title == testPost.Title);
                testFavourite.PostId = post.PostId;
                testFavourite.UserId = user.UserId;

                db.Favourite.Add(testFavourite);
                db.SaveChanges();

                Favourite favourite = db.Favourite.First(f => f.Date == testFavourite.Date);
                Assert.AreEqual(testUser.UserId, favourite.UserId);
                Assert.AreEqual(testPost.PostId, favourite.PostId);
            }
        }
Beispiel #2
0
        public PostDto CreatePost(PostDto postDto, int userId)
        {
            WikiArticle wikiArticle;
            string      articleImageUrl = null;

            try
            {
                articleImageUrl = _wikiMediaApiService.GetWikiThumbnailAsync(postDto.ArticleTitle).Result;
            }
            catch (AggregateException e)
            {
                e.Handle((x) =>
                {
                    if (x is WikiArticleThumbnailNotFoundException)
                    {
                        articleImageUrl = WIKIARTICLE_DEFAULT_THUMBNAIL;

                        return(true);
                    }

                    return(false);
                });
            }

            wikiArticle = new WikiArticle()
            {
                Url             = postDto.ArticleUrl,
                ArticleDate     = _wikiMediaApiService.GetWikiLatestDateAsync(postDto.ArticleTitle).Result,
                ArticleBody     = _wikiMediaApiService.GetWikiContentAsync(postDto.ArticleTitle).Result.Query.Pages.Values.First().Extract,
                ArticleTitle    = postDto.ArticleTitle,
                ArticleImageUrl = articleImageUrl
            };

            _db.WikiArticle.Add(wikiArticle);
            _db.SaveChanges();

            PostType postType = _db.PostType.First(pt => pt.PostTypeValue == postDto.PostType);
            Post     post     = new Post()
            {
                UserId        = userId,
                Date          = DateTime.Now,
                WikiArticleId = wikiArticle.WikiArticleId,
                Title         = postDto.Title,
                Description   = postDto.Description,
                PostTypeId    = postType.PostTypeId
            };

            _db.Post.Add(post);
            _db.SaveChanges();

            return(PostMapper.ToDtoPostUrl(post));
        }
Beispiel #3
0
        public UserDto Authenticate(Google.Apis.Auth.GoogleJsonWebSignature.Payload payload, out int userId)
        {
            User user = _wediumContext.User
                        .FirstOrDefault(x => x.Email == payload.Email);

            if (user == null)
            {
                user = new User()
                {
                    FirstName = payload.GivenName,
                    Email     = payload.Email,
                    LastName  = payload.FamilyName
                };

                _wediumContext.User.Add(user);
                _wediumContext.SaveChanges();
            }

            userId = user.UserId;

            UserDto userDto = UserMapper.ToDto(user);

            userDto.PictureUri = payload.Picture;

            return(userDto);
        }
Beispiel #4
0
        public void PersistanceTest()
        {
            // Adding a User to the DB
            User testUser = new User
            {
                FirstName = "Test",
                LastName  = "User",
                Email     = "*****@*****.**"
            };

            using (WediumContext db = new WediumContext(_wediumContextOptions))
            {
                try
                {
                    db.User.Add(testUser);
                    db.SaveChanges();

                    var user = db.User.FirstOrDefault(u => u.FirstName == "Test");
                    Assert.AreEqual(testUser.FirstName, user.FirstName);
                    Assert.AreEqual(testUser.LastName, user.LastName);
                    Assert.AreEqual(testUser.Email, user.Email);

                    user.FirstName = "Updated Firstname";
                    user.LastName  = "Updated Lastname";

                    db.SaveChanges();

                    Assert.AreEqual("Updated Firstname", user.FirstName);
                    Assert.AreEqual("Updated Lastname", user.LastName);
                }
                finally
                {
                    // Deleting the User from the database
                    db.User.Remove(testUser);
                    db.SaveChanges();

                    var removedUser = db.User.FirstOrDefault(u => u.FirstName == "Test user");
                    Assert.AreEqual(null, removedUser);
                }
            }
        }
Beispiel #5
0
        public static void InitializeDB(DbContextOptions <WediumContext> options)
        {
            using (WediumContext db = new WediumContext(options))
            {
                foreach (Settings setting in InitialSettings())
                {
                    db.Settings.Add(setting);
                }

                foreach (PostType postType in InitialPostTypes())
                {
                    db.PostType.Add(postType);
                }

                foreach (User user in InitialUsers())
                {
                    db.User.Add(user);
                }

                foreach (WikiArticle wikiArticle in InitialWikiArticles())
                {
                    db.WikiArticle.Add(wikiArticle);
                }

                foreach (Post post in InitializePosts())
                {
                    db.Post.Add(post);
                }

                foreach (Favourite favourite in InitializeFavourites())
                {
                    db.Favourite.Add(favourite);
                }

                foreach (PostLike postLike in InitializePostLikes())
                {
                    db.PostLike.Add(postLike);
                }

                foreach (CommentType commentType in InitializeCommentTypes())
                {
                    db.CommentType.Add(commentType);
                }

                foreach (Comment comment in InitializeComments())
                {
                    db.Comment.Add(comment);
                }

                db.SaveChanges();
            }
        }
Beispiel #6
0
        public void CreatePostLike(int userId, int postId)
        {
            if (!_postService.CheckExists(postId))
            {
                throw new PostNotFoundException();
            }

            if (_db.PostLike.Any(p => p.UserId == userId && p.PostId == postId))
            {
                throw new PostLikeAlreadyExistsException();
            }

            PostLike postLike = new PostLike
            {
                PostId = postId,
                UserId = userId,
                Date   = DateTime.Now
            };

            _db.PostLike.Add(postLike);
            _db.SaveChanges();
        }
        public void CreateCommentLike(int userId, int commentId)
        {
            if (!_commentService.CheckExists(commentId))
            {
                throw new CommentNotFoundException();
            }

            if (_db.CommentLike.Any(c => c.UserId == userId && c.CommentId == commentId))
            {
                throw new CommentLikeAlreadyExistsException();
            }

            CommentLike commentLike = new CommentLike
            {
                CommentId = commentId,
                UserId    = userId,
                Date      = DateTime.Now
            };

            _db.CommentLike.Add(commentLike);
            _db.SaveChanges();
        }
Beispiel #8
0
        public (CommentDto commentDto, PostDto post) CreateComment(CommentDto commentDto, int?userId)
        {
            Post post = _db.Post
                        .Where(p => p.PostId == commentDto.PostId)
                        .Include(p => p.PostType)
                        .FirstOrDefault() ?? throw new PostNotFoundException();

            Comment comment = CommentMapper.FromDto(commentDto);

            comment.UserId = (int)userId;

            _db.Comment.Add(comment);
            _db.SaveChanges();

            Comment commentWithJoin = _db.Comment
                                      .Where(c => c.CommentId == comment.CommentId)
                                      .Include(c => c.User)
                                      .Include(c => c.CommentType)
                                      .First();

            return(CommentMapper.ToDto(commentWithJoin, userId), PostMapper.ToDtoPostUrl(post));
        }