UserController.cs 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
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("User")]

    public class UserController : ApiController
    {
        AIADatabaseV5Entities dbContext = new AIADatabaseV5Entities();

        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        [Route("GetUserProfile/{userId}")]
        [HttpGet]
        public IHttpActionResult GetUserProfile(int userId)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            try
            {
                return Ok(dbContext.AIAUsers.Where(u => u.Id == userId).ToList());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //return ToJson(dbContext.AIAUsers.Where(u => u.Id == userId).AsEnumerable());            

        }
        [Route("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);
            }
        }
        [Route("ChangeUserPassword")]
        [HttpPost]
        public HttpResponseMessage UpdateUserPassword(JObject jsonData)
        {
            bool Status = false;
            int id = jsonData["id"].Value<Int32>();
            string newPassword = jsonData["newPassword"].Value<string>();
            try
            {
                Status = UserModel.UpdateUserPassword(dbContext, id, newPassword);
                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("UpdateUserId")]
        [HttpPost]
        public HttpResponseMessage UpdateUserId(UserModel userInfo)
        {
            string Status = string.Empty;
            try
            {
                Status = UserModel.UpdateUserId(dbContext, userInfo.Id, userInfo.NewLoginId, userInfo.LoginId);
                if (Status.Equals("1"))
                {
                    return Request.CreateResponse(HttpStatusCode.OK, "success");
                }
                else if (Status.Equals("2"))
                {
                    return Request.CreateResponse(HttpStatusCode.OK, "Already Exist Userid");
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Please try again some time later.");
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        #region USERS List
        [Route("GetUserType/{UserTypeId}")]
        [HttpGet]
        public IHttpActionResult GetUserType(int UserTypeId)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<UserType> userTypelist = new List<UserType>();
            var userTypeEntity = dbContext.usp_GetUserType(UserTypeId).ToList();
            userTypelist = userTypeEntity.Select(l => new UserType() { Id = l.Id, Title = l.Title }).ToList();
            //userTypelist.Insert(0, new UserType { Id = 0, Title = "All" });
            return Ok(userTypelist);
        }

        [Route("GetAccountType/{AccountTypeId}")]
        [HttpGet]
        public IHttpActionResult GetAccountType(int AccountTypeId)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            return Ok(AccountTypeModel.GetAccountTypeList(dbContext, AccountTypeId));
        }

        [Route("Users")]
        [HttpGet]
        public IHttpActionResult UserList(string firstname, string lastname, string emailid, string accountnumber, string usertypeid, string accounttypeid)
        {
            int UserTypeId = (!string.IsNullOrEmpty(usertypeid) ? Convert.ToInt32(usertypeid) : 0);
            int AccountTypeId = (!string.IsNullOrEmpty(accounttypeid) ? Convert.ToInt32(accounttypeid) : 0);
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<usp_GetSearchUserList_Result> Users = dbContext.usp_GetSearchUserList(firstname, lastname, emailid, accountnumber, UserTypeId, AccountTypeId, 1).ToList();
            return Ok(Users);
        }

        [Route("UpdateUser")]
        [HttpPost]
        public HttpResponseMessage UpdateUser(JObject jsonUserData)
        {
            string Status = string.Empty;
            UserModel UserEntity = new UserModel();
            UserEntity.Id = jsonUserData["id"].Value<int>();
            UserEntity.FirstName = jsonUserData["FirstName"].Value<string>();
            UserEntity.LastName = jsonUserData["LastName"].Value<string>();
            UserEntity.EmailId = jsonUserData["EmailId"].Value<string>();
            UserEntity.LoginId = jsonUserData["UserName"].Value<string>();
            UserEntity.Password = jsonUserData["Password"].Value<string>();
            UserEntity.IsActive = jsonUserData["IsActive"].Value<bool>();
            UserEntity.CreatorId = jsonUserData["Modifiedby"].Value<int>();
            try
            {
                Status = UserModel.UpdateUser(dbContext, UserEntity);
                if (Status.Equals("1"))
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "User Name already exist");
                }
                else if (Status.Equals("2"))
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Email Id already exist");
                }
                else if (Status.Equals("3"))
                {
                    return Request.CreateResponse(HttpStatusCode.OK, "User updated successfully");
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "We have encountered a technical error and same has been notified to our technical team.");
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "We have encountered a technical error and same has been notified to our technical team.");
            }
        }

        [Route("ManageRight")]
        [HttpGet]
        public IHttpActionResult UserManageRight(int UserId,string UserType)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            try
            {
                List<usp_GetManageRights_Result> UserRights = dbContext.usp_GetManageRights(UserId, UserType).ToList();
                return Ok(UserRights);
            }
            catch(Exception ex)
            {
                var message = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent("We have encountered a technical error and same has been notified to our technical team.")
                };
                throw new HttpResponseException(message);
            }                       
        }

        [Route("InsertDeleteUserManageRights")]
        [HttpPost]
        public HttpResponseMessage InsertDeleteUserManageRights(JObject jsonUserData)
        {
            bool Status = false;
            var jsonString = jsonUserData;
            try
            {
                int UserId = 0;
                string RoleName = string.Empty;
                List<int> CheckedUserRights = new List<int>();
                List<int> UnCheckedUserRights = new List<int>();
                foreach (var item in jsonUserData)
                {
                    if(item.Key=="UserId")
                    {
                         UserId = Convert.ToInt32(item.Value);
                    }
                    else if (item.Key == "UserType")
                    {
                        RoleName = item.Value.ToString();
                    }
                    else if (item.Key == "CheckedUserRights")
                    {
                        JArray jsonVal = JArray.Parse(item.Value.ToString()) as JArray;
                        dynamic CheckedUserRightsList = jsonVal;
                        foreach (dynamic itemCheckedUserRights in CheckedUserRightsList)
                        {
                            CheckedUserRights.Add(Convert.ToInt32(itemCheckedUserRights));                            
                        }
                    }
                    else if (item.Key == "UnCheckedUserRights")
                    {
                        JArray jsonVal = JArray.Parse(item.Value.ToString()) as JArray;
                        dynamic CheckedUserRightsList = jsonVal;
                        foreach (dynamic itemCheckedUserRights in CheckedUserRightsList)
                        {
                            UnCheckedUserRights.Add(Convert.ToInt32(itemCheckedUserRights));
                        }
                    }
                    
                    
                }
                Status = UserModel.InsertDeleteUserManageRight(dbContext, CheckedUserRights, UnCheckedUserRights, UserId, RoleName);
                if (Status)
                {
                    return Request.CreateResponse(HttpStatusCode.OK, "Done");
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, Status.ToString());
                }              
                
               
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        #endregion
        #region Add User 
        [Route("GetUserTypebyLicenseId")]
        [HttpGet]
        public IHttpActionResult GetUserTypebyLicenseId(int UserTypeId, int LicenseId)
        {
            short UserType = (short)UserTypeId;
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<GetUserTyeByAccountNumber_Result> userTypelist = new List<GetUserTyeByAccountNumber_Result>();
            var userTypeEntity = dbContext.GetUserTyeByAccountNumber((byte)UserType, LicenseId).ToList();
            userTypelist = userTypeEntity.Select(l => new GetUserTyeByAccountNumber_Result() { Id = l.Id, Title = l.Title }).ToList();
            if (userTypelist != null && userTypelist.Count==0)
            {
                userTypelist.Insert(0, new GetUserTyeByAccountNumber_Result { Id = 2, Title = "General Admin" });
            }
            //userTypelist.Insert(0, new UserType { Id = 0, Title = "All" });
            return Ok(userTypelist);
        }

        [Route("GetAccountNumber")]
        [HttpGet]
        public IHttpActionResult GetAccountNumber()
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<usp_GetAccountNumber_Result> AccountNumberList = new List<usp_GetAccountNumber_Result>();
            var AccountNumberEntity = dbContext.usp_GetAccountNumber(0).ToList();
            AccountNumberList = AccountNumberEntity.Select(l => new usp_GetAccountNumber_Result() { Id = l.Id, AccountNumber = l.AccountNumber }).ToList();
            //userTypelist.Insert(0, new UserType { Id = 0, Title = "All" });
            return Ok(AccountNumberList);
        }

        [Route("GetProductEdition")]
        [HttpGet]
        public IHttpActionResult GetProductEditionByLicense(int LicenseId)
        {
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<usp_GetProductEditionByLicense_Result> ProductEditionList = new List<usp_GetProductEditionByLicense_Result>();
            var ProductEditionListEntity = dbContext.usp_GetProductEditionByLicense(LicenseId).ToList();
            ProductEditionList = ProductEditionListEntity.Select(l => new usp_GetProductEditionByLicense_Result() { Id = l.Id, Title = l.Title }).ToList();
            //userTypelist.Insert(0, new UserType { Id = 0, Title = "All" });
            return Ok(ProductEditionList);
        }

        [Route("NewUser")]
        [HttpPost]
        public HttpResponseMessage InsertUser(JObject jsonUserData)
        {
            string Status = string.Empty;
            UserModel UserEntity = new UserModel();
            UserEntity.Id = jsonUserData["id"].Value<int>();
            UserEntity.FirstName = jsonUserData["FirstName"].Value<string>();
            UserEntity.LastName = jsonUserData["LastName"].Value<string>();
            UserEntity.EmailId = jsonUserData["EmailId"].Value<string>();
            UserEntity.LoginId = jsonUserData["UserName"].Value<string>();
            UserEntity.Password = jsonUserData["Password"].Value<string>();
            UserEntity.LicenseId = jsonUserData["AccountNumberId"].Value<int>();
            UserEntity.iUserTypeId = jsonUserData["UserTypeId"].Value<short>();
            UserEntity.EditionId = jsonUserData["ProductEditionId"].Value<int>();
            
            try
            {
                Status = UserModel.InsertUser(dbContext, UserEntity);
                if (Status.Equals("1"))
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "User Name already exist");
                }
                else if (Status.Equals("2"))
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Email Id already exist");
                }
                else if (Status.Equals("3"))
                {
                    return Request.CreateResponse(HttpStatusCode.OK, "User added successfully");
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "We have encountered a technical error and same has been notified to our technical team.");
                }
            }
            catch (Exception ex)
            {
                // Log exception code goes here  
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "We have encountered a technical error and same has been notified to our technical team.");
            }
        }
        #endregion

        #region UnBlock Users
        [Route("BlockedUser")]
        [HttpGet]
        public IHttpActionResult GetBlockedUserByAccNoAndType(int UserTypeId, int LicenseId)
        {           
            dbContext.Configuration.ProxyCreationEnabled = false;
            List<GetBlockedUserByAccNoAndType_Result> Users = dbContext.GetBlockedUserByAccNoAndType((byte)UserTypeId, LicenseId).ToList();
            return Ok(Users);
        }
        [Route("UnblockedUser")]
        [HttpPost]
        public HttpResponseMessage UnblockedUser(List<int> UserIds)
        {
            bool Status = false;
            try
            {
                Status = UserModel.UpdateUnblockedUser(dbContext, UserIds);
                Status = true;
                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);
            }
        }

        #endregion

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