SiteController.cs 5.39 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("Site")]
    public class SiteController : ApiController
    {
        AIADatabaseV5Entities dbContext = new AIADatabaseV5Entities();

        [Route("SiteDetail")]
        [HttpGet]
        public HttpResponseMessage GetSiteById(int SiteId)
        {
            SiteModel SiteEntity = new SiteModel();
            try
            {
                SiteEntity = SiteModel.GetSiteById(dbContext, SiteId);
                return Request.CreateResponse(HttpStatusCode.OK, SiteEntity);
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

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

        [Route("InsertUpdateSiteAccount")]
        [HttpPost]
        public HttpResponseMessage InsertUpdateSiteAccount(JObject jsonData)
        {
            bool Status = false;
            SiteModel SiteEntity = new SiteModel();
            SiteEntity.LicenseId = jsonData["licenseId"].Value<int>();
            SiteEntity.Id = jsonData["siteId"].Value<int>();
            SiteEntity.Title = jsonData["title"].Value<string>();
            SiteEntity.Ip = jsonData["siteUrl"].Value<string>();
            SiteEntity.SiteIpTo = jsonData["siteUrlTo"].Value<string>();
            SiteEntity.MasterIpTo = jsonData["siteMasterUrlTo"].Value<string>();
            SiteEntity.InstituteName = jsonData["institutionName"].Value<string>();
            SiteEntity.Department = jsonData["departmentName"].Value<string>();
            SiteEntity.Address1 = jsonData["address1"].Value<string>();
            SiteEntity.Address2 = jsonData["address2"].Value<string>();
            SiteEntity.City = jsonData["city"].Value<string>();
            SiteEntity.Phone = jsonData["phone"].Value<string>(); 
            SiteEntity.Zip = jsonData["zip"].Value<string>();
            SiteEntity.CountryId = jsonData["countryId"].Value<int>();
            SiteEntity.StateId = jsonData["stateId"].Value<int>();
            SiteEntity.SiteUserId = jsonData["userId"].Value<int>();
            SiteEntity.IsActive = jsonData["isActive"].Value<bool>();
            SiteEntity.IsMaster = jsonData["isMaster"].Value<bool>();
            SiteEntity.SiteEditionIds = jsonData["siteEditionIds"].Value<string>();
            SiteEntity.CreationDate = jsonData["creationDate"].Value<DateTime>();
            SiteEntity.ModifiedDate = jsonData["modifiedDate"].Value<DateTime>();
            try
            {
                Status = SiteModel.InsertUpdateSiteAccount(dbContext, SiteEntity);
                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("DeleteSiteAccount")]
        [HttpGet]
        public HttpResponseMessage DeleteSiteAccount(int SiteId, int LicenseId, int UserId)
        {
            bool Status = false;
            try
            {
                Status = SiteModel.DeleteSiteAccount(dbContext, SiteId, LicenseId, UserId);
                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);
            }
        }

        protected HttpResponseMessage ToJson(dynamic obj)
        {
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/jsonP");
            return response;
        }
    }
}