-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (79 loc) · 2.81 KB
/
Program.cs
File metadata and controls
98 lines (79 loc) · 2.81 KB
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
using System;
using DocoptNet;
using Newtonsoft.Json;
using Raven.Client;
namespace RavenShowOff
{
internal partial class Program
{
private static IDocumentSession Session;
private const string usage = @"Raven Show Off!
Usage:
RavenShowOff.exe save
RavenShowOff.exe load <id>
RavenShowOff.exe list
RavenShowOff.exe add_hometown <id> <hometown>
RavenShowOff.exe obliterate_bobs
Options:
save will save Bob M. Smiley
load <id> will load whichever instance of Bob you want
list will list all the Bob'hometown
";
const string DatabaseName = "CineSample";
private static void Main(string[] args)
{
var arguments = new Docopt().Apply(usage, args, exit: true);
Setup();
if (arguments["save"].IsTrue)
SaveBob();
if (arguments["load"].IsTrue)
LoadBob(arguments["<id>"].ToString());
if (arguments["list"].IsTrue)
ListBobs();
if (arguments["add_hometown"].IsTrue)
SaveBobsHometown(arguments["<id>"].ToString(), arguments["<hometown>"].ToString());
}
private static void SaveBobsHometown(string idOfBob, string hometown)
{
var bob = Session.Load<Person>($"People/{idOfBob}");
var bobDoesNotExistForId = bob == null;
if (bobDoesNotExistForId)
Console.WriteLine($"No Bob found for {idOfBob}");
else
{
bob.Hometown = hometown;
Session.SaveChanges();
}
}
private static void LoadBob(string idOfBob)
{
var bob = Session.Load<Person>($"People/{idOfBob}");
var bobWithThisIdWasFound = bob == null;
if (bobWithThisIdWasFound)
Console.WriteLine($"No Bob found for {idOfBob}");
else
Console.WriteLine(JsonConvert.SerializeObject(bob));
}
private static void ListBobs()
{
var allTheBobs = Session.Query<Person>();
foreach (var bob in allTheBobs)
{
Console.WriteLine(JsonConvert.SerializeObject(bob));
}
}
private static void SaveBob()
{
var person = new Person("Bob", "Smiley") {MiddleInitial = "N"};
Session.Store(person);
Console.WriteLine($"Got Id:{person.Id} for Bob.");
person.PhoneNumbers.Add(new Person.PhoneNumber(404, "123-4588"));
Session.SaveChanges();
}
private static void Setup()
{
DocumentStoreHolder.Store.DatabaseCommands.GlobalAdmin.EnsureDatabaseExists(DatabaseName);
Session = DocumentStoreHolder.Store.OpenSession(DatabaseName);
}
}
}