Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Created August 24, 2017 18:19
Show Gist options
  • Save dontpaniclabsgists/de7e079ec0dac8135663e841cccb3115 to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/de7e079ec0dac8135663e841cccb3115 to your computer and use it in GitHub Desktop.
cosmos3_6
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CosmosTest
{
class Program
{
static void Main(string[] args)
{
var endpoint = args[0];
var key = args[1];
var databaseId = args[2];
Task.Run(async () =>
{
await Run(endpoint, key, databaseId);
}).GetAwaiter().GetResult();
}
static async Task Run(string endpoint, string key, string databaseId)
{
Console.WriteLine(endpoint);
Console.WriteLine(key);
Console.WriteLine(databaseId);
var exit = false;
Note added = null;
var customerId = "test";
while (!exit)
{
try
{
WriteMenu();
var number = int.Parse(Console.ReadLine());
var accessor = new NotesAccessor(endpoint, key, databaseId);
switch (number)
{
case 1:
Console.WriteLine("COUNT: " + accessor.Count());
break;
case 2:
{
Console.WriteLine("READ ALL: " + JsonConvert.SerializeObject(await accessor.All()));
}
break;
case 3:
{
if (added != null)
{
Console.WriteLine("READ ONE: " + JsonConvert.SerializeObject(await accessor.ReadOne(added.Id, added.CustomerId)));
}
else
{
Console.WriteLine("add item first");
}
}
break;
case 4:
{
added = new Note()
{
Id = Guid.NewGuid().ToString(),
CustomerId = customerId,
Text = "Something",
CreatedAt = DateTime.Now,
};
Console.WriteLine("CREATE: " + JsonConvert.SerializeObject(added));
await accessor.Create(added);
}
break;
case 5:
{
if (added != null)
{
added.Text = "updated";
Console.WriteLine("UPDATE: " + JsonConvert.SerializeObject(added));
await accessor.Update(added);
}
else
{
Console.WriteLine("add item first");
}
}
break;
case 6:
{
if (added != null)
{
Console.WriteLine("DELETE: " + added.Id);
await accessor.Delete(added.Id, added.CustomerId);
}
else
{
Console.WriteLine("add item first");
}
}
break;
case 7:
{
string token = null;
while (true)
{
var result = await accessor.Page(token, 2, customerId);
if (result.Notes.Length > 0)
{
var text = JsonConvert.SerializeObject(result.Notes);
token = result.Token;
Console.WriteLine("PAGE: " + text);
}
if (!result.HasMore)
{
break;
}
}
}
break;
case 8:
Console.WriteLine("COUNT: " + accessor.CountSql());
break;
case 9:
{
Console.WriteLine("READ ALL SQL: " + JsonConvert.SerializeObject(await accessor.AllSql()));
}
break;
case 99:
exit = true;
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
exit = true;
}
}
}
static void WriteMenu()
{
Console.WriteLine("");
Console.WriteLine("###### Menu ######");
Console.WriteLine(" 1. Count");
Console.WriteLine(" 2. Read All");
Console.WriteLine(" 3. Read One");
Console.WriteLine(" 4. Create");
Console.WriteLine(" 5. Update");
Console.WriteLine(" 6. Delete");
Console.WriteLine(" 7. Page");
Console.WriteLine(" 8. Count - SQL");
Console.WriteLine(" 9. Read All - SQL");
Console.WriteLine("99. Exit");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment