public Post.Post AddPost(string user, string message, DateTime dateTime)
        {
            var newPost = new Post.Post(user, message, dateTime);

            Posts.Add(newPost);
            return(newPost);
        }
        public void PostService_AddPost_PostCorrect_Post()
        {
            var expected = new Post.Post("Alice", "Alice Message", DateTime.Parse("01/01/2018"));

            var result = postService.AddPost("Alice", "Alice Message", DateTime.Parse("01/01/2018"));

            Assert.AreEqual(expected, result);
        }
Пример #3
0
        public void InMemoryDBPostRepository_AddPost_PostCorrect_Post()
        {
            var expected = new Post.Post("Alice", "Alice Message", DateTime.Parse("09/09/2018"));

            var result = inMemoryDBPostRepository.AddPost("Alice", "Alice Message", DateTime.Parse("09/09/2018"));

            Assert.AreEqual(expected, result);
        }
Пример #4
0
        private string PrintPost(Post.Post post, bool addUser)
        {
            string printedPost = "";

            if (addUser)
            {
                printedPost = $"{post.User} - ";
            }
            return(printedPost + $"{post.Message} ({GetDateDifferences(post.PostingDate)} ago)");
        }
Пример #5
0
 public void AddPost(Post.Post newpost)
 {
     Post.Post[] temp = new Post.Post[++PostCount];
     if (Posts != null)
     {
         Posts.CopyTo(temp, 0);
     }
     temp[temp.Length - 1] = newpost;
     Posts = temp;
 }
Пример #6
0
        static async Task Main(string[] args)
        {
            await gatherer.Data("https://jsonplaceholder.typicode.com/posts/1");

            //new generated object
            Post.Post post = Post.Post.NewPost(gatherer.data);
            //test
            Console.WriteLine(post.title);
            Console.WriteLine(post.id);
            Console.WriteLine(post.userId);
            Console.WriteLine(post.body);
        }
Пример #7
0
        public void addPost(Post.Post post)
        {
            var newPost = new Post.Post[PostCount + 1];

            for (int i = 0; i < PostCount; i++)
            {
                newPost[i] = Posts[i];
            }
            newPost[PostCount] = post;
            PostCount++;
            Posts = newPost;
        }
Пример #8
0
        public void RemovePost(int id)
        {
            var postIndex = Array.FindIndex(Posts, post => post.Id == id);

            if (postIndex < 0)
            {
                throw new DatabaseException($"There is no post associated this id -> {id}");
            }

            var temp = new Post.Post[Posts.Length - 1];

            if (temp != null)
            {
                Array.Copy(Posts, temp, postIndex);
                Array.Copy(Posts, postIndex + 1, temp, postIndex, Posts.Length - postIndex - 1);
            }

            Posts = temp;
        }
Пример #9
0
        public void AddPost(ref Post.Post post)
        {
            var newLength = (Posts != null) ? Posts.Length + 1 : 1;
            var temp      = new Post.Post[newLength];

            if (temp == null)
            {
                throw new DatabaseException("Can not allocate new memory!");
            }

            if (Posts != null)
            {
                Array.Copy(Posts, temp, Posts.Length);
            }

            temp[newLength - 1] = post;

            Posts = temp;
        }
Пример #10
0
        public void Save(Post.Post post)
        {
            using (_context)
            {
                using (var dbContextTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        var model = post.ToModel();

                        if (post.New)
                        {
                            _posts.Add(model);
                        }
                        else
                        {
                            foreach (var postCategoryModel in _postCategories.Where(x => x.PostId == post.Id).ToList())
                            {
                                _postCategories.Remove(postCategoryModel);
                            }

                            foreach (var postTagModel in _postTags.Where(x => x.PostId == post.Id).ToList())
                            {
                                _postTags.Remove(postTagModel);
                            }

                            _context.Entry(model).State = EntityState.Modified;
                        }

                        _context.SaveChanges();

                        dbContextTransaction.Commit();
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                    }
                }
            }
        }
Пример #11
0
        public static Post.Post ToDomain(this PostModel model)
        {
            var post = new Post.Post();

            post.GetType().GetProperty("BlogId").SetValue(post, model.BlogId, null);
            post.GetType().GetProperty("Id").SetValue(post, model.Id, null);
            post.GetType().GetProperty("Title").SetValue(post, model.Title, null);
            post.GetType().GetProperty("Content").SetValue(post, model.Content, null);
            post.GetType().GetProperty("Deleted").SetValue(post, model.Deleted, null);
            post.GetType().GetProperty("Published").SetValue(post, model.Published, null);

            foreach (var postCategoryModel in model.PostCategories)
            {
                post.Categories.Add(postCategoryModel.CategoryId);
            }

            foreach (var postTagModel in model.PostTags)
            {
                post.Tags.Add(postTagModel.TagName);
            }

            return(post);
        }
Пример #12
0
        public static PostModel ToModel(this Post.Post post)
        {
            var model = new PostModel
            {
                BlogId    = post.BlogId,
                Id        = post.Id,
                Title     = post.Title,
                Content   = post.Content,
                Deleted   = post.Deleted,
                Published = post.Published
            };

            foreach (var categoryId in post.Categories)
            {
                var postCategoryModel = new PostCategoryModel
                {
                    PostId     = post.Id,
                    CategoryId = categoryId
                };

                model.PostCategories.Add(postCategoryModel);
            }

            foreach (var tag in post.Tags)
            {
                var postTagModel = new PostTagModel
                {
                    PostId  = post.Id,
                    TagName = tag
                };

                model.PostTags.Add(postTagModel);
            }

            return(model);
        }
Пример #13
0
        public void Start()
        {
            Admin.Admin admin = new Admin.Admin()
            {
                Username = "******",
                Email    = "*****@*****.**",
                Password = "******",
            };
            User.User u1 = new User.User()
            {
                Name     = "User",
                Surname  = "Number 1",
                Email    = "*****@*****.**",
                Password = "******"
            };
            User.User u2 = new User.User()
            {
                Name     = "Sun",
                Surname  = "Glasses",
                Email    = "*****@*****.**",
                Password = "******"
            };
            User.User u3 = new User.User()
            {
                Name     = "Moon",
                Surname  = "Shine",
                Email    = "*****@*****.**",
                Password = "******"
            };
            Post.Post p1 = new Post.Post()
            {
                Content      = "Forest Holiday",
                CreatingDate = new DateTime(2017, 2, 25),
                LikeCount    = 100,
                ViewCount    = 160
            };

            Post.Post p2 = new Post.Post()
            {
                Content      = "Snowing woww!",
                CreatingDate = new DateTime(2015, 12, 13),
                LikeCount    = 20,
                ViewCount    = 22
            };

            Post.Post p3 = new Post.Post()
            {
                Content      = "Happy Easter everyone!",
                CreatingDate = new DateTime(2021, 3, 17),
                LikeCount    = 2,
                ViewCount    = 14
            };

            admin.addPost(p1);
            admin.addPost(p2);
            admin.addPost(p3);

            User.User[] users = new User.User[3] {
                u1, u2, u3
            };
            dynamic a;

            while (true)
            {
                Console.WriteLine("1)Admin");
                Console.WriteLine("2)User");
                try
                {
                    a = Choosing(Console.ReadLine(), ref admin, ref users);
                    Console.Clear();
                    if (a == 'u')
                    {
                        while (true)
                        {
                            try
                            {
                                LookingAround(ref users[indexofuser], ref admin);
                                break;
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Пример #14
0
 public PostResponse(Post.Post post)
 {
     this.Post = post;
 }
Пример #15
0
        public static void Start(ref Database.Database db, ref Session.LoginAccountSession session)
        {
            var userMenuLoop = true;

            while (userMenuLoop)
            {
                ConsoleInterface.PrintMenu(ConsoleInterface.UserMenuOptions);

                switch ((UserMenuOptions)ConsoleInterface.InputChoice(ConsoleInterface.UserMenuOptions.Length))
                {
                case UserMenuOptions.SHOWALLPOSTS:
                {
                    try
                    {
                        Console.Clear();
                        db.ShowAllPosts(true);
                        ConsoleHelper.ClearConsole();
                    }
                    catch (Exception e)
                    {
                        var line = Console.CursorTop;
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        Console.WriteLine(e.Message);
                        ConsoleHelper.ClearConsole(line - 1, 10);
                    }
                    break;
                }

                case UserMenuOptions.SHOW:
                {
                    try
                    {
                        Console.Clear();
                        db.ShowAllPosts();

                        var id = 0;
                        while (true)
                        {
                            try
                            {
                                Console.Write("ID: ");
                                id = Convert.ToInt32(Console.ReadLine());
                                break;
                            }
                            catch (Exception e)
                            {
                                var line = Console.CursorTop;
                                Console.ForegroundColor = ConsoleColor.DarkRed;
                                Console.WriteLine(e.Message);
                                ConsoleHelper.ClearConsole(line - 1, 10);
                            }
                        }
                        Console.Clear();

                        var post = db.GetPost(id);
                        Console.WriteLine(post);

                        post.IncreaseView();

                        var newNotf = new Notification.Notification()
                        {
                            FromUser = session.User as User.User, Text = $"{(session.User as User.User).Username} viewed this post [id] ->{post.Id}"
                        };

                        db.AddNotification(ref newNotf);

                        Mail.SendMail(db.Admins[0].Email, "New views!", newNotf.Text);

                        ConsoleHelper.ClearConsole();
                    }
                    catch (Exception e)
                    {
                        var line = Console.CursorTop;
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        Console.WriteLine(e.Message);
                        ConsoleHelper.ClearConsole(line - 1, 10);
                    }
                    break;
                }

                case UserMenuOptions.LIKE:
                {
                    try
                    {
                        Console.Clear();
                        db.ShowAllPosts();

                        var id = 0;
                        while (true)
                        {
                            try
                            {
                                Console.Write("ID: ");
                                id = Convert.ToInt32(Console.ReadLine());
                                break;
                            }
                            catch (Exception e)
                            {
                                var line = Console.CursorTop;
                                Console.ForegroundColor = ConsoleColor.DarkRed;
                                Console.WriteLine(e.Message);
                                ConsoleHelper.ClearConsole(line - 1, 10);
                            }
                        }
                        Console.Clear();
                        var post = db.GetPost(id);
                        post++;

                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine("Post liked!");
                        Console.ResetColor();

                        var newNotf = new Notification.Notification()
                        {
                            FromUser = session.User as User.User, Text = $"{(session.User as User.User).Username} liked this post [id] ->{post.Id}"
                        };

                        db.AddNotification(ref newNotf);

                        Mail.SendMail(db.Admins[0].Email, "New likes!", newNotf.Text);


                        ConsoleHelper.ClearConsole();
                    }
                    catch (Exception e)
                    {
                        var line = Console.CursorTop;
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        Console.WriteLine(e.Message);
                        ConsoleHelper.ClearConsole(line - 1, 10);
                    }
                    break;
                }

                case UserMenuOptions.CREATEPOST:
                {
                    Console.Clear();

                    Console.Write("Content: ");

                    var content = string.Empty;

                    do
                    {
                        content = Console.ReadLine();
                    } while (String.IsNullOrWhiteSpace(content));

                    var newPost = new Post.Post()
                    {
                        Username = (session.User as User.User).Username, Content = content
                    };

                    (session.User as User.User).AddPost(ref newPost);
                    break;
                }

                case UserMenuOptions.LOGOUTUSER:
                {
                    session.Logout();
                    userMenuLoop = false;
                    break;
                }

                default:
                    break;
                }
            }
        }