This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
92 lines (89 loc) · 3.61 KB
/
Program.cs
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
using System;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var collection = database.GetCollection<TodoItem>("todo");
ITodoListDao todoListDao = new MongoDriverDao(collection);
readInput(todoListDao);
}
static void readInput(ITodoListDao todoListDao)
{
Console.WriteLine("Please type a command:");
string currentCommand;
while ((currentCommand = Console.ReadLine()) != null)
{
string[] args = currentCommand.Split();
try
{
switch (args[0])
{
case "create":
if (args.Length != 3)
{
Console.WriteLine("Usage: create <contents> <priority>");
}
else
{
var id = todoListDao.CreateNewItem(args[1], Convert.ToInt32(args[2].Trim()));
Console.WriteLine("Created an item with the following id: " + id);
}
break;
case "read":
if (args.Length != 1)
{
Console.WriteLine("Usage: read");
}
else
{
var allItems = todoListDao.ListAllItems();
foreach (var item in allItems)
{
Console.WriteLine(item);
}
}
break;
case "update":
if (args.Length != 4)
{
Console.WriteLine("Usage: update <id> <contents> <priority>");
}
else
{
todoListDao.UpdateItemContent(ObjectId.Parse(args[1]), args[2]);
todoListDao.UpdateItemPriority(ObjectId.Parse(args[1]), Convert.ToInt32(args[3]));
Console.WriteLine("Update successful!");
}
break;
case "delete":
if (args.Length != 2)
{
Console.WriteLine("Usage: delete <id>");
}
else
{
todoListDao.RemoveItem(ObjectId.Parse(args[1]));
Console.WriteLine("Remove successful!");
}
break;
default:
Console.WriteLine("Try one of create, read, update or delete.");
break;
}
}
catch (MongoConnectionException)
{
Console.WriteLine("No connection to the database.");
}
}
}
}
}