LicenseController.cs 18.1 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using AIAHTML5.ADMIN.API.Models;
using System.Web.Http.Cors;
using System.Web.Cors;
using AIAHTML5.Server.Constants;
using log4net;
using System.Text;
using AIAHTML5.ADMIN.API.Entity;

namespace AIAHTML5.ADMIN.API.Controllers
{
    //[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
    [RoutePrefix("License")]
    public class LicenseController : ApiController
    {
        AIADatabaseV5Entities dbContext = new AIADatabaseV5Entities();

        [Route("LicenseTypes")]
        [HttpGet]
        public HttpResponseMessage GetLicenseTypes()
        {
            List<LicenseTypeModel> LicenseTypeList = new List<LicenseTypeModel>();
            try
            {
                LicenseTypeList = LicenseTypeModel.GetLicenseTypes(dbContext);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseTypeList);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("Licenses")]
        [HttpGet]
        public HttpResponseMessage GetLicenses(string accountNumber, string licenseeFirstName, string licenseeLastName, byte licenseTypeId,
            string institutionName, int stateId, int countryId, string emailId, DateTime subscriptionStartDate, DateTime subscriptionEndDate,
            bool isActive, int pageNo, int pageLength)
        {
            List<LicenseModel> LicenseList = new List<LicenseModel>();
            int recordCount = 0;
            try
            {
                LicenseList = LicenseModel.GetLicenses(dbContext, accountNumber, licenseeFirstName, licenseeLastName, licenseTypeId, institutionName,
                    stateId, countryId, emailId, subscriptionStartDate, subscriptionEndDate, isActive, pageNo, pageLength, out recordCount);
                return Request.CreateResponse(HttpStatusCode.OK, new { LicenseList = LicenseList, RecordCount = recordCount });
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }


        [Route("InsertLicense")]
        [HttpPost]
        public HttpResponseMessage InsertLicense(JObject jsonData)
        {
            bool Status = false;
            LicenseModel licenseModel = new LicenseModel();
            licenseModel.LicenseId = jsonData["licenseId"].Value<int>();
            licenseModel.AccountNumber = jsonData["accountNumber"].Value<string>();
            licenseModel.LicenseeFirstName = jsonData["licenseeFirstName"].Value<string>();
            licenseModel.LicenseeLastName = jsonData["licenseeLastName"].Value<string>();
            licenseModel.LicenseTypeId = jsonData["licenseTypeId"].Value<byte>();
            licenseModel.AccountTypeId = jsonData["accountTypeId"].Value<byte>();
            licenseModel.InstitutionName = jsonData["institutionName"].Value<string>();
            licenseModel.Address1 = jsonData["address1"].Value<string>();
            licenseModel.Address2 = jsonData["address2"].Value<string>();
            licenseModel.City = jsonData["city"].Value<string>();
            licenseModel.Zip = jsonData["zip"].Value<string>();
            licenseModel.StateId = jsonData["stateId"].Value<int?>();
            licenseModel.CountryId = jsonData["countryId"].Value<int?>();
            licenseModel.Phone = jsonData["phone"].Value<string>();
            licenseModel.EmailId = jsonData["email"].Value<string>();
            licenseModel.TotalLogins = jsonData["totalLogins"].Value<int?>();
            licenseModel.EditionLogins = jsonData["editionLogins"].Value<string>();
            licenseModel.Price = jsonData["price"].Value<decimal>();
            licenseModel.ProductKey = jsonData["productKey"].Value<string>();
            licenseModel.MasterSiteUrl = jsonData["masterSiteUrl"].Value<string>();
            licenseModel.SiteUrlFrom = jsonData["siteFromUrl"].Value<string>();
            licenseModel.SiteUrlTo = jsonData["siteToUrl"].Value<string>();
            licenseModel.NoOfImages = jsonData["noOfImages"].Value<int?>();
            licenseModel.LoginId = jsonData["loginId"].Value<string>();
            licenseModel.Password = jsonData["password"].Value<string>();
            licenseModel.SubscriptionStartDate = jsonData["subscriptionStartDate"].Value<DateTime>();
            licenseModel.SubscriptionEndDate = jsonData["subscriptionEndDate"].Value<DateTime>();
            licenseModel.SecurityQuestionId = jsonData["securityQuestionId"].Value<byte?>();
            licenseModel.Answer = jsonData["answer"].Value<string>();
            licenseModel.TestLicenseEditionId = jsonData["testLicenseEditionId"].Value<byte?>();
            licenseModel.CreatorId = jsonData["creatorId"].Value<int>();
            try
            {
                Status = LicenseModel.InsertLicense(dbContext, licenseModel);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("UpdateLicense")]
        [HttpPost]
        public HttpResponseMessage UpdateLicense(JObject jsonData)
        {
            bool Status = false;
            LicenseModel licenseModel = new LicenseModel();
            licenseModel.LicenseId = jsonData["licenseId"].Value<int>();
            licenseModel.AccountNumber = jsonData["accountNumber"].Value<string>();
            licenseModel.LicenseeFirstName = jsonData["licenseeFirstName"].Value<string>();
            licenseModel.LicenseeLastName = jsonData["licenseeLastName"].Value<string>();
            licenseModel.LicenseTypeId = jsonData["licenseTypeId"].Value<byte>();
            licenseModel.AccountTypeId = jsonData["accountTypeId"].Value<byte>();
            licenseModel.InstitutionName = jsonData["institutionName"].Value<string>();
            licenseModel.Address1 = jsonData["address1"].Value<string>();
            licenseModel.Address2 = jsonData["address2"].Value<string>();
            licenseModel.City = jsonData["city"].Value<string>();
            licenseModel.Zip = jsonData["zip"].Value<string>();
            licenseModel.StateId = jsonData["stateId"].Value<int?>();
            licenseModel.CountryId = jsonData["countryId"].Value<int?>();
            licenseModel.Phone = jsonData["phone"].Value<string>();
            licenseModel.EmailId = jsonData["email"].Value<string>();
            licenseModel.TotalLogins = jsonData["totalLogins"].Value<int?>();
            licenseModel.EditionLogins = jsonData["editionLogins"].Value<string>();
            licenseModel.Price = jsonData["price"].Value<decimal>();
            licenseModel.ProductKey = jsonData["productKey"].Value<string>();
            licenseModel.MasterSiteUrl = jsonData["masterSiteUrl"].Value<string>();
            licenseModel.SiteUrlFrom = jsonData["siteUrlFrom"].Value<string>();
            licenseModel.SiteUrlTo = jsonData["siteUrlTo"].Value<string>();
            licenseModel.NoOfImages = jsonData["noOfImages"].Value<int?>();
            licenseModel.LoginId = jsonData["loginId"].Value<string>();
            licenseModel.Password = jsonData["password"].Value<string>();
            licenseModel.SubscriptionStartDate = jsonData["subscriptionStartDate"].Value<DateTime>();
            licenseModel.SubscriptionEndDate = jsonData["subscriptionEndDate"].Value<DateTime>();
            licenseModel.RenewDate = jsonData["renewDate"].Value<DateTime>();
            licenseModel.SecurityQuestionId = jsonData["securityQuestionId"].Value<byte?>();
            licenseModel.Answer = jsonData["answer"].Value<string>();
            licenseModel.TestLicenseEditionId = jsonData["testLicenseEditionId"].Value<byte?>();
            licenseModel.CreatorId = jsonData["creatorId"].Value<int>();
            licenseModel.IsActive = jsonData["isActive"].Value<bool>();
            licenseModel.IsRenew = jsonData["renew"].Value<bool>();
            try
            {
                Status = LicenseModel.UpdateLicense(dbContext, licenseModel);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("GetLicense")]
        [HttpGet]
        public HttpResponseMessage GetLicense(int LicenseId)
        {
            LicenseModel LicenseEntity = new LicenseModel();
            try
            {
                LicenseEntity = LicenseModel.GetLicenseById(dbContext, LicenseId);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseEntity);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("DeleteLicense")]
        [HttpGet]
        public HttpResponseMessage DeleteLicense(int LicenseId)
        {
            bool Status = false;
            try
            {
                Status = LicenseModel.DeleteLicense(dbContext, LicenseId);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("LicenseAccounts")]
        [HttpGet]
        public HttpResponseMessage GetLicenseAccounts(int LicenseType)
        {
            List<Tuple<int, string>> LicenseAccountList = new List<Tuple<int, string>>();
            try
            {
                LicenseAccountList = LicenseModel.GetLicenseAccounts(dbContext, LicenseType);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseAccountList);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("LicenseSites")]
        [HttpGet]
        public HttpResponseMessage GetLicenseAccounts(string AccountNo)
        {
            List<SiteModel> LicenseSiteList = new List<SiteModel>();
            try
            {
                LicenseSiteList = LicenseModel.GetLicenseSites(dbContext, AccountNo);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseSiteList);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("LicenseModestySettings")]
        [HttpGet]
        public HttpResponseMessage GetLicenseModestySettings(int LicenseId, int BuildingLevelId)
        {
            List<Tuple<int, bool, string>> LicenseModestyList = new List<Tuple<int, bool, string>>();
            try
            {
                LicenseModestyList = LicenseModel.GetLicenseModestySettings(dbContext, LicenseId, BuildingLevelId);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseModestyList);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("UpdateLicenseBasicSettings")]
        [HttpPost]
        public HttpResponseMessage UpdateLicenseBasicSettings(JObject jsonData)
        {
            bool Status = false;
            LicenseModel licenseModel = new LicenseModel();
            licenseModel.LicenseId = jsonData["licenseId"].Value<int>();
            licenseModel.AccountNumber = jsonData["accountNumber"].Value<string>();
            licenseModel.LicenseeFirstName = jsonData["licenseeFirstName"].Value<string>();
            licenseModel.LicenseeLastName = jsonData["licenseeLastName"].Value<string>();
            licenseModel.InstitutionName = jsonData["institutionName"].Value<string>();
            licenseModel.Address1 = jsonData["address1"].Value<string>();
            licenseModel.Address2 = jsonData["address2"].Value<string>();
            licenseModel.City = jsonData["city"].Value<string>();
            licenseModel.Zip = jsonData["zip"].Value<string>();
            licenseModel.StateId = jsonData["stateId"].Value<int?>();
            licenseModel.CountryId = jsonData["countryId"].Value<int?>();
            licenseModel.Phone = jsonData["phone"].Value<string>();
            licenseModel.EmailId = jsonData["email"].Value<string>();
            try
            {
                Status = LicenseModel.UpdateLicenseBasicSettings(dbContext, licenseModel);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("UpdateLicenseModestySettings")]
        [HttpPost]
        public HttpResponseMessage UpdateLicenseModestySettings(JObject jsonData)
        {
            bool Status = false;
            List<Tuple<int, int, bool>> LicenseModestyList = new List<Tuple<int, int, bool>>();
            Tuple<int, int, bool> LicenseModesty;
            for (int i = 0; i < jsonData["obj"].Count(); i++)
            {
                LicenseModesty = new Tuple<int, int, bool>(
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["licenseEditionId"])).Value<int>(),
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["siteId"])).Value<int>(),
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["isModesty"])).Value<bool>());
                LicenseModestyList.Add(LicenseModesty);
            }
            try
            {
                Status = LicenseModel.UpdateLicenseModestySettings(dbContext, LicenseModestyList);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("LicenseModulesStatus")]
        [HttpGet]
        public HttpResponseMessage GetLicenseModulesStatus(int LicenseId)
        {
            List<Tuple<int, bool, string>> LicenseModulesStatusList = new List<Tuple<int, bool, string>>();
            try
            {
                LicenseModulesStatusList = LicenseModel.GetLicenseModulesStatus(dbContext, LicenseId);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseModulesStatusList);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("UpdateLicenseModulesStatus")]
        [HttpPost]
        public HttpResponseMessage UpdateLicenseModulesStatus(JObject jsonData)
        {
            bool Status = false;
            List<Tuple<int, int, bool>> LicenseModuleStatusList = new List<Tuple<int, int, bool>>();
            Tuple<int, int, bool> LicenseModuleStatus;
            for (int i = 0; i < jsonData["obj"].Count(); i++)
            {
                LicenseModuleStatus = new Tuple<int, int, bool>(
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["licenseId"])).Value<int>(),
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["moduleId"])).Value<int>(),
                    ((Newtonsoft.Json.Linq.JValue)(jsonData["obj"][i]["status"])).Value<bool>());
                LicenseModuleStatusList.Add(LicenseModuleStatus);
            }
            try
            {
                Status = LicenseModel.UpdateLicenseModulesStatus(dbContext, LicenseModuleStatusList);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, Status.ToString());
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

    }
}