Skip to content

Commit

Permalink
GH-13 Try to loose item and key relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-yagodin committed Apr 24, 2018
1 parent 78f7d85 commit 57c21a4
Showing 1 changed file with 63 additions and 49 deletions.
112 changes: 63 additions & 49 deletions R7.Dnn.Extensions/Modules/EditPortalModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.UI.UserControls;
using R7.Dnn.Extensions.Data;
Expand All @@ -37,28 +36,28 @@ namespace R7.Dnn.Extensions.Modules
/// <summary>
/// A base class to build simple edit module controls
/// </summary>
public abstract class EditPortalModuleBase<TItem, TItemId>: PortalModuleBase
public abstract class EditPortalModuleBase<TItem, TKey>: PortalModuleBase
where TItem : class, new()
where TItemId : struct
where TKey : struct
{
#region Fields & Properties

/// <summary>
/// The edited item identifier.
/// </summary>
protected virtual TItemId? ItemId {
protected virtual TKey? ItemKey {
get {
var itemIdObj = ViewState ["ItemId"];
if (itemIdObj != null) {
return (TItemId?) itemIdObj;
var itemKeyObj = ViewState ["ItemKey"];
if (itemKeyObj != null) {
return (TKey?) itemKeyObj;
}

// parse querystring parameters
var itemId = ParseHelper.ParseToNullable<TItemId> (Request.QueryString [Key]);
ViewState ["ItemId"] = itemId;
return itemId;
var itemKey = ParseHelper.ParseToNullable<TKey> (Request.QueryString [Key]);
ViewState ["ItemKey"] = itemKey;
return itemKey;
}
set { ViewState ["ItemId"] = value; }
set { ViewState ["ItemKey"] = value; }
}

/// <summary>
Expand Down Expand Up @@ -97,7 +96,8 @@ protected virtual TItemId? ItemId {
/// <summary>
/// Initializes a new instance of the <see cref="R7.Dnn.Extensions.Modules.EditPortalModuleBase{TItem,TKey}"/> class.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="key">Querystring key.</param>
/// <param name="crudProvider">CRUD provider object.</param>
protected EditPortalModuleBase (string key, ICrudProvider<TItem> crudProvider)
{
Key = key;
Expand Down Expand Up @@ -152,28 +152,32 @@ protected override void OnLoad (EventArgs e)

try {
if (!IsPostBack) {
// load the data into the control the first time we hit this page

// check we have an item to edit
if (ItemId != null) {
// load the item
var item = GetItemWithDependencies (ItemId.Value);

if (item != null && CanEditItem (item)) {
ButtonDelete.Visible = CanDeleteItem (item);
LoadItem (item);
if (Request.QueryString [Key] != null) {
if (ItemKey != null) {
var item = GetItemWithDependencies (ItemKey.Value);
if (item != null) {
if (CanEditItem (item)) {
ButtonDelete.Visible = CanDeleteItem (item);
LoadItem (item);
}
}
else {
Exceptions.LogException (new SecurityException ($"Wrong item key: {ItemKey}"));
}
}
else {
ItemDoesNotExists ();
if (CanAddItem ()) {
ButtonDelete.Visible = false;
if (ModuleAuditControl != null) {
ModuleAuditControl.Visible = false;
}

LoadNewItem ();
}
}
}
else {
ButtonDelete.Visible = false;
if (ModuleAuditControl != null) {
ModuleAuditControl.Visible = false;
}

LoadNewItem ();
Exceptions.LogException (new SecurityException ($"Wrong edit URL: {Request.RawUrl}"));
}
}
else {
Expand All @@ -194,19 +198,23 @@ protected virtual void OnButtonUpdateClick (object sender, EventArgs e)
{
try {
if (Page.IsValid) {
// create new or get existing item
var item = (ItemId == null) ? new TItem () : GetItem (ItemId.Value);
var item = GetItem (ItemKey.Value);
var isNew = item == null;

BeforeUpdateItem (item);
if (isNew) {
item = new TItem ();
}

BeforeUpdateItem (item, isNew);

if (ItemId == null) {
if (isNew) {
AddItem (item);
}
else {
UpdateItem (item);
}

AfterUpdateItem (item);
AfterUpdateItem (item, isNew);

// synchronize module
ModuleController.SynchronizeModule (ModuleId);
Expand All @@ -227,22 +235,20 @@ protected virtual void OnButtonUpdateClick (object sender, EventArgs e)
protected virtual void OnButtonDeleteClick (object sender, EventArgs e)
{
try {
if (ItemId.HasValue) {
var item = GetItem (ItemId.Value);
if (item != null && CanDeleteItem (item)) {
DeleteItem (item);
Response.Redirect (Globals.NavigateURL (), true);
}
var item = GetItem (ItemKey.Value);
if (item != null && CanDeleteItem (item)) {
DeleteItem (item);
Response.Redirect (Globals.NavigateURL (), true);
}
}
catch (Exception ex) {
Exceptions.ProcessModuleLoadException (this, ex);
}
}


#region CRUD methods

// TODO: Remove as too children-specific?
/// <summary>
/// Override this method if you need extra data
/// (e.g. some dependent objects and collections)
Expand All @@ -251,26 +257,26 @@ protected virtual void OnButtonDeleteClick (object sender, EventArgs e)
/// </summary>
/// <returns>The item.</returns>
/// <param name="itemId">Item identifier.</param>
protected virtual TItem GetItemWithDependencies (TItemId itemId)
protected virtual TItem GetItemWithDependencies (TKey itemId)
{
return GetItem (itemId);
}

/// <summary>
/// Gets the integer identifier of the item.
/// Gets the key of the item.
/// </summary>
/// <returns>The integer identifier.</returns>
/// <returns>The key of the item.</returns>
/// <param name="item">Item.</param>
protected abstract TItemId GetItemId (TItem item);
protected abstract TKey GetItemKey (TItem item);

/// <summary>
/// Implement method which will get item by id.
/// Usually there is no need to return extra data
/// (e.g. some dependent objects or collections) here.
/// </summary>
/// <returns>The item.</returns>
/// <param name="itemId">Item identifier.</param>
protected virtual TItem GetItem (TItemId itemId) => CrudProvider.Get (itemId);
/// <param name="itemKey">Item key.</param>
protected virtual TItem GetItem (TKey itemKey) => CrudProvider.Get (itemKey);

/// <summary>
/// Implement method which will store new item in the datastore
Expand Down Expand Up @@ -319,6 +325,7 @@ protected virtual TItem GetItemWithDependencies (TItemId itemId)
protected virtual void LoadNewItem ()
{ }

// TODO: Rename to LoadPostBack?
/// <summary>
/// Override to provide code which should be called on Page_Load then (IsPostBack == true) here
/// </summary>
Expand All @@ -329,16 +336,18 @@ protected virtual void PostBack ()
/// Implement to provide code to fill item from form controls here.
/// </summary>
/// <param name="item">Item.</param>
protected abstract void BeforeUpdateItem (TItem item);
protected abstract void BeforeUpdateItem (TItem item, bool isNew);

/// <summary>
/// Implement to provide code which will be called
/// after item update in the DB
/// </summary>
/// <param name="item">Item.</param>
protected virtual void AfterUpdateItem (TItem item)
protected virtual void AfterUpdateItem (TItem item, bool isNew)
{ }

// TODO: Extract CRUD security provider

/// <summary>
/// Override to define edit permission checks here.
/// </summary>
Expand All @@ -349,6 +358,11 @@ protected virtual bool CanEditItem (TItem item)
return true;
}

protected virtual bool CanAddItem ()
{
return true;
}

/// <summary>
/// Override to define delete permission checks here.
/// </summary>
Expand Down

0 comments on commit 57c21a4

Please sign in to comment.