using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using umbracoAssi_2.Models;
using Umbraco.Web.WebApi;
namespace umbracoAssi_2.Controllers
{
public class EmailController : UmbracoApiController
{
public List<Organization> GetOrganization()
{
List<Organization> listOrg = new List<Organization>();
var root = Umbraco.TypedContentAtRoot().FirstOrDefault();
var org = root.Children.Where(x => x.DocumentTypeAlias == "organization").ToList();
foreach (var item in org)
{
Organization orgModel = new Organization();
orgModel.OrganizationName = item.GetProperty("organizationName").DataValue.ToString();
orgModel.OrganizationLocation = item.GetProperty("organizationLocation").DataValue.ToString();
orgModel.OrganizationCode = item.GetProperty("organizationCode").DataValue.ToString();
listOrg.Add(orgModel);
}
return listOrg;
}
[HttpPost]
public ResponseModel Create(Organization model)
{
ResponseModel response = new ResponseModel();
try
{
if (ModelState.IsValid)
{
var contentService = Services.ContentService;
var root = Umbraco.TypedContentAtRoot().First();
var organizationNode = root.Children.First(x => x.DocumentTypeAlias == "organization");
var organization = contentService.CreateContent("Organization", root.Id, "organization");
organization.SetValue("organizationName", model.OrganizationName);
organization.SetValue("organizationLocation", model.OrganizationLocation);
organization.SetValue("organizationCode", model.OrganizationCode);
contentService.SaveAndPublishWithStatus(organization);
var emaillistNode = contentService.CreateContent("EmailList1", organization.Id, "emailList");
contentService.SaveAndPublishWithStatus(emaillistNode);
CreateEmail(emaillistNode.Id);
response.Status = true;
response.Message = "Organization created";
}
else
{
response.Status = false;
response.Message = "Organization not Created";
}
}
catch (Exception ex)
{
response.Status = false;
response.Message = ex.Message;
}
return response;
}
public void CreateEmail(int emailNodeId)
{
try
{
var contentService = Services.ContentService;
var Email = contentService.CreateContent("Email1", emailNodeId, "email");
Email.SetValue("subject", "Test email subject");
Email.SetValue("body", "This is email test body message");
contentService.SaveAndPublishWithStatus(Email);
}
catch (Exception )
{
}
}
}
public class ResponseModel
{
public bool Status { get; set; }
public string Message { get; set; }
}
}
No comments:
Post a Comment