-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
47 lines (34 loc) · 859 Bytes
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"strconv"
"gorm.io/gorm"
)
func Delete(db *gorm.DB, reference string) {
ID, err := strconv.Atoi(reference)
var result *gorm.DB
if err == nil {
result = db.Table("Todos").Delete(&Todo{ID: ID})
} else {
records := CheckRecordsExistance(db, reference)
if len(records) == 0 {
fmt.Println("Record(s) Doesn't Exist, try another <Name>")
return
}
result = db.Table("Todos").Where("Name", reference).Delete(&Todo{})
}
if result.Error != nil {
fmt.Println("Error: ", result.Error)
return
}
message := "Item Deleted Successfully"
if result.RowsAffected > 1 {
message = "All Items with the Name " + reference + " have been delete"
}
fmt.Println(message)
}
func CheckRecordsExistance(db *gorm.DB, ref string) []Todo {
var todos []Todo
db.Limit(-1).Table("Todos").Find(&todos)
return todos
}