Commit 419ec5b0 authored by Kirill's avatar Kirill

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

parent 69743608
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Ninject; using Ninject;
namespace TourDataManager { namespace TourDataManager {
...@@ -9,19 +10,22 @@ namespace TourDataManager { ...@@ -9,19 +10,22 @@ namespace TourDataManager {
[Inject] public HttpClient HttpClient{ get; set; } [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, var x = await HttpClient.PostAsync(authUri,
new FormUrlEncodedContent(new[]{ new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("email", email), new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", password) new KeyValuePair<string, string>("password", password)
})); }));
var content = await x.Content.ReadAsStringAsync(); var content = await x.Content.ReadAsStringAsync();
return (x.IsSuccessStatusCode, content);
continuation?.Invoke(x.IsSuccessStatusCode,content);
} }
public void Logout(){ public void Logout(){
throw new NotImplementedException();
} }
} }
} }
\ No newline at end of file
...@@ -12,5 +12,11 @@ namespace TourDataManager { ...@@ -12,5 +12,11 @@ namespace TourDataManager {
Console.WriteLine(msg); Console.WriteLine(msg);
Console.ResetColor(); 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;
using System.Threading.Tasks;
namespace TourDataManager { namespace TourDataManager {
public interface IAuthenticator { 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(); void Logout();
} }
} }
\ No newline at end of file
...@@ -13,7 +13,7 @@ namespace TourDataManager { ...@@ -13,7 +13,7 @@ namespace TourDataManager {
/// <summary> /// <summary>
/// Фасад для всего плагина /// Фасад для всего плагина
/// </summary> /// </summary>
public class TourDataManager { public class TourDataManager : IAuthenticator{
public static volatile string PersistentPath; public static volatile string PersistentPath;
...@@ -21,9 +21,9 @@ namespace TourDataManager { ...@@ -21,9 +21,9 @@ namespace TourDataManager {
return Path.Combine(PersistentPath, relative); return Path.Combine(PersistentPath, relative);
} }
public FetchEstatesUseCase estatesfetch{ get; set; } private FetchEstatesUseCase estatesfetch{ get; set; }
public FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; } private FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; }
public Db database{ get; set; } private Db database{ get; set; }
private IKernel Container; private IKernel Container;
...@@ -38,22 +38,27 @@ namespace TourDataManager { ...@@ -38,22 +38,27 @@ namespace TourDataManager {
database = Container.Get<Db>(); database = Container.Get<Db>();
} }
public void Login(string email, string password){ public async Task<(bool, string)> LoginAsync(string email, string password){
Container.Get<IAuthenticator>().Login(email,password, (b, s) => { var result = await Container.Get<IAuthenticator>().LoginAsync(email, password);
if(result.Item1) SaveCookie();
return result;
}
private void SaveCookie(){
var cookiestor = Container.Get<CookieStorage>(); var cookiestor = Container.Get<CookieStorage>();
Debug.Log($"Authorization : {s}"); Debug.Log($"Cookie saved. Current count : {cookiestor.Get().Count}");
Debug.Log($"Cookie count in storage : {cookiestor.Get().Count}");
cookiestor.Save(); cookiestor.Save();
});
} }
public async Task<Estate[]> DownloadEstates(){ public void Logout(){ throw new NotImplementedException(); }
public async Task<Estate[]> FetchEstatesAsync(){
var estates = await estatesfetch.FetchEstatesFromServerAsync(); var estates = await estatesfetch.FetchEstatesFromServerAsync();
database.InsertEstates(estates); database.InsertEstates(estates);
return estates; return estates;
} }
public async Task<Tour[]> DownloadTourPreviews(long estateId){ public async Task<Tour[]> FetchTourPreviewsAsync(long estateId){
var tours = await tourpreviewsfetch.FetchTourFromServerAsync(estateId); var tours = await tourpreviewsfetch.FetchTourFromServerAsync(estateId);
database.InsertTours(tours); database.InsertTours(tours);
return tours; return tours;
...@@ -62,6 +67,13 @@ namespace TourDataManager { ...@@ -62,6 +67,13 @@ namespace TourDataManager {
public ContentLoadingTask DownloadTourContent(long tourId){ public ContentLoadingTask DownloadTourContent(long tourId){
return Container.Get<FetchTourContentUseCase>().FetchTourContentFromServerAsync(tourId); return Container.Get<FetchTourContentUseCase>().FetchTourContentFromServerAsync(tourId);
} }
public (bool, string) Login(string email, string password){
return LoginAsync(email, password).Result;
}
} }
public class MyModule : NinjectModule { public class MyModule : NinjectModule {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ninject.Activation.Strategies;
using TourDataManager; using TourDataManager;
using TourDataManager.Entities; using TourDataManager.Entities;
...@@ -13,22 +14,74 @@ namespace TourDataManagerConsoleApplication { ...@@ -13,22 +14,74 @@ namespace TourDataManagerConsoleApplication {
const string defaultLogin = "demo@biganto.ru"; const string defaultLogin = "demo@biganto.ru";
const string defaultPassword = "demo"; const string defaultPassword = "demo";
public static TourDataManager.TourDataManager tourDataManager;
public static void Main(string[] args){ public static void Main(string[] args){
var tourDataManager = new TourDataManager.TourDataManager(PersistentPath); tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
MainWindow();
//tourDataManager.Login(defaultLogin, defaultPassword); //tourDataManager.Login(defaultLogin, defaultPassword);
AsyncMethod(tourDataManager); //AsyncMethod(tourDataManager);
Console.Read(); 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){ public static async void AsyncMethod(TourDataManager.TourDataManager plugin){
try{ try{
var estates = await plugin.DownloadEstates(); var estates = await plugin.FetchEstatesAsync();
Tour[] tours = null; Tour[] tours = null;
foreach (var estate in estates){ foreach (var estate in estates){
Debug.Log(estate); Debug.Log(estate);
tours = await plugin.DownloadTourPreviews(estate.Id); tours = await plugin.FetchTourPreviewsAsync(estate.Id);
foreach (var tour in tours){ foreach (var tour in tours){
Debug.Log(tour); 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