static void Main(string[] args) { using (var db = new ForumContext()) { var user = new User() { login = "******", password = "******", role = Role.ADMIN }; var topic = new Topic() { title = "Java", createdAt = DateTime.Now }; var post = new Post() { content = "Java 8 jest fajna!", createdAt = DateTime.Now, }; post.topic = topic; post.user = user; topic.posts.Add(post); user.topics.Add(topic); user.posts.Add(post); db.users.Add(user); db.topics.Add(topic); db.posts.Add(post); db.SaveChanges(); } }
public bool EditUser(User user) { using (var db = new ForumContext()) { //check if exists var query = from u in db.users where u.login.Equals(user.login) && u.usrID != user.usrID select u; if (query.Any()) return false; var found = db.users.Find(user.usrID); if (found == null) return false; found.login = user.login; found.password = user.password; found.role = user.role; db.SaveChanges(); } return true; }
public bool DeleteUser(User user) { using (var db = new ForumContext()) { var found = db.users.Find(user.usrID); if (found.posts.Any() || found.topics.Any()) return false; db.users.Remove(found); db.SaveChanges(); return true; } }
public bool AddUser(String login, String pswd, Role role) { using (var db = new ForumContext()) { //check if exists var query = from u in db.users where u.login.Equals(login) select u; if (query.Any()) { return false; } User newUser = new User() { login = login, password = pswd, role = role }; db.users.Add(newUser); db.SaveChanges(); return true; } }
public void AddPost(String content, int topID, User user) { using (var db = new ForumContext()) { var post = new Post(); post.content = content; post.createdAt = DateTime.Now; var query = from t in db.topics where t.topID == topID select t; List<Topic> results = query.ToList<Topic>(); Topic findTopic = results[0]; if (findTopic != null) { findTopic.posts.Add(post); post.topic = findTopic; } var findUser = db.users.Find(user.usrID); if (findUser != null) { findUser.posts.Add(post); post.user = findUser; } db.SaveChanges(); } }
public void AddPost(String content, Topic topic, User user) { using (var db = new ForumContext()) { var post = new Post(); post.content = content; post.createdAt = DateTime.Now; var findTopic = db.topics.Find(topic.topID); if (findTopic != null) { findTopic.posts.Add(post); post.topic = findTopic; } var findUser = db.users.Find(user.usrID); if (findUser != null) { findUser.posts.Add(post); post.user = findUser; } db.SaveChanges(); } }
public void AddTopic(String title, User user) { using (var db = new ForumContext()) { var topic = new Topic(); topic.title = title; topic.createdAt = DateTime.Now; var founded = db.users.Find(user.usrID); if (founded != null) founded.topics.Add(topic); topic.author = founded; db.topics.Add(topic); db.SaveChanges(); } }
public bool EditTopicTitle(int topID, string newTitle) { using (var db = new ForumContext()) { var query = from t in db.topics where t.topID == topID select t; if (!query.Any()) return false; var toEdit = query.First(); toEdit.modifiedAt = DateTime.Now; toEdit.title = newTitle; db.SaveChanges(); } return true; }
public bool EditPostContent(int posID, string newContent) { using (var db = new ForumContext()) { var query = from p in db.posts where p.posID == posID select p; if (!query.Any()) return false; var toEdit = query.First(); toEdit.modifiedAt = DateTime.Now; toEdit.content = newContent; db.SaveChanges(); } return true; }