-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (80 loc) · 2.11 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"gopkg.in/mgo.v2/bson"
"strings"
"os"
"github.com/maxwellhealth/bongo"
)
//TODO : a Strucutre Representation of TODO List
type TODO struct {
bongo.DocumentBase `bson:",inline"`
Task string
IsDone bool
}
func main() {
config := &bongo.Config{
ConnectionString: "localhost:27017",
Database: "TODOGO",
}
connection, err := bongo.Connect(config)
if err != nil {
panic(err)
}
fmt.Println("Connected To DB")
todoSlice := []*TODO{&TODO{Task: "Task 1", IsDone: false}, &TODO{Task: "Task 2", IsDone: false}}
AddTODO(connection, todoSlice...)
// DeleteingleTODO(connection, "58ed6b283464f927d49388ae")
// FindAllToDos(connection)
fmt.Println(strings.Repeat("=", 50))
fmt.Println("What Do you want to do?\n", "1 - Get All Elements\n", "2- Delete Element\n", "3- Add new TODO")
var input int
fmt.Scan(&input)
switch input {
case 1:
FindAllToDos(connection)
case 2:
fmt.Println("Enter The ID of TODO")
var id string
fmt.Scan(&id)
DeleteingleTODO(connection, id)
case 3:
fmt.Println("Enter Task name")
var inputString string
fmt.Scanln(&inputString)
fmt.Println(inputString)
if inputString == "" {
os.Exit(0)
}
AddTODO(connection, &TODO{Task: inputString, IsDone: false})
default:
fmt.Println("Wrong Number")
}
}
// AddTODO : to Add Data To Collection
func AddTODO(connection *bongo.Connection, todos ...*TODO) {
for _, todo := range todos {
err := connection.Collection("TODOS").Save(todo)
if err != nil {
panic(err)
}
}
fmt.Println("Added Successfully ")
}
// FindAllToDos : To Get All Rows in The Collection
func FindAllToDos(connection *bongo.Connection) {
res := connection.Collection("TODOS").Find(bson.M{})
some := new(TODO)
for i := 0; res.Next(some); i++ {
fmt.Println(i+1, some.Id, some.Task)
}
fmt.Println("Finish")
}
// DeleteingleTODO : To Delete Single Element By ID
func DeleteingleTODO(connection *bongo.Connection, id string) {
err := connection.Collection("TODOS").DeleteOne(bson.M{"_id": bson.ObjectIdHex(id)})
if err != nil {
panic(err)
}
fmt.Println("TODO Deleted Successfully !!!")
}