Before being able to add and edit FAQ question/answers we need to create the entity. Create the following item in the entities folder.
using MrCMS.Entities; using MrCMS.Web.Apps.Faqs.Pages; namespace MrCMS.Web.Apps.Faqs.Entities { public class Faq : SiteEntity { public virtual string Question { get; set; } public virtual string Answer { get; set; } public virtual ShowFaqs ShowFaqs { get; set; } } }
Make sure to inhreit from SiteEntity.Mr CMS recognises this class and does a few things for you. At this stage, that is all you have to do - Mr CMS will wireup the rest. However by default Mr CMS will map a string field to a varchar(255) field. The answer of the question could be lengthy, so we are going to create a mapping override to let Mr CMS know that the Answer field should be varchar(max).
To do this, in your DbConfiguration folder add a class called FaqItemOverride.cs.
public class FaqItemOverride : IAutoMappingOverride { public void Override(AutoMapping mapping) { mapping.Map(faq => faq.Answer).CustomType().Length(4001); } }
This will make sure that when Mr CMS creates the database field, it will make the Answer field a varchar(max) field.
Now we have the Faq entitiy, we need to update our webpage class:
using System.Collections.Generic; using MrCMS.Entities.Documents.Web; using MrCMS.Web.Apps.Faqs.Entities; namespace MrCMS.Web.Apps.Faqs.Pages { public class ShowFaqs : Webpage { public virtual IList<Faq> Faqs { get; set; } } }
This makes sure the relation between Faqs and the page is set.
Finally create a service to add/edit the Faqs:
using System.Collections.Generic; using MrCMS.Helpers; using MrCMS.Web.Apps.Faqs.Entities; using NHibernate; namespace MrCMS.Web.Apps.Faqs.Services { public class FaqService : IFaqService { private readonly ISession _session; public FaqService(ISession session) { _session = session; } public void Add(Faq faq) { faq.ShowFaqs.Faqs.Add(faq); //required to bust page cache _session.Transact(session => session.Save(faq)); } public void Update(Faq faq) { _session.Transact(session => session.Update(faq)); } public void Delete(Faq faq) { faq.ShowFaqs.Faqs.Remove(faq); //required to bust page cache _session.Transact(session => session.Delete(faq)); } } }
Interface:
using MrCMS.Web.Apps.Faqs.Entities; namespace MrCMS.Web.Apps.Faqs.Services { public interface IFaqService { void Add(Faq speaker); void Update(Faq speaker); void Delete(Faq speaker); } }
2014 © Mr CMS. ALL Rights Reserved.