using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DotNetNuke.Security;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Web.Api;
using <%= fullNamespace %>.Components;
using <%= fullNamespace %>.Models;
namespace <%= fullNamespace %>.Services
{
///
/// This is a partial class that spans multiple class files, in order to keep the code manageable. Each method is necessary to support the front end SPA implementation.
///
///
/// The SupportModules attribute will require that all API calls set and include module headers, event GET requests. Even Fiddler will return 401 Unauthorized errors.
///
[SupportedModules("<%= friendlyName %>")]
public partial class ExampleController : ServiceBase
{
///
/// Get an event
///
///
///
/// GET: http://dnndev.me/DesktopModules/MVC/<%= fullNamespace %>/API/Example/GetExamples
///
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[HttpGet]
public HttpResponseMessage GetExamples()
{
try
{
var examples = ExampleDataAccess.GetItems(ActiveModule.ModuleID);
var response = new ServiceResponse> { Content = examples.ToList() };
if (examples == null || !examples.Any())
{
ServiceResponseHelper>.AddNoneFoundError("ExampleInfo", ref response);
}
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
}
}
///
/// Get an event
///
///
///
/// GET: http://dnndev.me/DesktopModules/<%= fullNamespace %>/MVC/API/Example/GetExample
///
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[HttpGet]
public HttpResponseMessage GetExample(int exampleId)
{
try
{
var example = ExampleDataAccess.GetItem(exampleId, ActiveModule.ModuleID);
var response = new ServiceResponse { Content = example };
if (example == null)
{
ServiceResponseHelper.AddNoneFoundError("ExampleInfo", ref response);
}
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
}
}
///
/// Delete an event
///
///
///
/// DELETE: http://dnndev.me/DesktopModules/<%= fullNamespace %>/MVC/API/Example/DeleteExample
///
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Edit)]
[ValidateAntiForgeryToken]
[HttpDelete]
public HttpResponseMessage DeleteExample(int exampleId)
{
try
{
ExampleDataAccess.DeleteItem(exampleId, ActiveModule.ModuleID);
var response = new ServiceResponse { Content = SUCCESS_MESSAGE };
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
}
}
///
/// Create an event
///
///
///
/// POST: http://dnndev.me/DesktopModules/MVC/<%= fullNamespace %>/API/Example/CeateExample
///
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Edit)]
[ValidateAntiForgeryToken]
[HttpPost]
public HttpResponseMessage CreateExample(ExampleInfo newExample)
{
try
{
newExample.CreatedOnDate = DateTime.Now;
newExample.CreatedByUserId = UserInfo.UserID;
newExample.LastUpdatedOnDate = DateTime.Now;
newExample.LastUpdatedByUserId = UserInfo.UserID;
newExample.ModuleId = ActiveModule.ModuleID;
var security = new PortalSecurity();
newExample.Title = security.InputFilter(newExample.Title.Trim(), PortalSecurity.FilterFlag.NoMarkup);
newExample.Description = security.InputFilter(newExample.Description.Trim(), PortalSecurity.FilterFlag.NoMarkup);
ExampleDataAccess.CreateItem(newExample);
var response = new ServiceResponse { Content = Globals.RESPONSE_SUCCESS };
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
}
}
///
/// Update an event
///
///
///
/// POST: http://dnndev.me/DesktopModules/MVC/<%= fullNamespace %>/API/Example/UpdateExample
///
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Edit)]
[ValidateAntiForgeryToken]
[HttpPost]
public HttpResponseMessage UpdateExample(ExampleInfo example)
{
try
{
var originalExample = ExampleDataAccess.GetItem(example.ExampleId, example.ModuleId);
var updatesToProcess = ExampleHasUpdates(ref originalExample, ref example);
if (updatesToProcess)
{
originalExample.LastUpdatedOnDate = DateTime.Now;
originalExample.LastUpdatedByUserId = UserInfo.UserID;
var security = new PortalSecurity();
originalExample.Title = security.InputFilter(originalExample.Title.Trim(), PortalSecurity.FilterFlag.NoMarkup);
originalExample.Description = security.InputFilter(originalExample.Description.Trim(), PortalSecurity.FilterFlag.NoMarkup);
ExampleDataAccess.UpdateItem(originalExample);
}
var savedExample = ExampleDataAccess.GetItem(originalExample.ExampleId, originalExample.ModuleId);
var response = new ServiceResponse { Content = savedExample };
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
catch (Exception ex)
{
Exceptions.LogException(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ERROR_MESSAGE);
}
}
///
/// Use to determine if the user has edit permissions
///
///
///
/// GET: http://dnndev.me/DesktopModules/MVC/<%= fullNamespace %>/API/Example/UserCanEditExample
///
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage UserCanEditExample()
{
ServiceResponse response = null;
if (UserInfo.IsSuperUser || UserInfo.IsInRole(PortalSettings.AdministratorRoleName) || ModulePermissionController.HasModulePermission(ActiveModule.ModulePermissions, "Edit"))
{
response = new ServiceResponse() { Content = Globals.RESPONSE_SUCCESS };
}
else
{
response = new ServiceResponse() { Content = Globals.RESPONSE_FAILURE };
}
return Request.CreateResponse(HttpStatusCode.OK, response.ObjectToJson());
}
#region Private Helper Methods
private bool ExampleHasUpdates(ref ExampleInfo originalExample, ref ExampleInfo newExample)
{
var updatesToProcess = false;
if (!string.Equals(newExample.Title, originalExample.Title))
{
originalExample.Title = newExample.Title;
updatesToProcess = true;
}
if (!string.Equals(newExample.Description, originalExample.Description))
{
originalExample.Description = newExample.Description;
updatesToProcess = true;
}
if (newExample.ModuleId != originalExample.ModuleId)
{
originalExample.ModuleId = newExample.ModuleId;
updatesToProcess = true;
}
return updatesToProcess;
}
#endregion
}
}