public async Task <DatabaseResponse> CreateAdminUser(RegisterAdminUser adminuser, int AdminUserID) { try { SqlParameter[] parameters = { new SqlParameter("@FullName", SqlDbType.NVarChar), new SqlParameter("@Email", SqlDbType.NVarChar), new SqlParameter("@Password", SqlDbType.NVarChar), new SqlParameter("@RoleID", SqlDbType.Int), new SqlParameter("@CreatedBy", SqlDbType.Int) }; parameters[0].Value = adminuser.FullName; parameters[1].Value = adminuser.Email; parameters[2].Value = new Sha2().Hash(adminuser.Password); parameters[3].Value = adminuser.RoleID; parameters[4].Value = AdminUserID; _DataHelper = new DataAccessHelper("Admin_CreateAdminUser", parameters, _configuration); DataTable dt = new DataTable(); int result = await _DataHelper.RunAsync(dt); AdminUsers newCustomer = new AdminUsers(); if (dt != null && dt.Rows.Count > 0) { newCustomer = (from model in dt.AsEnumerable() select new AdminUsers() { AdminUserID = model.Field <int>("AdminUserID"), Email = model.Field <string>("Email"), Password = model.Field <string>("Password"), Name = model.Field <string>("Name"), Role = model.Field <string>("Role"), }).FirstOrDefault(); } return(new DatabaseResponse { ResponseCode = result, Results = adminuser }); } catch (Exception ex) { LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); throw (ex); } finally { _DataHelper.Dispose(); } }
public async Task <IActionResult> Create([FromHeader(Name = "Grid-Authorization-Token")] string token, [FromBody] RegisterAdminUser adminuser) { try { if (string.IsNullOrEmpty(token)) { return(Ok(new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, Message = EnumExtensions.GetDescription(CommonErrors.TokenEmpty) })); } AdminUsersDataAccess _adminUsersDataAccess = new AdminUsersDataAccess(_iconfiguration); DatabaseResponse tokenAuthResponse = await _adminUsersDataAccess.AuthenticateAdminUserToken(token); if (tokenAuthResponse.ResponseCode == (int)DbReturnValue.AuthSuccess) { if (!((AuthTokenResponse)tokenAuthResponse.Results).IsExpired) { int _AdminUserID = ((AuthTokenResponse)tokenAuthResponse.Results).CustomerID; if (!ModelState.IsValid) { LogInfo.Error(StatusMessages.DomainValidationError); new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, Message = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)) }; } DatabaseResponse response = await _adminUsersDataAccess.CreateAdminUser(adminuser, _AdminUserID); if (response.ResponseCode == ((int)DbReturnValue.EmailExists)) { return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.EmailExists), IsDomainValidationErrors = true })); } else { return(Ok(new OperationResponse { HasSucceeded = true, Message = EnumExtensions.GetDescription(DbReturnValue.CreateSuccess), IsDomainValidationErrors = false, ReturnedObject = response.Results })); } } else { //Token expired LogInfo.Warning(EnumExtensions.GetDescription(CommonErrors.ExpiredToken)); return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenExpired), IsDomainValidationErrors = true })); } } else { // token auth failure LogInfo.Warning(EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed)); return(Ok(new OperationResponse { HasSucceeded = false, Message = EnumExtensions.GetDescription(DbReturnValue.TokenAuthFailed), IsDomainValidationErrors = false })); } } catch (Exception ex) { LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); return(Ok(new OperationResponse { HasSucceeded = false, Message = StatusMessages.ServerError, IsDomainValidationErrors = false })); } }