UserController.cs 2.99 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;

namespace AIAHTML5.ADMIN.API
{


    [RoutePrefix("User")]
    public class UserController : ApiController
    {
        AIADatabaseV5Entities dbContext = new AIADatabaseV5Entities();
        
        [Route("Api/UpdateProfile1")]
        [HttpPost]
        public HttpResponseMessage UpdateProfile1([FromBody] UserModel userInfo) 
        {
            ILog logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
            bool Status = false; 
            try
            {
                Status = UserModel.UpdateUserProfile(dbContext, userInfo.Id, userInfo.FirstName, userInfo.LastName, userInfo.EmailId);
                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("Api/GetUserProfile/{userId}")]
        [HttpGet]
        public IHttpActionResult GetUserProfile(int userId)
        {
            return Ok(dbContext.AIAUsers.Where(u => u.Id == userId).ToList());
          //return ToJson(dbContext.AIAUsers.Where(u => u.Id == userId).AsEnumerable());            

        }
        [Route("Api/UpdateProfile")]
        [HttpPost]
        public HttpResponseMessage UpdateUserProfile(UserModel userInfo)
        {
            bool Status = false;
            try
            {
                Status = UserModel.UpdateUserProfile(dbContext, userInfo.Id, userInfo.FirstName, userInfo.LastName, userInfo.EmailId);
                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;
        }
    }
}