LicenseController.cs 10.4 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)        
        {
            List<LicenseModel> LicenseList = new List<LicenseModel>();
            try
            {
                LicenseList = LicenseModel.GetLicenses(dbContext, accountNumber, licenseeFirstName, licenseeLastName, licenseTypeId, institutionName,
                    stateId, countryId, emailId, subscriptionStartDate, subscriptionEndDate, isActive);
                return Request.CreateResponse(HttpStatusCode.OK, LicenseList);
            }
            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);
            }
        }
    }
}