1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4
5namespace Couchbase.SDK3._0.Examples
6{
7    internal class Program
8    {
9        private static void Main(string[] args)
10        {
11            Task.Run(async () =>
12            {
13                try
14                {
15                    var cluster = new Cluster();
16                    var task = cluster.Initialize(
17                        new Configuration()
18                            .WithServers("couchbase://127.0.0.1")
19                            .WithBucket("default")
20                            .WithCredentials("Administrator", "password")
21                    );
22                    task.ConfigureAwait(false);
23                    task.Wait();
24
25                    var bucket = await cluster.Bucket("default");
26                    var collection = await bucket.DefaultCollection;
27
28                    await BasicCrud(collection);
29                    await BasicProjection(collection);
30                    await BasicDurability(collection);
31                    await BasicQuery(cluster);
32                }
33                catch (Exception e)
34                {
35                    Console.WriteLine(e);
36                }
37            });
38
39            Console.WriteLine("Hello World!");
40            Console.Read();
41        }
42
43        private static async Task BasicQuery(ICluster cluster)
44        {
45            var statement = "SELECT * FROM `default` WHERE type=$type";
46
47            var results = await cluster.Query<Person>(
48                statement,
49                parameters => parameters.Add("type", "person"),
50                options => options.Timeout(new TimeSpan(0, 0, 0, 75)));
51
52            foreach (var result in results) Console.WriteLine(result);
53        }
54
55        private static async Task BasicCrud(ICollection collection)
56        {
57            var id = "p01";
58
59            //upsert a new person
60            var result = await collection.Upsert(id, new Person
61            {
62                name = "Joan Deere",
63                age = 28,
64                animals = new List<string> {"kitty", "puppy"},
65                attributes = new Attributes
66                {
67                    dimensions = new Dimensions
68                    {
69                        height = 65,
70                        weight = 120
71                    },
72                    hair = "brown",
73                    hobbies = new List<Hobby>
74                    {
75                        new Hobby
76                        {
77                            name = "Curling",
78                            type = "winter",
79                            details = new Details
80                            {
81                                location = new Location
82                                {
83                                    @long = -121.886330,
84                                    lat = 37.338207
85                                }
86                            }
87                        }
88                    }
89                }
90            }, options => options.Expiration = new TimeSpan(1, 0, 0, 0));
91
92            var get = await collection.Get(id,
93                options => options.Timeout = new TimeSpan(0, 0, 0, 10));
94
95            var person = get.ContentAs<Person>();
96            Console.WriteLine(person.name);
97        }
98
99        private static async Task BasicDurability(ICollection collection)
100        {
101            var id = "p02";
102            var result = await collection.Insert(id, new Person
103            {
104                name = "Jon Henry",
105                age = 34
106            }, options =>
107            {
108                options.DurabilityLevel = DurabilityLevel.MajorityAndPersistActive;
109                options.Timeout = new TimeSpan(0, 0, 0, 30);
110            });
111
112            Console.WriteLine(result.Cas);
113        }
114
115        private static async Task BasicProjection(ICollection collection)
116        {
117            var id = "p01";
118
119            var result = await collection.Get(id,
120                options => options.Project("name", "age", "attributes.hair"));
121
122            var person = result.ContentAs<Person>();
123            Console.WriteLine("Age={person.age}, Name={person.name}, Hair{person.attributes.hair}");
124        }
125
126        public class Dimensions
127        {
128            public int height { get; set; }
129            public int weight { get; set; }
130        }
131
132        public class Location
133        {
134            public double lat { get; set; }
135            public double @long { get; set; }
136        }
137
138        public class Details
139        {
140            public Location location { get; set; }
141        }
142
143        public class Hobby
144        {
145            public string type { get; set; }
146            public string name { get; set; }
147            public Details details { get; set; }
148        }
149
150        public class Attributes
151        {
152            public string hair { get; set; }
153            public Dimensions dimensions { get; set; }
154            public List<Hobby> hobbies { get; set; }
155        }
156
157        public class Person
158        {
159            public string name { get; set; }
160            public int age { get; set; }
161            public List<string> animals { get; set; }
162            public Attributes attributes { get; set; }
163            public string type => "person";
164        }
165    }
166}