This repository has been archived on 2025-02-19. You can view files and clone it, but cannot push or open issues or pull requests.
Amen/Amen/Controllers/APIController.cs
2022-09-20 08:40:13 -07:00

139 lines
4.7 KiB
C#

using System;
using System.Linq;
using System.Web.Mvc;
namespace Amen.Controllers
{
public class APIController : BaseController
{
/* Previous totals before initial reset
229,412 prayers prayed before reset and 5,762,657 prayers by KTIS affiliates before migration
13,082 prayer requests before reset and 364,224 requests by KTIS affiliates before migration
6,211 notes sent before reset and 72,923 notes by KTIS affiliates before migration
*/
public const int CONST_PreviousPrayedCount = 229412 + 5762657;
public const int CONST_PreviousPrayerCount = 13082 + 364224;
public const int CONST_PreviousNotesSent = 6211 + 72923;
// GET: API/Index
public JsonResult Index()
{
return Json("", JsonRequestBehavior.AllowGet);
}
public JsonResult PraiseCount(string affiliatekey, string includeAll = "false")
{
return PrayerCount(affiliatekey, includeAll, bool.TrueString);
}
// GET: API/PrayerCount
public JsonResult PrayerCount(string affiliatekey, string includeAll = "false", string isPraise = "false")
{
var isPraiseBool = StringToBool(isPraise);
var includeAllBool = StringToBool(includeAll);
var affiliate = controllerHelper.GetAffiliate(affiliatekey);
var totalCount = controllerHelper
.db.Prayers
.Count(i => (i.AffiliateId == affiliate.Id
|| includeAllBool
)
&& !i.IsDeleted
&& i.IsPraise == isPraiseBool);
if (includeAllBool && !isPraiseBool)
{
totalCount += CONST_PreviousPrayerCount;
}
var data = new
{
count = totalCount,
display = totalCount.ToString("N0")
};
return Json(data, JsonRequestBehavior.AllowGet);
}
// GET: API/PrayedCount
public JsonResult PrayedCount(string affiliatekey, string includeAll = "false")
{
var includeAllBool = StringToBool(includeAll);
var affiliate = controllerHelper.GetAffiliate(affiliatekey);
var totalCount = controllerHelper
.db.Prayers
.Where(i => (i.AffiliateId == affiliate.Id
&& !i.IsDeleted
&& !i.IsPraise
)
|| includeAllBool
)
.Select(i => i.CountPrayed).DefaultIfEmpty(0).Sum();
if (includeAllBool)
{
totalCount += CONST_PreviousPrayedCount;
}
var data = new
{
count = totalCount,
display = totalCount.ToString("N0")
};
return Json(data, JsonRequestBehavior.AllowGet);
}
// GET: API/PrayedCount
public JsonResult NotesCount(string affiliatekey, string includeAll = "false")
{
var includeAllBool = StringToBool(includeAll);
var affiliate = controllerHelper.GetAffiliate(affiliatekey);
var totalCount = controllerHelper
.db.PrayerNotes
.Include("Prayer")
.Where(i => (i.Prayer.AffiliateId == affiliate.Id
|| includeAllBool
)
&& !i.Prayer.IsDeleted
&& !i.Prayer.IsPraise)
.Count();
if (includeAllBool)
{
totalCount += CONST_PreviousNotesSent;
}
var data = new
{
count = totalCount,
display = totalCount.ToString("N0")
};
return Json(data, JsonRequestBehavior.AllowGet);
}
public static bool StringToBool(string boolString)
{
if (boolString.Equals("1", StringComparison.InvariantCultureIgnoreCase)
|| boolString.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
{
boolString = bool.TrueString;
}
var boolValue = false;
bool.TryParse(boolString, out boolValue);
return boolValue;
}
}
}