From 08894f1ef49d2e28484962d084757914c83199c4 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 7 Nov 2019 23:39:16 -0500 Subject: [PATCH] working search --- entry/entry.go | 27 +++++++++++++++++---- frontend/src/components/MainEditor.vue | 11 ++++----- frontend/src/components/Search.vue | 5 +++- frontend/src/store/index.js | 28 +++++++++++----------- frontend/src/views/Console.vue | 13 ++++------ todo.adoc | 33 ++++++++++---------------- web/entry.go | 7 ++++-- 7 files changed, 67 insertions(+), 57 deletions(-) diff --git a/entry/entry.go b/entry/entry.go index 12f2ccf..ab640c3 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -15,6 +15,7 @@ type Entry struct { db *db.Database ID int64 Slug string + Title string Content string Tags []string Created time.Time @@ -66,6 +67,7 @@ func GetBySlug(db *db.Database, slug string) (Entry, error) { if err := db.Get(&e, q, slug); err != nil { return e, err } + e.Title = e.GenerateTitle() return e, e.populateTags() } @@ -75,14 +77,16 @@ func GetByID(db *db.Database, id int64) (Entry, error) { if err := db.Get(&e, q, id); err != nil { return e, err } + e.Title = e.GenerateTitle() return e, e.populateTags() } func Search(db *db.Database, query string) ([]*Entry, error) { entries := []*Entry{} + log.Debug().Str("query", query).Msg("searching") if query != "" { - q := `select * from entries where content like '%?%'` - err := db.Select(&entries, q, query) + q := `select * from entries where content like ?` + err := db.Select(&entries, q, "%"+query+"%") if err != nil { return nil, err } @@ -95,6 +99,7 @@ func Search(db *db.Database, query string) ([]*Entry, error) { } for _, e := range entries { e.db = db + e.Title = e.GenerateTitle() e.populateTags() } return entries, nil @@ -141,18 +146,16 @@ func (e *Entry) removeTag(tag string) error { return err } -func (e *Entry) UniqueSlug() string { +func (e *Entry) GenerateTitle() string { candidate := strings.Split(e.Content, "\n")[0] candidateNumber := 0 r := regexp.MustCompile(`[^a-zA-Z0-9 -]`) candidate = r.ReplaceAllString(candidate, "") candidate = strings.TrimSpace(candidate) - candidate = strings.ReplaceAll(candidate, " ", "-") if len(candidate) == 0 { candidate = "untitled" } - candidate = strings.ToLower(candidate) q := `select slug from entries where slug like ?` slugs := []string{} @@ -179,6 +182,20 @@ func (e *Entry) UniqueSlug() string { return tmpCandidate } +func (e *Entry) UniqueSlug() string { + if e.Title == "" { + e.Title = e.GenerateTitle() + } + candidate := e.Title + + r := regexp.MustCompile(`[^a-zA-Z0-9 -]`) + candidate = r.ReplaceAllString(candidate, "") + candidate = strings.TrimSpace(candidate) + candidate = strings.ReplaceAll(candidate, " ", "-") + candidate = strings.ToLower(candidate) + return candidate +} + func (e *Entry) Update() error { if e.ID == -1 { return e.Create() diff --git a/frontend/src/components/MainEditor.vue b/frontend/src/components/MainEditor.vue index a4ef250..cbc41ca 100644 --- a/frontend/src/components/MainEditor.vue +++ b/frontend/src/components/MainEditor.vue @@ -56,23 +56,22 @@ this.$store.dispatch('getFile', slug) }, tagUpdate: function(newTags) { - if (JSON.stringify(newTags) === JSON.stringify(this.file.Tags)) + if (JSON.stringify(newTags) === JSON.stringify(this.$store.state.file.Tags)) return - this.file.Tags = newTags + this.$store.commit('setTags', newTags) this.$emit('markDirty', true) this.save() }, updateContent: function (newContent) { if (this.$store.state.file.Content === newContent) return - this.$store.state.file.Content = newContent + this.$store.commit('setContent', newContent) this.$emit('markDirty', true) this.save() }, save: function () { - this.$store.state.file.Content = this.content - console.log("Saving file: " + this.file.Slug) - this.$store.dispatch('saveFile', this.file) + this.$store.commit('setContent', this.content) + this.$store.dispatch('saveFile') .then(res => { this.$emit('markDirty', false) this.$store.dispatch('updateSearch') diff --git a/frontend/src/components/Search.vue b/frontend/src/components/Search.vue index c5c3630..22b0dda 100644 --- a/frontend/src/components/Search.vue +++ b/frontend/src/components/Search.vue @@ -7,7 +7,7 @@ {{item.Slug}} + >{{item.Title}} @@ -29,6 +29,9 @@ watch: { query: function(newValue) { this.queryText = newValue + }, + queryText: function(newValue) { + this.$store.commit('setQuery', newValue) } }, computed: { diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index 6831687..bb8a06e 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -40,6 +40,12 @@ export default new Vuex.Store({ }, setFile(state, file) { state.file = file + }, + setContent(state, content) { + state.file.Content = content + }, + setTags(state, tags) { + state.file.Tags = tags } }, actions: { @@ -51,29 +57,23 @@ export default new Vuex.Store({ commit('setFile', res.data) }) }, - getSearchResults: function ({dispatch, commit}, query) { - commit('setQuery', query) + getSearchResults: function ({dispatch}) { dispatch('updateSearch') }, updateSearch: function ({commit, state}) { - let query = state.query - if (query) { - axios.get('/v1/entries?query='+query) - .catch(err => state.addError(err)) - .then(res =>{ - console.log("getSearchResults:"+res.data) - commit('setResults', res.data) - }) - } - axios.get('/v1/entries') + let query = state.query || "" + console.log("running search for: " + query) + axios.get('/v1/entries?query='+query) .catch(err => state.addError(err)) .then(res =>{ console.log("getSearchResults:"+res.data) commit('setResults', res.data) }) }, - saveFile: function(state, file) { - return axios.put('/v1/entries/'+file.Slug, file) + saveFile: function({state}) { + console.log(state.file) + if (state.file) + return axios.put('/v1/entries/'+state.file.Slug, state.file) } }, modules: { diff --git a/frontend/src/views/Console.vue b/frontend/src/views/Console.vue index ce3cebd..2eeadc4 100644 --- a/frontend/src/views/Console.vue +++ b/frontend/src/views/Console.vue @@ -8,7 +8,7 @@
- +