Commit 419ec5b0 authored by Kirill's avatar Kirill

Немного элементов управления через консоль и рефакторинга

parent 69743608
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Ninject;
namespace TourDataManager {
......@@ -9,19 +10,22 @@ namespace TourDataManager {
[Inject] public HttpClient HttpClient{ get; set; }
public async void Login(string email, string password, Action<bool, string> continuation = null){
public (bool, string) Login(string email, string password){
return LoginAsync(email, password).Result;
}
public async Task<(bool,string)> LoginAsync(string email, string password){
var x = await HttpClient.PostAsync(authUri,
new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", password)
}));
var content = await x.Content.ReadAsStringAsync();
continuation?.Invoke(x.IsSuccessStatusCode,content);
return (x.IsSuccessStatusCode, content);
}
public void Logout(){
throw new NotImplementedException();
}
}
}
\ No newline at end of file
......@@ -12,5 +12,11 @@ namespace TourDataManager {
Console.WriteLine(msg);
Console.ResetColor();
}
public static char ReadKey(){
var ch = Console.ReadKey().KeyChar;
Console.WriteLine();
return ch;
}
}
}
\ No newline at end of file
using System;
using System.Threading.Tasks;
namespace TourDataManager {
public interface IAuthenticator {
void Login(string email, string password, Action<bool,string> continuation = null);
(bool,string) Login(string email, string password);
Task<(bool,string)> LoginAsync(string email, string password);
void Logout();
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@ namespace TourDataManager {
/// <summary>
/// Фасад для всего плагина
/// </summary>
public class TourDataManager {
public class TourDataManager : IAuthenticator{
public static volatile string PersistentPath;
......@@ -21,9 +21,9 @@ namespace TourDataManager {
return Path.Combine(PersistentPath, relative);
}
public FetchEstatesUseCase estatesfetch{ get; set; }
public FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; }
public Db database{ get; set; }
private FetchEstatesUseCase estatesfetch{ get; set; }
private FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; }
private Db database{ get; set; }
private IKernel Container;
......@@ -38,22 +38,27 @@ namespace TourDataManager {
database = Container.Get<Db>();
}
public void Login(string email, string password){
Container.Get<IAuthenticator>().Login(email,password, (b, s) => {
public async Task<(bool, string)> LoginAsync(string email, string password){
var result = await Container.Get<IAuthenticator>().LoginAsync(email, password);
if(result.Item1) SaveCookie();
return result;
}
private void SaveCookie(){
var cookiestor = Container.Get<CookieStorage>();
Debug.Log($"Authorization : {s}");
Debug.Log($"Cookie count in storage : {cookiestor.Get().Count}");
Debug.Log($"Cookie saved. Current count : {cookiestor.Get().Count}");
cookiestor.Save();
});
}
public async Task<Estate[]> DownloadEstates(){
public void Logout(){ throw new NotImplementedException(); }
public async Task<Estate[]> FetchEstatesAsync(){
var estates = await estatesfetch.FetchEstatesFromServerAsync();
database.InsertEstates(estates);
return estates;
}
public async Task<Tour[]> DownloadTourPreviews(long estateId){
public async Task<Tour[]> FetchTourPreviewsAsync(long estateId){
var tours = await tourpreviewsfetch.FetchTourFromServerAsync(estateId);
database.InsertTours(tours);
return tours;
......@@ -62,6 +67,13 @@ namespace TourDataManager {
public ContentLoadingTask DownloadTourContent(long tourId){
return Container.Get<FetchTourContentUseCase>().FetchTourContentFromServerAsync(tourId);
}
public (bool, string) Login(string email, string password){
return LoginAsync(email, password).Result;
}
}
public class MyModule : NinjectModule {
......
......@@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Ninject.Activation.Strategies;
using TourDataManager;
using TourDataManager.Entities;
......@@ -13,22 +14,74 @@ namespace TourDataManagerConsoleApplication {
const string defaultLogin = "demo@biganto.ru";
const string defaultPassword = "demo";
public static TourDataManager.TourDataManager tourDataManager;
public static void Main(string[] args){
var tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
MainWindow();
//tourDataManager.Login(defaultLogin, defaultPassword);
AsyncMethod(tourDataManager);
//AsyncMethod(tourDataManager);
Console.Read();
}
public static void MainWindow(){
Debug.Log("Commands : \n0 - Auth\n1 - Show estates list\n2 - exit");
var ch = Debug.ReadKey();
switch (ch){
case '0' :
Debug.Log("Login : ");
var login = Console.ReadLine();
Debug.Log("Password : ");
var pass = Console.ReadLine();
var result = tourDataManager.Login(login,pass);
Debug.Log($"Auth result : {result.Item1} {result.Item2}");
MainWindow();
break;
case '1' :
var estates = tourDataManager.FetchEstatesAsync().Result;
foreach (var estate in estates){
Debug.Log(estate);
}
EstateWindow();
break;
case '2' : Environment.Exit(0); break;
}
}
public static void EstateWindow(){
Debug.Log("Commands : \n0 - Fetch tours of estate\n1 - back\n2 - exit");
var ch = Debug.ReadKey();
switch (ch){
case '0' :
Debug.Log("Estate id : ");
var id = Console.ReadLine();
var tours = tourDataManager.FetchTourPreviewsAsync(Int32.Parse(id)).Result;
foreach (var tour in tours){
Debug.Log(tour);
}
EstateWindow();
break;
case '1' : MainWindow(); break;
case '2' : Environment.Exit(0); break;
}
}
public static async void AsyncMethod(TourDataManager.TourDataManager plugin){
try{
var estates = await plugin.DownloadEstates();
var estates = await plugin.FetchEstatesAsync();
Tour[] tours = null;
foreach (var estate in estates){
Debug.Log(estate);
tours = await plugin.DownloadTourPreviews(estate.Id);
tours = await plugin.FetchTourPreviewsAsync(estate.Id);
foreach (var tour in tours){
Debug.Log(tour);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment