Esempio n. 1
0
        public static void Update(Context db, int id, int sectionId, FSection input,
                                  Func <TForm, bool> viewPermitting = null, Func <TForm, bool> editPermitting = null, Action <TSection> after = null)
        {
            var form = Form.Load(db, id) as TForm;

            if (null != editPermitting && !editPermitting.Invoke(form))
            {
                throw new AccessDenied();
            }
            var section = form.Sections.FirstOrDefault(x => sectionId == x.Id) as TSection;

            if (null == section)
            {
                throw new NotFound();
            }
            if (!string.IsNullOrEmpty(input.Title))
            {
                section.Title = input.Title;
            }
            if (null != input.Position)
            {
                section.Position = input.Position.Value;
            }
            if (null != input.ParentId && input.ParentId.Value > 0)
            {
                if (input.ParentId.Value == section.Id)
                {
                    throw new AccessDenied("ParentID is not valid");
                }
                var parentSection = db.FormCoreSections.Where(x => x.Id == input.ParentId.Value).Include("Form")
                                    .FirstOrDefault();
                if (null == parentSection)
                {
                    throw new NotFound();
                }
                if (null != viewPermitting && !viewPermitting.Invoke(parentSection.Form as TForm))
                {
                    throw new AccessDenied();
                }
                section.ParentId = parentSection.Id;
                if (string.IsNullOrEmpty(section.Title))
                {
                    section.Title = parentSection.Title;
                }
            }
            db.SaveChanges();
            after?.Invoke(section);
        }
Esempio n. 2
0
        public static void Update(Context db, int id, int sectionId, FSection input, Func <T, bool> permitting)
        {
            var form = Form.Load(db, id) as T;

            if (null != permitting && !permitting.Invoke(form))
            {
                throw new Exceptions.AccessDenied();
            }
            var section = form.Sections.FirstOrDefault(x => sectionId == x.Id);

            if (null == section)
            {
                throw new Exceptions.NotFound();
            }
            section.Title    = input.Title;
            section.Position = input.Position;
        }
Esempio n. 3
0
        public static int Create(Context db, int id, FSection input, Func <T, bool> permitting)
        {
            var form = Form.Load(db, id) as T;

            if (null != permitting && !permitting.Invoke(form))
            {
                throw new Exceptions.AccessDenied();
            }
            var section = new Section {
                FormId   = form.Id,
                Title    = input.Title,
                Position = input.Position,
            };

            db.FormCoreSections.Add(section);
            db.SaveChanges();
            return(section.Id);
        }
Esempio n. 4
0
        public static int Create(Context db, int id, FSection input, Func <TForm, bool> viewPermitting = null,
                                 Func <TForm, bool> editPermitting = null,
                                 Action <Section> after            = null)
        {
            var form = Form.Load(db, id) as TForm;

            if (null != editPermitting && !editPermitting.Invoke(form))
            {
                throw new AccessDenied();
            }
            var section = new Section {
                FormId   = form.Id,
                Title    = input.Title,
                Position = input.Position ?? 0
            };

            if (null != input.ParentId && input.ParentId.Value > 0)
            {
                var parentSection = db.FormCoreSections.Where(x => x.Id == input.ParentId).Include("Form").FirstOrDefault();
                if (null == parentSection)
                {
                    throw new NotFound();
                }
                if (null != viewPermitting && !viewPermitting.Invoke(parentSection.Form as TForm))
                {
                    throw new AccessDenied();
                }
                section.ParentId = parentSection.Id;
                if (string.IsNullOrEmpty(section.Title))
                {
                    section.Title = parentSection.Title;
                }
            }
            db.FormCoreSections.Add(section);
            db.SaveChanges();
            if (typeof(TSection) != typeof(Section))
            {
                var sql =
                    $"UPDATE TOP(1) dbo.FormCoreSections SET Discriminator='{typeof(TSection).Name}' where Id={section.Id}";
                db.Database.ExecuteSqlCommand(sql);
            }
            after?.Invoke(section);
            return(section.Id);
        }