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 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("GetUserDetail/{userId}")] [HttpGet] public IHttpActionResult GetUserDetail(int userId) { dbContext.Configuration.ProxyCreationEnabled = false; try { return Ok(dbContext.AIAUsers.Where(u => u.Id == userId).Select(s => new UserModel { DeactivationDate=s.DeactivationDate.Value, Createdby = dbContext.AIAUsers.Where(sub1 => sub1.Id == s.CreatorId).Select(sub1 => sub1.FirstName ).FirstOrDefault()+ " "+ dbContext.AIAUsers.Where(sub1 => sub1.Id == s.CreatorId).Select(sub1 => sub1.LastName).FirstOrDefault(), Modifiedby = dbContext.AIAUsers.Where(sub1 => sub1.Id == s.ModifierId).Select(sub1 => sub1.FirstName ).FirstOrDefault() + " " + dbContext.AIAUsers.Where(sub1 => sub1.Id == s.ModifierId).Select(sub1 => sub1.LastName).FirstOrDefault() }).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(); string newPassword = jsonData["newPassword"].Value(); 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 userTypelist = new List(); 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 pageNo, int pageLength, int iLoginUserType,string loggedIn="") { try { int UserTypeId = (!string.IsNullOrEmpty(usertypeid) ? Convert.ToInt32(usertypeid) : 0); int AccountTypeId = (!string.IsNullOrEmpty(accounttypeid) ? Convert.ToInt32(accounttypeid) : 0); int recordCount = 0; dbContext.Configuration.ProxyCreationEnabled = false; //var spStatus = new System.Data.Objects.ObjectParameter("Status", 0); var spRecordCount = new System.Data.Objects.ObjectParameter("recordCount", 0); //recordCount = (int)spRecordCount.Value; List Users = dbContext.usp_GetUsersList(firstname, lastname, emailid, accountnumber, UserTypeId, AccountTypeId, iLoginUserType, pageNo, pageLength, spRecordCount).ToList(); if (!string.IsNullOrEmpty(loggedIn)) { if (Users.Where(s => s.LoginId == loggedIn).Count() > 0) { Users = Users.Where(s => s.LoginId != loggedIn).ToList(); spRecordCount.Value = (int)spRecordCount.Value - 1; } } return Ok(new { UserList = Users, RecordCount = spRecordCount.Value }); } catch(Exception ex) { return BadRequest(); } } [Route("UpdateUser")] [HttpPost] public HttpResponseMessage UpdateUser(JObject jsonUserData) { string Status = string.Empty; UserModel UserEntity = new UserModel(); UserEntity.Id = jsonUserData["id"].Value(); UserEntity.FirstName = jsonUserData["FirstName"].Value(); UserEntity.LastName = jsonUserData["LastName"].Value(); UserEntity.EmailId = jsonUserData["EmailId"].Value(); UserEntity.LoginId = jsonUserData["UserName"].Value(); UserEntity.Password = jsonUserData["Password"].Value(); UserEntity.IsActive = jsonUserData["IsActive"].Value(); UserEntity.CreatorId = jsonUserData["Modifiedby"].Value(); 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 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 CheckedUserRights = new List(); List UnCheckedUserRights = new List(); 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 userTypelist = new List(); var userTypeEntity = dbContext.usp_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 AccountNumberList = new List(); 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 ProductEditionList = new List(); 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(); UserEntity.FirstName = jsonUserData["FirstName"].Value(); UserEntity.LastName = jsonUserData["LastName"].Value(); UserEntity.EmailId = jsonUserData["EmailId"].Value(); UserEntity.LoginId = jsonUserData["UserName"].Value(); UserEntity.Password = jsonUserData["Password"].Value(); UserEntity.LicenseId = jsonUserData["AccountNumberId"].Value(); UserEntity.iUserTypeId = jsonUserData["UserTypeId"].Value(); UserEntity.EditionId = jsonUserData["ProductEditionId"].Value(); 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 Users = dbContext.usp_GetBlockedUserByAccNoAndType((byte)UserTypeId, LicenseId).ToList(); return Ok(Users); } [Route("UnblockedUser")] [HttpPost] public HttpResponseMessage UnblockedUser(List 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 #region MenuBindForGeneralAdmin [Route("ManageMenu")] [HttpGet] public IHttpActionResult ManageMenu(int UserId, string UserType) { dbContext.Configuration.ProxyCreationEnabled = false; try { List UserRights = dbContext.usp_GetManageRights(UserId, UserType).Where(s=>s.MenuStatus=="1").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); } } #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; } } }