Commit e1994cb2 authored by Kirill's avatar Kirill

Фетчатся и выводятся в лог TourPreview объекты

parent 3a4c4875
...@@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations.Schema; ...@@ -4,6 +4,7 @@ using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common; using System.Data.Common;
using System.Data.Entity; using System.Data.Entity;
using System.Data.Entity.Core.Common; using System.Data.Entity.Core.Common;
using System.Data.Entity.Migrations;
using System.Data.SQLite; using System.Data.SQLite;
using System.Data.SQLite.EF6; using System.Data.SQLite.EF6;
using System.Linq; using System.Linq;
...@@ -68,9 +69,8 @@ namespace TourDataManager { ...@@ -68,9 +69,8 @@ namespace TourDataManager {
} }
public void InsertEstates(Estate[] estates){ public void InsertEstates(Estate[] estates){
//context.Estates.AddRange(estates);
foreach (var estate in estates){ foreach (var estate in estates){
context.Estates.Add(estate); context.Estates.AddOrUpdate(estate);
} }
foreach (var contextEstate in context.Estates){ foreach (var contextEstate in context.Estates){
Debug.Log(contextEstate.Id); Debug.Log(contextEstate.Id);
......
...@@ -9,7 +9,6 @@ namespace TourDataManager.Entities { ...@@ -9,7 +9,6 @@ namespace TourDataManager.Entities {
public string Screen{ get; set; } public string Screen{ get; set; }
public int EstateId{ get; set; }
public string Created{ get; set; } public string Created{ get; set; }
...@@ -20,5 +19,9 @@ namespace TourDataManager.Entities { ...@@ -20,5 +19,9 @@ namespace TourDataManager.Entities {
/// Состояние тура /// Состояние тура
public int State{ get; set; } public int State{ get; set; }
public long EstateId{ get; set; }
public override string ToString(){ return $"Tour : {Id} | {Title} | {EstateId}"; }
} }
} }
\ No newline at end of file
...@@ -12,7 +12,7 @@ namespace TourDataManager { ...@@ -12,7 +12,7 @@ namespace TourDataManager {
[Inject] public HttpClient HttpClient{ get; set; } [Inject] public HttpClient HttpClient{ get; set; }
[Inject] public Db Database{ get; set; } [Inject] public Db Database{ get; set; }
public Task<Estate[]> FetchEstatesFromDb (){ public Task<Estate[]> FetchEstatesFromDbAsync(){
return Database.GetEstatesAsync(); return Database.GetEstatesAsync();
} }
...@@ -21,7 +21,7 @@ namespace TourDataManager { ...@@ -21,7 +21,7 @@ namespace TourDataManager {
/// <exception cref="BigantoErrorException">Если вместо массива эстейтов с сервера получены ошибки</exception> /// <exception cref="BigantoErrorException">Если вместо массива эстейтов с сервера получены ошибки</exception>
/// <exception cref="JsonException">Если вместо массива эстейтов или ошибки пришло нечто иное</exception> /// <exception cref="JsonException">Если вместо массива эстейтов или ошибки пришло нечто иное</exception>
/// </summary> /// </summary>
public async Task<Estate[]> FetchEstatesFromServer(){ public async Task<Estate[]> FetchEstatesFromServerAsync(){
var response = await HttpClient.GetAsync("https://biganto.com/api-novus/estates.getList?client=desktopplayer&client_version=3.0&v=2.0"); var response = await HttpClient.GetAsync("https://biganto.com/api-novus/estates.getList?client=desktopplayer&client_version=3.0&v=2.0");
// throw HttpRequestException // throw HttpRequestException
......
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Ninject;
using TourDataManager.Entities;
namespace TourDataManager {
public class FetchTourPreviewsUseCase {
[Inject] public HttpClient HttpClient{ get; set; }
public async Task<Tour[]> FetchTourFromServerAsync(long estateId){
var response = await HttpClient.GetAsync(
$"https://biganto.com/api-novus/tours.getList?client=desktopplayer&client_version=3.0&v=2.0&estate_id={estateId}");
// throw HttpRequestException
var content = await response.Content.ReadAsStringAsync();
// TODO Это в буквальнос смысле копипаста с FetchEstatesUseCase.FetchEstatesFromServerAsync
ErrorContext error = null;
var errorHandlerSettings = new JsonSerializerSettings{
Error = (sender,args) => {
error = args.ErrorContext;
args.ErrorContext.Handled = true;
}
};
var tours = JsonConvert.DeserializeObject<Tour[]>(content,errorHandlerSettings);
if (error == null){
// Т.к из json нам не приходит estateId, а мы и так по контексту его знаем, то вбиваем так.
// Теперь и база будет знать.
foreach (var tour in tours){tour.EstateId = estateId;}
return tours;
}
error = null;
var exceptionList = JsonConvert.DeserializeObject<BigantoErrorList>(content,errorHandlerSettings);
if (error == null) throw new BigantoErrorException(exceptionList);
throw new JsonException("Something goes wrong",error.Error);
}
}
}
\ No newline at end of file
...@@ -15,7 +15,8 @@ namespace TourDataManager { ...@@ -15,7 +15,8 @@ namespace TourDataManager {
public class TourDataManager { public class TourDataManager {
public FetchEstatesUseCase fetch{ get; set; } public FetchEstatesUseCase estatesfetch{ get; set; }
public FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; }
public Db database{ get; set; } public Db database{ get; set; }
private string persistentPath; private string persistentPath;
...@@ -27,7 +28,8 @@ namespace TourDataManager { ...@@ -27,7 +28,8 @@ namespace TourDataManager {
Container = new StandardKernel(new MyModule(persistentPath)); Container = new StandardKernel(new MyModule(persistentPath));
fetch = Container.Get<FetchEstatesUseCase>(); estatesfetch = Container.Get<FetchEstatesUseCase>();
tourpreviewsfetch = Container.Get<FetchTourPreviewsUseCase>();
database = Container.Get<Db>(); database = Container.Get<Db>();
} }
...@@ -41,14 +43,14 @@ namespace TourDataManager { ...@@ -41,14 +43,14 @@ namespace TourDataManager {
} }
public async Task<Estate[]> DownloadEstates(){ public async Task<Estate[]> DownloadEstates(){
try{ var estates = await estatesfetch.FetchEstatesFromServerAsync();
var estates = await fetch.FetchEstatesFromServer(); database.InsertEstates(estates);
database.InsertEstates(estates); return estates;
return estates; }
} catch (Exception){
Debug.Log("Не удалось загрузить эстейты"); public async Task<Tour[]> DownloadTourPreviews(long estateId){
return null; var tours = await tourpreviewsfetch.FetchTourFromServerAsync(estateId);
} return tours;
} }
} }
......
...@@ -111,6 +111,7 @@ ...@@ -111,6 +111,7 @@
<Compile Include="Entities\Estate.cs" /> <Compile Include="Entities\Estate.cs" />
<Compile Include="Entities\Tour.cs" /> <Compile Include="Entities\Tour.cs" />
<Compile Include="FetchEstatesUseCase.cs" /> <Compile Include="FetchEstatesUseCase.cs" />
<Compile Include="FetchTourPreviewsUseCase.cs" />
<Compile Include="IAuthenticator.cs" /> <Compile Include="IAuthenticator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TourDataManager.cs" /> <Compile Include="TourDataManager.cs" />
......
using System; using System;
using System.Threading.Tasks;
using TourDataManager; using TourDataManager;
using TourDataManager.Entities;
namespace TourDataManagerConsoleApplication { namespace TourDataManagerConsoleApplication {
internal class Program { internal class Program {
...@@ -12,17 +14,29 @@ namespace TourDataManagerConsoleApplication { ...@@ -12,17 +14,29 @@ namespace TourDataManagerConsoleApplication {
public static void Main(string[] args){ public static void Main(string[] args){
var tourDataManager = new TourDataManager.TourDataManager(PersistentPath); var tourDataManager = new TourDataManager.TourDataManager(PersistentPath);
tourDataManager.Login(defaultLogin, defaultPassword); //tourDataManager.Login(defaultLogin, defaultPassword);
AsyncMethod(tourDataManager);
Console.Read();
}
//new Db(); public static async void AsyncMethod(TourDataManager.TourDataManager plugin){
try{
tourDataManager.DownloadEstates().ContinueWith(task => { var estates = await plugin.DownloadEstates();
foreach (var estate in task.Result){ foreach (var estate in estates){
Debug.Log(estate); Debug.Log(estate);
var tours = await plugin.DownloadTourPreviews(estate.Id);
foreach (var tour in tours){
Debug.Log(tour);
}
} }
}); } catch (Exception e){
Console.Read(); Debug.Log(e);
}
} }
} }
} }
\ No newline at end of file
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