mirror of
https://github.com/wahyd4/zendesk.git
synced 2026-08-08 21:02:44 +10:00
Add search and list searchable field function
This commit is contained in:
@@ -3,19 +3,19 @@ package index
|
||||
import "github.com/wahyd4/zendesk/model"
|
||||
|
||||
type OrganisationIndex struct {
|
||||
data map[string]map[string][]*model.Organisation
|
||||
Data map[string]map[string][]*model.Organisation
|
||||
}
|
||||
|
||||
func (index OrganisationIndex) ListSearchableFields() []string {
|
||||
var result []string
|
||||
for field := range index.data {
|
||||
for field := range index.Data {
|
||||
result = append(result, field)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (index OrganisationIndex) Search(field, value string) []string {
|
||||
organisations := index.data[field][value]
|
||||
organisations := index.Data[field][value]
|
||||
|
||||
var result []string
|
||||
for _, organisation := range organisations {
|
||||
|
||||
+3
-3
@@ -3,19 +3,19 @@ package index
|
||||
import "github.com/wahyd4/zendesk/model"
|
||||
|
||||
type TicketIndex struct {
|
||||
data map[string]map[string][]*model.Ticket
|
||||
Data map[string]map[string][]*model.Ticket
|
||||
}
|
||||
|
||||
func (index TicketIndex) ListSearchableFields() []string {
|
||||
var result []string
|
||||
for field := range index.data {
|
||||
for field := range index.Data {
|
||||
result = append(result, field)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (index TicketIndex) Search(field, value string) []string {
|
||||
tickets := index.data[field][value]
|
||||
tickets := index.Data[field][value]
|
||||
|
||||
var result []string
|
||||
for _, ticket := range tickets {
|
||||
|
||||
+3
-3
@@ -3,19 +3,19 @@ package index
|
||||
import "github.com/wahyd4/zendesk/model"
|
||||
|
||||
type UserIndex struct {
|
||||
data map[string]map[string][]*model.User
|
||||
Data map[string]map[string][]*model.User
|
||||
}
|
||||
|
||||
func (index UserIndex) ListSearchableFields() []string {
|
||||
var result []string
|
||||
for field := range index.data {
|
||||
for field := range index.Data {
|
||||
result = append(result, field)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (index UserIndex) Search(field, value string) []string {
|
||||
users := index.data[field][value]
|
||||
users := index.Data[field][value]
|
||||
|
||||
var result []string
|
||||
for _, user := range users {
|
||||
|
||||
+5
-9
@@ -3,6 +3,7 @@ package search
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/wahyd4/zendesk/index"
|
||||
"github.com/wahyd4/zendesk/model"
|
||||
)
|
||||
|
||||
@@ -19,10 +20,7 @@ type APP struct {
|
||||
users map[string]*model.User
|
||||
tickets map[string]*model.Ticket
|
||||
|
||||
organisationIndex map[string]map[string][]*model.Organisation
|
||||
userIndex map[string]map[string][]*model.User
|
||||
ticketIndex map[string]map[string][]*model.Ticket
|
||||
|
||||
indexes map[string]index.SearchIndex
|
||||
searchContext *SearchContext
|
||||
}
|
||||
|
||||
@@ -43,11 +41,9 @@ func InitAPP(organisationsFile, usersFile, ticketsFile string) *APP {
|
||||
}
|
||||
|
||||
return &APP{
|
||||
jsonContents: jsonContents,
|
||||
organisationIndex: make(map[string]map[string][]*model.Organisation),
|
||||
userIndex: make(map[string]map[string][]*model.User),
|
||||
ticketIndex: make(map[string]map[string][]*model.Ticket),
|
||||
searchContext: &SearchContext{},
|
||||
jsonContents: jsonContents,
|
||||
indexes: make(map[string]index.SearchIndex),
|
||||
searchContext: &SearchContext{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+17
-3
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/wahyd4/zendesk/index"
|
||||
"github.com/wahyd4/zendesk/model"
|
||||
)
|
||||
|
||||
@@ -49,7 +50,7 @@ func (app *APP) buildOrganisationIndex() error {
|
||||
if len(organisations) == 0 {
|
||||
return nil
|
||||
}
|
||||
searchIndex := app.organisationIndex
|
||||
searchIndex := make(map[string]map[string][]*model.Organisation)
|
||||
|
||||
// get first entity from json array as the template
|
||||
organisationsTemplate := organisations[0]
|
||||
@@ -78,6 +79,10 @@ func (app *APP) buildOrganisationIndex() error {
|
||||
|
||||
}
|
||||
}
|
||||
// update index
|
||||
app.indexes[OrganisationsKey] = index.OrganisationIndex{
|
||||
Data: searchIndex,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -94,7 +99,7 @@ func (app *APP) buildUserIndex() error {
|
||||
if len(users) == 0 {
|
||||
return nil
|
||||
}
|
||||
searchIndex := app.userIndex
|
||||
searchIndex := make(map[string]map[string][]*model.User)
|
||||
|
||||
// get first entity from json array as the template
|
||||
entityTemplate := users[0]
|
||||
@@ -123,6 +128,10 @@ func (app *APP) buildUserIndex() error {
|
||||
|
||||
}
|
||||
}
|
||||
// update index
|
||||
app.indexes[UsersKey] = index.UserIndex{
|
||||
Data: searchIndex,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -139,7 +148,7 @@ func (app *APP) buildTicketIndex() error {
|
||||
if len(tickets) == 0 {
|
||||
return nil
|
||||
}
|
||||
searchIndex := app.ticketIndex
|
||||
searchIndex := make(map[string]map[string][]*model.Ticket)
|
||||
|
||||
// get first entity from json array as the template
|
||||
entityTemplate := tickets[0]
|
||||
@@ -174,6 +183,11 @@ func (app *APP) buildTicketIndex() error {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// update index
|
||||
app.indexes[TicketsKey] = index.TicketIndex{
|
||||
Data: searchIndex,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+15
-1
@@ -11,10 +11,24 @@ func (app *APP) UpdateDataType(dataType string) {
|
||||
app.searchContext = &SearchContext{
|
||||
dataType: dataType,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// UpdateFieldName updates search field name of a data type
|
||||
func (app *APP) UpdateFieldName(fieldName string) {
|
||||
app.searchContext.fieldName = fieldName
|
||||
}
|
||||
|
||||
// ListSearchableFields list all the searchable fields
|
||||
func (app *APP) ListSearchableFields() []string {
|
||||
dataType := app.searchContext.dataType
|
||||
|
||||
return app.indexes[dataType].ListSearchableFields()
|
||||
}
|
||||
|
||||
// ListSearchableFields list all the searchable fields
|
||||
func (app *APP) Search(value string) []string {
|
||||
dataType := app.searchContext.dataType
|
||||
field := app.searchContext.fieldName
|
||||
|
||||
return app.indexes[dataType].Search(field, value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user