public bool AddEmployee(Admin admin, Employee employee) { if (employee == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var employeeExist = GetEmployeeById(admin, employee.employeeid); if (employeeExist != null) throw new Exception(ErrorConstants.EMPLOYEE_WITH_GIVEN_ID_ALREADY_EXIST); var response = elasticClient.Index<Employee>(employee, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool AddProduct(Admin admin, Product product) { if (product == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var productExist = GetProductById(admin, product.productid); if (productExist != null) throw new Exception(ErrorConstants.PRODUCT_WITH_GIVEN_ID_ALREADY_EXIST); var elasticClient = GetElasticClient(); var response = elasticClient.Index<Product>(product, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public Product GetProductById(Admin admin, string id) { if (String.IsNullOrWhiteSpace(id)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Product>(g => g .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) .Filter(f => f.Term(ConstProduct.ID, id)) .Size(1)); Product product = null; if(response.Total > 0) foreach (var item in response.Hits) product = item.Source; return product; } catch (Exception e) { throw e; } }
public Employee GetEmployeeById(Admin admin, string employeeid) { if (String.IsNullOrWhiteSpace(employeeid)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Employee>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) .Filter(f => f.Term(ConstEmployee.ID, employeeid)) .Size(1)); Employee retEmployee = null; if (response.Total > 0) foreach (var item in response.Hits) retEmployee = item.Source; return retEmployee; } catch (Exception e) { throw e; } }
public bool AddUser(Admin admin, User user) { if (user == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var userExist = GetUserById(admin, user.userid); if (userExist != null) throw new Exception(ErrorConstants.USER_WITH_GIVEN_ID_ALREADY_EXIST); var response = elasticClient.Index<User>(user, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_USER) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool CreateAdmin(Admin admin, string userName) { if (String.IsNullOrWhiteSpace(userName)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.SUPER_ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); var newAdmin = new Admin() { id = Guid.NewGuid(), username = userName, type = (int)BillingEnums.USER_TYPE.ADMIN, created_at = DateTime.UtcNow }; try { var elasticClient = GetElasticClient(); var userNameFilter = new TermFilter() { Field = ConstAdmin.USER_NAME, Value = userName }; var adminList = elasticClient.Search<Admin>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Filter(userNameFilter) ); if(adminList.Total > 0) { throw new Exception(ErrorConstants.ADMIN_USERNAME_ALREADY_TAKEN); } var response = elasticClient.Index<Admin>(newAdmin, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) ); return response.Created; } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().Message); throw e; } }
public Admin LoginAdmin(string userName, string password) { if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(password)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); try { var elasticClient = GetElasticClient(); var userNameFilter = new TermFilter() { Field = ConstAdmin.USER_NAME, Value = userName }; var loginFilters = new List<FilterContainer>(); loginFilters.Add(userNameFilter); var loginFilter = new AndFilter(); loginFilter.Filters = loginFilters; var loginResponse = elasticClient.Search<Admin>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Filter(loginFilter) .Size(1)); var admin = new Admin(); if (loginResponse.Total > 0) { foreach (IHit<Admin> hit in loginResponse.Hits) { admin = hit.Source; } if (!PasswordHash.ValidatePassword(password, admin.password, admin.salt)) throw new Exception(ErrorConstants.WRONG_PASSWORD); return admin; } else return null; } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().Message); return null; } }
public void AddSuperAdmin() { var elasticClient = GetElasticClient(); var termFilter = new TermFilter() { Field = ConstAdmin.TYPE, Value = (short)BillingEnums.USER_TYPE.SUPER_ADMIN }; try { var response = elasticClient.Search<Admin>(a => a .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Filter(termFilter) .Take(1)); if (response.Total == 0) { var salt = PasswordHash.GenerateSalt(); var admin = new Admin() { id = Guid.NewGuid(), username = AppConstants.SUPER_ADMIN_USER_NAME, salt = salt, password = PasswordHash.CreateHash(AppConstants.SUPER_ADMIN_PASSWORD, salt), type = (int)BillingEnums.USER_TYPE.SUPER_ADMIN, created_at = DateTime.UtcNow }; var create = elasticClient.Index<Admin>(admin, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN)); } } catch (Exception e) { Console.Error.WriteLine(e.GetBaseException().Message); } }
public bool AddSales(Admin admin, Sales sales, List<SalesInfo> salesInfo, bool stock) { if (sales == null || salesInfo == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var salesResponse = elasticClient.Index<Sales>(sales, i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_SALES) ); var insertDescriptor = new BulkDescriptor(); foreach (var item in salesInfo) { item.salesid = sales.salesid; insertDescriptor.Index<SalesInfo>(i => i .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_SALES_INFO) .Document(item) ); } var bulkResponse = elasticClient.Bulk(insertDescriptor); return salesResponse.RequestInformation.Success && bulkResponse.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool UpdateProduct(Admin admin, Product product) { if (product == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var productElasticId = GetProductId(product.productid); if(String.IsNullOrWhiteSpace(productElasticId)) { throw new Exception(ErrorConstants.PRODUCT_NOT_FOUND); } var productUpdate = new Dictionary<string, object>(); if (!String.IsNullOrWhiteSpace(product.productname)) productUpdate[ConstProduct.PRODUCT_NAME] = product.productname; if (product.price>0) productUpdate[ConstProduct.PRICE] = product.price; if (product.quantity>0) productUpdate[ConstProduct.QUANTITY] = product.quantity; if (product.unit>0) productUpdate[ConstProduct.UNIT] = product.unit; var elasticClient = GetElasticClient(); var response = elasticClient.Update<Product, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) .Id(productElasticId) .Doc(productUpdate) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public List<Employee> GetEmployeeList(Admin admin, int start, int size) { if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Employee>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) .Skip(start) .Take(size) ); var employeeList = new List<Employee>(); if (response.Total > 0) foreach (var item in response.Hits) employeeList.Add(item.Source); return employeeList; } catch (Exception e) { throw e; } }
public List<Admin> GetAdminList(Admin admin) { if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.SUPER_ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Admin>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Filter(f => f.Term(ConstAdmin.TYPE, (int)BillingEnums.USER_TYPE.ADMIN)) .SortAscending(a => a.username) .Size(int.MaxValue)); if (!response.RequestInformation.Success) throw new Exception(ErrorConstants.PROBLEM_OCCURES_ON_RETRIVING_ADMIN_LIST); var adminList = new List<Admin>(); foreach (var hit in response.Hits) { var resultAdmin = hit.Source; resultAdmin.salt = null; resultAdmin.password = null; adminList.Add(resultAdmin); } return adminList; } catch (Exception e ) { throw e; } }
public bool DeleteAdmin(Admin admin, Guid adminId) { if (adminId == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.SUPER_ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var deleteAdmin = GetAdminById(adminId); if (deleteAdmin == null) throw new Exception(ErrorConstants.ADMIN_NOT_FOUND); var elasticClient = GetElasticClient(); var response = elasticClient.Delete<Admin>(adminId.ToString(), d => d .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN)); if (response.RequestInformation.Success) return true; return false; } catch (Exception e) { throw e; } }
public bool ChangePassword(Admin admin, string oldPassword, string newPassword) { if (String.IsNullOrWhiteSpace(oldPassword) || String.IsNullOrWhiteSpace(newPassword)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var updateAdmin = GetAdminById(admin.id); if (updateAdmin == null) throw new Exception(ErrorConstants.ADMIN_NOT_FOUND); if (!PasswordHash.ValidatePassword(oldPassword, updateAdmin.password, updateAdmin.salt)) throw new Exception(ErrorConstants.WRONG_PASSWORD); var newPasswordHash = PasswordHash.CreateHash(newPassword, updateAdmin.salt); var elasticClient = GetElasticClient(); var passwordDict = new Dictionary<string, object>(); passwordDict[ConstAdmin.PASSWORD] = newPasswordHash; var response = elasticClient.Update<Admin, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Id(admin.id.ToString()) .Doc(passwordDict)); if (response.RequestInformation.Success) return true; return false; } catch (Exception e) { throw e; } }
public string GenerateAdminPassword(Admin admin, Guid userId) { if (userId == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.SUPER_ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var updateAdmin = GetAdminById(userId); if (updateAdmin == null) throw new Exception(ErrorConstants.ADMIN_NOT_FOUND); var password = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10); updateAdmin.salt = PasswordHash.GenerateSalt(); updateAdmin.password = PasswordHash.CreateHash(password, updateAdmin.salt); var adminUpdate = new Dictionary<string, object>(); adminUpdate[ConstAdmin.SALT] = updateAdmin.salt; adminUpdate[ConstAdmin.PASSWORD] = updateAdmin.password; var elasticClient = GetElasticClient(); var updateResponse = elasticClient.Update<Admin, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_ADMIN) .Id(updateAdmin.id.ToString()) .Doc(adminUpdate)); if (!updateResponse.RequestInformation.Success) throw new Exception(ErrorConstants.PROBLEM_OCCURED_WHILE_GENERATING_PASSWORD); return password; } catch (Exception e) { throw e; } }
public bool UpdateEmployee(Admin admin, Employee employee) { if (employee == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var employeeElasticId = GetEmployeeId(employee.employeeid); if (String.IsNullOrWhiteSpace(employeeElasticId)) throw new Exception(ErrorConstants.EMPLOYEE_NOT_FOUND); var employeeUpdate = new Dictionary<string, object>(); if (!String.IsNullOrWhiteSpace(employee.name)) employeeUpdate[ConstEmployee.NAME] = employee.name; if (!String.IsNullOrWhiteSpace(employee.addr1)) employeeUpdate[ConstEmployee.ADDR_1] = employee.addr1; if (!String.IsNullOrWhiteSpace(employee.addr2)) employeeUpdate[ConstEmployee.ADDR_2] = employee.addr2; if (!String.IsNullOrWhiteSpace(employee.city)) employeeUpdate[ConstEmployee.CITY] = employee.city; if (!String.IsNullOrWhiteSpace(employee.district)) employeeUpdate[ConstEmployee.DISTRICT] = employee.district; if (!String.IsNullOrWhiteSpace(employee.state)) employeeUpdate[ConstEmployee.STATE] = employee.state; if (!String.IsNullOrWhiteSpace(employee.country)) employeeUpdate[ConstEmployee.COUNTRY] = employee.country; if (!String.IsNullOrWhiteSpace(employee.pincode)) employeeUpdate[ConstEmployee.PIN_CODE] = employee.pincode; if (!String.IsNullOrWhiteSpace(employee.phone)) employeeUpdate[ConstEmployee.PHONE] = employee.phone; if (!String.IsNullOrWhiteSpace(employee.email)) employeeUpdate[ConstEmployee.EMAIL] = employee.email; if (!String.IsNullOrWhiteSpace(employee.designation)) employeeUpdate[ConstEmployee.DESIGNATION] = employee.designation; var elasticClient = GetElasticClient(); var response = elasticClient.Update<Employee, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) .Id(employeeElasticId) .Doc(employeeUpdate) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public List<Employee> GetEmployeesByName(Admin admin, string name, int start, int size) { if (String.IsNullOrWhiteSpace(name)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Employee>(s => s .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) .Query(q => q.Prefix(ConstEmployee.NAME, name)) .Skip(start) .Take(size) ); var employeeList = new List<Employee>(); if (response.Total > 0) foreach (var item in response.Hits) employeeList.Add(item.Source); return employeeList; } catch (Exception e) { throw e; } }
public bool UpdateUser(Admin admin, User user) { if (user == null) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var userElasticId = GetUserId(user.userid); if (String.IsNullOrWhiteSpace(userElasticId)) throw new Exception(ErrorConstants.USER_NOT_FOUND); var userUpdate = new Dictionary<string, object>(); if (!String.IsNullOrWhiteSpace(user.name)) userUpdate[ConstUser.NAME] = user.name; if (!String.IsNullOrWhiteSpace(user.addr1)) userUpdate[ConstUser.ADDR_1] = user.addr1; if (!String.IsNullOrWhiteSpace(user.addr2)) userUpdate[ConstUser.ADDR_2] = user.addr2; if (!String.IsNullOrWhiteSpace(user.city)) userUpdate[ConstUser.CITY] = user.city; if (!String.IsNullOrWhiteSpace(user.district)) userUpdate[ConstUser.DISTRICT] = user.district; if (!String.IsNullOrWhiteSpace(user.state)) userUpdate[ConstUser.STATE] = user.state; if (!String.IsNullOrWhiteSpace(user.country)) userUpdate[ConstUser.COUNTRY] = user.country; if (!String.IsNullOrWhiteSpace(user.pincode)) userUpdate[ConstUser.PIN_CODE] = user.pincode; if (!String.IsNullOrWhiteSpace(user.phone)) userUpdate[ConstUser.PHONE] = user.phone; if (!String.IsNullOrWhiteSpace(user.email)) userUpdate[ConstUser.EMAIL] = user.email; var elasticClient = GetElasticClient(); var response = elasticClient.Update<User, object>(u => u .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_USER) .Id(userElasticId) .Doc(userUpdate) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool DeleteEmployee(Admin admin, String id) { if (String.IsNullOrWhiteSpace(id)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (int)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var employeeElasticId = GetEmployeeId(id); if (String.IsNullOrWhiteSpace(employeeElasticId)) throw new Exception(ErrorConstants.EMPLOYEE_NOT_FOUND); var elasticClient = GetElasticClient(); var response = elasticClient.Delete<Employee>(employeeElasticId, d => d .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_EMPLOYEE) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public bool DeleteProduct(Admin admin, string id) { if (String.IsNullOrWhiteSpace(id)) throw new Exception(ErrorConstants.REQUIRED_FIELD_EMPTY); if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var productElasticId = GetProductId(id); if (String.IsNullOrWhiteSpace(productElasticId)) throw new Exception(ErrorConstants.PRODUCT_NOT_FOUND); var elasticClient = GetElasticClient(); var response = elasticClient.Delete<Product>(productElasticId, d => d .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) ); return response.RequestInformation.Success; } catch (Exception e) { throw e; } }
public List<Product> GetProductList(Admin admin, int start, int size) { if (admin == null || admin.type != (short)BillingEnums.USER_TYPE.ADMIN) throw new Exception(ErrorConstants.NO_PREVILAGE); try { var elasticClient = GetElasticClient(); var response = elasticClient.Search<Product>(g => g .Index(ElasticMappingConstants.INDEX_NAME) .Type(ElasticMappingConstants.TYPE_PRODUCT) .Skip(start) .Take(size) ); List<Product> productList = new List<Product>(); if(response.Total > 0) foreach (var item in response.Hits) productList.Add(item.Source); return productList; } catch (Exception e) { throw e; } }