-
Notifications
You must be signed in to change notification settings - Fork 17
/
Program.cs
132 lines (117 loc) · 4.43 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Divan;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
namespace CloudantAndCouchDB
{
class Program
{
static void CreateWord(string word, string translation, ICouchDatabase database)
{
if (database.GetDocument(word) == null)
{
database.CreateDocument("{\"_id\": \"" + word + "\", \"word\": \"" + word + "\", \"translation\": \"" + translation + "\"}");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Word added");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The word already exist");
Console.ResetColor();
}
}
static void GetWordTranslation(string word, ICouchDatabase database)
{
try
{
JObject ourWord = JObject.Parse(database.GetDocument(word).ToString());
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("{0} - {1}", word, (string)ourWord["translation"]);
Console.ResetColor();
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("This word don't exist in the dictionary");
Console.ResetColor();
}
}
static void ListAllLords(ICouchDatabase database)
{
try
{
var allWords = database.GetAllDocuments();
foreach (var item in allWords)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(item.Obj["word"] + " - " + item.Obj["translation"]);
Console.ResetColor();
}
}
catch (Exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The dictionary is empty");
Console.ResetColor();
}
}
static void Main()
{
//This is real username and password and must work with them
CouchServer server = new CouchServer("testuser.cloudant.com", 5984, "testuser", "testpassword");
ICouchDatabase database = server.GetDatabase("dictionary");
bool isWorking = true;
ICouchDocument doc = database.GetDocument("game");
while (isWorking)
{
Console.WriteLine("For add new word press : 1");
Console.WriteLine("For finding a translation for given word press 2");
Console.WriteLine("For list all words and their translations press 3");
Console.WriteLine("For exit press 4");
string command = Console.ReadLine();
switch (command)
{
case "1":
{
Console.WriteLine("Enter word");
string word = Console.ReadLine();
Console.WriteLine("Enter translation");
string translation = Console.ReadLine();
CreateWord(word, translation, database);
}
break;
case "2":
{
Console.WriteLine("Enter word");
string word = Console.ReadLine();
GetWordTranslation(word, database);
}
break;
case "3":
{
ListAllLords(database);
}
break;
case "4":
{
isWorking = false;
}
break;
default:
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unknown Command");
Console.ResetColor();
}
break;
}
}
}
}
}