diff --git a/entry/entry.go b/entry/entry.go
index ab640c3..18a46d1 100644
--- a/entry/entry.go
+++ b/entry/entry.go
@@ -52,13 +52,18 @@ func PrepareTable(tx *sqlx.Tx) error {
return nil
}
-func New(db *db.Database) Entry {
- return Entry{
+func New(db *db.Database) *Entry {
+ e := Entry{
db: db,
ID: -1,
Created: time.Now(),
Updated: time.Now(),
+ Tags: []string{},
}
+ e.Title = e.GenerateTitle()
+ e.Slug = e.UniqueSlug()
+ e.Content = "= " + e.Title
+ return &e
}
func GetBySlug(db *db.Database, slug string) (Entry, error) {
@@ -85,13 +90,13 @@ 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 ?`
+ q := `select * from entries where content like ? order by updated desc`
err := db.Select(&entries, q, "%"+query+"%")
if err != nil {
return nil, err
}
} else {
- q := `select * from entries`
+ q := `select * from entries order by updated desc`
err := db.Select(&entries, q)
if err != nil {
return nil, err
@@ -119,12 +124,12 @@ func RemoveBySlug(db *db.Database, slug string) error {
tx.Rollback()
return err
}
- q = `delete from entries where entry_id = ?`
+ q = `delete from entries where id = ?`
if _, err := tx.Exec(q, e.ID); err != nil {
tx.Rollback()
return err
}
- return nil
+ return tx.Commit()
}
func (e *Entry) populateTags() error {
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 0335ceb..d8074ae 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -7,6 +7,11 @@
Console
About
+
+
+ +
+
+
@@ -42,6 +47,16 @@
name: 'app',
components: {
Error
+ },
+ methods: {
+ newFile: function() {
+ this.$store.dispatch('newFile')
+ .catch(err => console.log(err))
+ .then(file => {
+ this.$store.dispatch('updateSearch')
+ this.$router.push({ name: 'console-slug', params: { slug: file.Slug }})
+ })
+ }
}
}
diff --git a/frontend/src/components/Search.vue b/frontend/src/components/Search.vue
index 22b0dda..286df71 100644
--- a/frontend/src/components/Search.vue
+++ b/frontend/src/components/Search.vue
@@ -1,11 +1,11 @@
-
+
- X {{item.Title}}
@@ -24,7 +24,8 @@
},
props: {
target: String,
- query: String
+ query: String,
+ editMode: Boolean
},
watch: {
query: function(newValue) {
@@ -42,15 +43,21 @@
},
created() {
this.getResults()
- this.runQuery = _.debounce(this.runQuery, 1000)
+ this.getResults = _.debounce(this.getResults, 1000)
},
methods: {
getResults: function () {
- this.$store.dispatch('getSearchResults', null)
+ this.$store.dispatch('updateSearch')
},
- runQuery: function() {
- this.$store.dispatch('getSearchResults', this.query)
+ deleteFile: function(slug) {
+ this.$store.dispatch('deleteBySlug', slug)
}
}
}
+
+
diff --git a/frontend/src/components/TagList.vue b/frontend/src/components/TagList.vue
index d11e030..7440927 100644
--- a/frontend/src/components/TagList.vue
+++ b/frontend/src/components/TagList.vue
@@ -23,7 +23,7 @@
},
watch: {
tags: function (newValue) {
- this.internalTags = newValue
+ this.internalTags = newValue || []
}
},
computed: {
diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js
index bb8a06e..2f28a9a 100644
--- a/frontend/src/store/index.js
+++ b/frontend/src/store/index.js
@@ -49,6 +49,19 @@ export default new Vuex.Store({
}
},
actions: {
+ newFile: function({commit}) {
+ return new Promise((resolve, reject) => {
+ axios.post('/v1/entries', {})
+ .catch(err => {
+ commit('addError', err)
+ reject(err)
+ })
+ .then(res => {
+ commit('setFile', res.data)
+ resolve(res.data)
+ })
+ })
+ },
getFile: function({ commit }, slug) {
if (slug)
return axios.get('/v1/entries/'+slug)
@@ -57,23 +70,36 @@ export default new Vuex.Store({
commit('setFile', res.data)
})
},
- getSearchResults: function ({dispatch}) {
- dispatch('updateSearch')
- },
updateSearch: function ({commit, state}) {
- 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)
- })
+ return new Promise((resolve, reject) => {
+ let query = state.query || ""
+ console.log("running search for: " + query)
+ axios.get('/v1/entries?query='+query)
+ .catch(err => {
+ state.addError(err)
+ reject(err)
+ })
+ .then(res =>{
+ commit('setResults', res.data)
+ resolve(res.data)
+ })
+ })
},
saveFile: function({state}) {
console.log(state.file)
if (state.file)
return axios.put('/v1/entries/'+state.file.Slug, state.file)
+ },
+ deleteBySlug: function({dispatch,commit}, slug) {
+ axios.delete('/v1/entries/'+slug)
+ .catch(err => {
+ commit('addError', err)
+ console.log(err)
+ })
+ .then(res => {
+ dispatch('updateSearch')
+ console.log(res.status, res.statusText)
+ })
}
},
modules: {
diff --git a/frontend/src/views/Console.vue b/frontend/src/views/Console.vue
index 2eeadc4..b817794 100644
--- a/frontend/src/views/Console.vue
+++ b/frontend/src/views/Console.vue
@@ -25,7 +25,7 @@
Search Results
-
+
diff --git a/web/entry.go b/web/entry.go
index c26ce9e..bb3018f 100644
--- a/web/entry.go
+++ b/web/entry.go
@@ -130,6 +130,7 @@ func (web *Web) removeEntry(w http.ResponseWriter, r *http.Request) {
err := entry.RemoveBySlug(web.db, slug)
if err != nil {
+ log.Error().Msgf("Error deleting: %s", err)
w.WriteHeader(500)
fmt.Fprint(w, err)
return
diff --git a/web/routes.go b/web/routes.go
index 04950d5..15162c6 100644
--- a/web/routes.go
+++ b/web/routes.go
@@ -11,25 +11,25 @@ import (
"github.com/speps/go-hashids"
- "github.com/gorilla/mux"
"github.com/gorilla/handlers"
+ "github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"github.com/stretchr/graceful"
)
type Web struct {
- addr string
- db *db.Database
- salt string
- h *hashids.HashID
- box *packr.Box
+ addr string
+ db *db.Database
+ salt string
+ h *hashids.HashID
+ box *packr.Box
}
func New(addr string, db *db.Database, box *packr.Box) *Web {
w := &Web{
- addr: addr,
- db: db,
- box: box,
+ addr: addr,
+ db: db,
+ box: box,
}
if err := db.MakeDB(); err != nil {
log.Fatal().
@@ -44,7 +44,7 @@ func (web *Web) routeSetup() http.Handler {
api := r.PathPrefix("/v1/").Subrouter()
api.HandleFunc("/entries", web.allEntries).Methods(http.MethodGet)
api.HandleFunc("/entries", web.newEntry).Methods(http.MethodPost)
- api.HandleFunc("/entries", web.removeEntry).Methods(http.MethodDelete)
+ api.HandleFunc("/entries/{slug}", web.removeEntry).Methods(http.MethodDelete)
api.HandleFunc("/entries/{slug}", web.editEntry).Methods(http.MethodPut)
api.HandleFunc("/entries/{slug}", web.getEntry).Methods(http.MethodGet)
r.PathPrefix("/").HandlerFunc(web.indexHandler("/index.html"))