Skip to content

Commit e0ec7ac

Browse files
author
Anand
committed
fixed bugs issue #32 #33 #34 - added migrate option and tag as a regular field - issues #30 #31
1 parent e806ecb commit e0ec7ac

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

db.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Entry struct {
2222
Url string `gorm:"column:url"`
2323
Password string `gorm:"column:password"`
2424
Notes string `gorm:"column:notes"`
25+
Tags string `gorm:"column:tags"`
2526
Timestamp time.Time `gorm:"type:timestamp;default:(datetime('now','localtime'))"` // sqlite3
2627
}
2728

@@ -56,6 +57,16 @@ func (e1 *Entry) Copy(e2 *Entry) {
5657
}
5758
}
5859

60+
// Clone an entry
61+
func (e1 *ExtendedEntry) Copy(e2 *ExtendedEntry) {
62+
63+
if e2 != nil {
64+
e1.FieldName = e2.FieldName
65+
e1.FieldValue = e2.FieldValue
66+
e1.EntryID = e2.EntryID
67+
}
68+
}
69+
5970
// Create a new database
6071
func openDatabase(filePath string) (error, *gorm.DB) {
6172

@@ -219,13 +230,15 @@ func replaceCustomEntries(db *gorm.DB, entry *Entry, updatedEntries []CustomEntr
219230
}
220231

221232
// Add a new entry to current database
222-
func addNewDatabaseEntry(title, userName, url, passwd, notes string, customEntries []CustomEntry) error {
233+
func addNewDatabaseEntry(title, userName, url, passwd, tags string,
234+
notes string, customEntries []CustomEntry) error {
223235

224236
var entry Entry
225237
var err error
226238
var db *gorm.DB
227239

228-
entry = Entry{Title: title, User: userName, Url: url, Password: passwd, Notes: notes}
240+
entry = Entry{Title: title, User: userName, Url: url, Password: passwd, Tags: strings.TrimSpace(tags),
241+
Notes: notes}
229242

230243
err, db = openActiveDatabase()
231244
if err == nil && db != nil {
@@ -247,13 +260,20 @@ func addNewDatabaseEntry(title, userName, url, passwd, notes string, customEntri
247260
}
248261

249262
// Update current database entry with new values
250-
func updateDatabaseEntry(entry *Entry, title, userName, url, passwd, notes string, customEntries []CustomEntry, flag bool) error {
263+
func updateDatabaseEntry(entry *Entry, title, userName, url, passwd, tags string,
264+
notes string, customEntries []CustomEntry, flag bool) error {
251265

252266
var updateMap map[string]interface{}
253267

254268
updateMap = make(map[string]interface{})
255269

256-
keyValMap := map[string]string{"title": title, "user": userName, "url": url, "password": passwd, "notes": notes}
270+
keyValMap := map[string]string{
271+
"title": title,
272+
"user": userName,
273+
"url": url,
274+
"password": passwd,
275+
"notes": notes,
276+
"tags": tags}
257277

258278
for key, val := range keyValMap {
259279
if len(val) > 0 {
@@ -416,11 +436,22 @@ func removeDatabaseEntry(entry *Entry) error {
416436

417437
err, db = openActiveDatabase()
418438
if err == nil && db != nil {
439+
var exEntries []ExtendedEntry
440+
419441
res := db.Delete(entry)
420442
if res.Error != nil {
421443
return res.Error
422444
}
423445

446+
// Delete extended entries if any
447+
exEntries = getExtendedEntries(entry)
448+
if len(exEntries) > 0 {
449+
res = db.Delete(exEntries)
450+
if res.Error != nil {
451+
return res.Error
452+
}
453+
}
454+
424455
return nil
425456
}
426457

@@ -450,6 +481,31 @@ func cloneEntry(entry *Entry) (error, *Entry) {
450481
return err, nil
451482
}
452483

484+
// Clone extended entries for an entry and return error code
485+
func cloneExtendedEntries(entry *Entry, exEntries []ExtendedEntry) error {
486+
487+
var err error
488+
var db *gorm.DB
489+
490+
err, db = openActiveDatabase()
491+
if err == nil && db != nil {
492+
for _, exEntry := range exEntries {
493+
var exEntryNew ExtendedEntry
494+
495+
exEntryNew.Copy(&exEntry)
496+
// Update the ID!
497+
exEntryNew.EntryID = entry.ID
498+
499+
result := db.Create(&exEntryNew)
500+
if result.Error != nil {
501+
return result.Error
502+
}
503+
}
504+
}
505+
506+
return err
507+
}
508+
453509
// Return an iterator over all entries using the given order query keys
454510
func iterateEntries(orderKey string, order string) (error, []Entry) {
455511

0 commit comments

Comments
 (0)