Example #1
0
 public User GetById(int id)
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Users.FirstOrDefault(u => u.Id == id));
     }
 }
Example #2
0
        public string RegisterUser(string username, string password)
        {
            using (var db = new ForumSystemDbContext())
            {
                var user = new User()
                {
                    Username = username,
                    Password = password
                };

                if (user.IsValid())
                {
                    var isUserExisting = db.Users.Any(u => u.Username == user.Username);

                    if (!isUserExisting)
                    {
                        db.Users.Add(user);
                        db.SaveChanges();
                        return(REGISTRATION_SUCCESSFULL);
                    }
                    else
                    {
                        return(USERNAME_TAKEN_ERROR);
                    }
                }
                else
                {
                    return(DETAILS_ERROR);
                }
            }
        }
Example #3
0
 public Category GetById(int categoryId)
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Categories.Find(categoryId));
     }
 }
        internal IEnumerable <Reply> CreateReplies(ForumSystemDbContext db)
        {
            var sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

            var allUsersIds = db
                              .Users
                              .Select(u => u.Id)
                              .ToArray();

            var allPostsIds = db
                              .Posts
                              .Select(c => c.Id)
                              .ToArray();

            var replies = new List <Reply>();

            for (int i = 1; i < 300; i++)
            {
                var authorId = allUsersIds[this.random.Next(0, allUsersIds.Length)];
                var postId   = allPostsIds[this.random.Next(0, allPostsIds.Length)];

                replies.Add(new Reply()
                {
                    Content  = sampleContent,
                    AuthorId = authorId,
                    PostId   = postId
                });
            }

            return(replies);
        }
Example #5
0
 public Category GetByName(string categoryName)
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Categories.FirstOrDefault(c => c.Name == categoryName));
     }
 }
Example #6
0
 public virtual void Setup()
 {
     DbContext = new ForumSystemDbContext(DbName);
     DbContext.Database.CreateIfNotExists();
     //
     Transaction = DbContext.Database.BeginTransaction();
 }
        internal IEnumerable <Post> CreatePosts(ForumSystemDbContext db)
        {
            var sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

            var allUsersIds = db
                              .Users
                              .Select(u => u.Id)
                              .ToArray();

            var allCategoriesIds = db
                                   .Categories
                                   .Select(c => c.Id)
                                   .ToArray();

            var posts = new List <Post>();

            for (int i = 1; i < 100; i++)
            {
                var postTitle  = $"Post #{i}";
                var authorId   = allUsersIds[this.random.Next(0, allUsersIds.Length)];
                var categoryId = allCategoriesIds[this.random.Next(0, allCategoriesIds.Length)];

                posts.Add(new Post()
                {
                    Title      = postTitle,
                    Content    = sampleContent,
                    AuthorId   = authorId,
                    CategoryId = categoryId
                });
            }

            return(posts);
        }
Example #8
0
        public PostServiceModel GetById(int id)
        {
            using (var db = new ForumSystemDbContext())
            {
                var model = db.Posts
                            .Where(p => p.Id == id)
                            .Select(p => new PostServiceModel
                {
                    Title    = p.Title,
                    Content  = p.Content,
                    Author   = p.Author.Username,
                    Category = p.Category.Name,
                    Replies  = p.Replies
                               .Select(r => new ReplyServiceModel
                    {
                        Content = r.Content,
                        Author  = r.Author.Username
                    })
                               .ToArray()
                })
                            .First();

                return(model);
            }
        }
Example #9
0
 /// <summary>
 /// Performs data seed on a separate instance of the db context
 /// In order to isolate the EF context cache, we need to perform the data seeding and data Querying on separate contexts
 /// Otherwise we might get false positives on tests that rely on internal cache</summary>
 protected async Task SeedData(Action <ForumSystemDbContext> seedDataAction)
 {
     using (var context = new ForumSystemDbContext(DbName))
     {
         seedDataAction(context);
         await context.SaveChangesAsync();
     }
 }
Example #10
0
 public bool ValidateLoginTrial(string username, string password)
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Users
                .Any(u => u.Username == username && u.Password == password));
     }
 }
Example #11
0
 public DbInitializer(ForumSystemDbContext db)
 {
     this.replyGenerator    = new ReplyGenerator();
     this.postGenerator     = new PostGenerator();
     this.categoryGenerator = new CategoryGenerator();
     this.userGenerator     = new UserGenerator();
     this.db = db;
 }
Example #12
0
 public IEnumerable <Post> GetPostsById(int categoryId)
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Posts
                .Where(p => p.CategoryId == categoryId)
                .ToArray());
     }
 }
Example #13
0
 public IEnumerable <string> GetAllCategoryNames()
 {
     using (var db = new ForumSystemDbContext())
     {
         return(db.Categories
                .Select(c => c.Name)
                .ToArray());
     }
 }
Example #14
0
        public async Task SeedAsync(ForumSystemDbContext dbContext, IServiceProvider serviceProvider)
        {
            var roleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();

            var userManager = serviceProvider.GetRequiredService <UserManager <User> >();

            await SeedRolesAsync(roleManager, GlobalConstants.AdminstrationRoleName);

            await SeedUserWithRoleAsync(userManager);
        }
Example #15
0
        public IEnumerable <ReplyServiceModel> GetReplyModelsByPostId(int postId)
        {
            using (var db = new ForumSystemDbContext())
            {
                var replies = db.Replies
                              .Where(r => r.PostId == postId)
                              .Select(r => new ReplyServiceModel
                {
                    Author  = r.Author.Username,
                    Content = r.Content
                })
                              .ToArray();

                return(replies);
            }
        }
Example #16
0
        public static void Main()
        {
            // To update the database manualy read the README file!

            //(If the program throws a System.Data.SqlClient.SqlException - read the README file to resolve the problem)
            using (var database = new ForumSystemDbContext())
            {
                var initializer = new DbInitializer(database);
                initializer.ResetDatabase();

                // To initialize the database with sample data uncomment the line below:
                //initializer.SeedData();
            }

            Engine engine = new Engine();

            engine.Run();
        }
Example #17
0
        public bool Create(string name)
        {
            using (var db = new ForumSystemDbContext())
            {
                var category = new Category()
                {
                    Name = name
                };

                if (category.IsValid())
                {
                    db.Categories.Add(category);
                    db.SaveChanges();

                    return(true);
                }

                return(false);
            }
        }
Example #18
0
        public bool Create(ReplyServiceModel model)
        {
            using (var db = new ForumSystemDbContext())
            {
                var reply = new Reply()
                {
                    Content  = model.Content,
                    AuthorId = db.Users.First(u => u.Username == model.Author).Id,
                    PostId   = model.PostId
                };

                if (reply.IsValid())
                {
                    db.Replies.Add(reply);
                    db.SaveChanges();
                    return(true);
                }

                return(false);
            }
        }
Example #19
0
        public bool Create(PostServiceModel model)
        {
            using (var db = new ForumSystemDbContext())
            {
                var newPost = new Post()
                {
                    Title      = model.Title,
                    Content    = model.Content,
                    AuthorId   = db.Users.FirstOrDefault(u => u.Username == model.Author).Id,
                    CategoryId = db.Categories.First(c => c.Name == model.Category).Id
                };

                if (newPost.IsValid())
                {
                    db.Posts.Add(newPost);
                    db.SaveChanges();

                    return(true);
                }

                return(false);
            }
        }
 public AdminTopicService(ForumSystemDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #21
0
 public SeedData(ForumSystemDbContext context, UserManager<User> userManager, RoleManager<IdentityRole> roleManager)
 {
     this.context = context;
     this.userManager = userManager;
     this.roleManager = roleManager;
 }