public static void RemovePostById(int id)
 {
     using (var db = new DbAscContext())
     {
         db.Posts.Remove(db.Posts.FirstOrDefault(p => p.Id == id));
         db.SaveChanges();
     }
 }
 public static void AddPost(Post project)
 {
     using (var db = new DbAscContext())
     {
         db.Posts.Add(project);
         db.SaveChanges();
     }
 }
 public static void UpdateCodeLinkById(CodeLink link)
 {
     using (var db = new DbAscContext())
     {
         var e = GetCodeLinkById(link.Id);
         if (e != null)
         {
             e.Code = link.Code;
         }
         db.SaveChanges();
     }
 }
        public static void DeletePostRelations(int postId)
        {
            using (var db = new DbAscContext())
            {
                foreach (var entry in db.UserPostContributions.Where(e => e.PostId == postId))
                {
                    db.Remove(entry);
                }

                db.SaveChanges();
            }
        }
        public static int CreateCodeLink(string text)
        {
            CodeLink link = new CodeLink {
                Code = text
            };

            using (var db = new DbAscContext())
            {
                var newLink = db.CodeLinks.Add(link);
                db.SaveChanges();
            }
            return(link.Id);
        }
 public static void AddOrUpdatePost(Post project)
 {
     using (var db = new DbAscContext())
     {
         var found = db.Posts.FirstOrDefault(p => p.Id == project.Id);
         if (found != null)
         {
             found.Assign(project);
             db.Posts.Update(found);
         }
         else
         {
             db.Posts.Add(project);
         }
         db.SaveChanges();
     }
 }
        public static List <List <string> > ExecuteRawSqlQuery(string query)
        {
            using (var db = new DbAscContext())
                using (var command = db.Database.GetDbConnection().CreateCommand())
                {
                #pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
                    command.CommandText = query;
                #pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

                    command.CommandType = CommandType.Text;
                    db.Database.OpenConnection();

                    using (var reader = command.ExecuteReader())
                    {
                        var list = new List <List <string> >();

                        if (reader.HasRows)
                        {
                            var innerList = new List <string>();
                            // read rows names
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                string rowName = $"{reader.GetName(i)}";
                                innerList.Add(Functions.FormatDbField(rowName));
                            }
                            list.Add(innerList);

                            // read values
                            while (reader.Read())
                            {
                                innerList = new List <string>();
                                for (int i = 0; i < reader.FieldCount; i++)
                                {
                                    string field = $"{reader[i]}";
                                    innerList.Add(Functions.FormatDbField(field));
                                }
                                list.Add(innerList);
                            }
                        }
                        db.SaveChanges();
                        return(list);
                    }
                }
        }
        public static void AddRelations(int postId, List <PostUserEntry> relations)
        {
            using (var db = new DbAscContext())
            {
                foreach (var entry in relations)
                {
                    if (entry.userData == null)
                    {
                        throw new NullReferenceException("UserData must not be null");
                    }

                    db.UserPostContributions.Add(new UserPostContribution
                    {
                        PostId = postId,
                        UserId = entry.userData.Id,
                        Type   = (int)entry.postRelation
                    });
                }

                db.SaveChanges();
            }
        }