public User GetById(int id) { using (var db = new ForumSystemDbContext()) { return(db.Users.FirstOrDefault(u => u.Id == id)); } }
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); } } }
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); }
public Category GetByName(string categoryName) { using (var db = new ForumSystemDbContext()) { return(db.Categories.FirstOrDefault(c => c.Name == categoryName)); } }
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); }
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); } }
/// <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(); } }
public bool ValidateLoginTrial(string username, string password) { using (var db = new ForumSystemDbContext()) { return(db.Users .Any(u => u.Username == username && u.Password == password)); } }
public DbInitializer(ForumSystemDbContext db) { this.replyGenerator = new ReplyGenerator(); this.postGenerator = new PostGenerator(); this.categoryGenerator = new CategoryGenerator(); this.userGenerator = new UserGenerator(); this.db = db; }
public IEnumerable <Post> GetPostsById(int categoryId) { using (var db = new ForumSystemDbContext()) { return(db.Posts .Where(p => p.CategoryId == categoryId) .ToArray()); } }
public IEnumerable <string> GetAllCategoryNames() { using (var db = new ForumSystemDbContext()) { return(db.Categories .Select(c => c.Name) .ToArray()); } }
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); }
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); } }
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(); }
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); } }
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); } }
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; }
public SeedData(ForumSystemDbContext context, UserManager<User> userManager, RoleManager<IdentityRole> roleManager) { this.context = context; this.userManager = userManager; this.roleManager = roleManager; }