Commit 13c40374 authored by Kirill's avatar Kirill

Файлы добавляются в БД

parent f88a196a
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common; using System.Data.Common;
...@@ -49,6 +50,7 @@ namespace TourDataManager { ...@@ -49,6 +50,7 @@ namespace TourDataManager {
public DbSet<Estate> Estates{ get; set; } public DbSet<Estate> Estates{ get; set; }
public DbSet<Tour> Tours{ get; set; } public DbSet<Tour> Tours{ get; set; }
public DbSet<File> Files{ get; set; }
...@@ -71,14 +73,14 @@ namespace TourDataManager { ...@@ -71,14 +73,14 @@ namespace TourDataManager {
return context.Estates.ToArrayAsync(); return context.Estates.ToArrayAsync();
} }
public void InsertEstates(Estate[] estates){ public void InsertEstates(IEnumerable<Estate> estates){
foreach (var estate in estates){ foreach (var estate in estates){
context.Estates.AddOrUpdate(estate); context.Estates.AddOrUpdate(estate);
} }
context.SaveChanges(); context.SaveChanges();
} }
public void InsertTours(Tour[] tours){ public void InsertTours(IEnumerable<Tour> tours){
foreach (var tour in tours){ foreach (var tour in tours){
context.Tours.AddOrUpdate(tour); context.Tours.AddOrUpdate(tour);
} }
...@@ -86,6 +88,13 @@ namespace TourDataManager { ...@@ -86,6 +88,13 @@ namespace TourDataManager {
} }
public void InsertFiles(IEnumerable<File> files){
foreach (var file in files){
context.Files.AddOrUpdate(file);
}
context.SaveChanges();
}
public void Dispose(){ public void Dispose(){
connection?.Dispose(); connection?.Dispose();
} }
......
using SQLite.CodeFirst;
namespace TourDataManager.Entities { namespace TourDataManager.Entities {
public class File { public class File {
public long TourId{ get; set; } public long Id{ get; set; }
public string Url{ get; set; } [Unique] public string Url{ get; set; }
public int Size{ get; set; } public int Size{ get; set; }
public string LocalUrl{ get; set; } public string LocalUrl{ get; set; }
......
...@@ -20,6 +20,7 @@ namespace TourDataManager { ...@@ -20,6 +20,7 @@ namespace TourDataManager {
public class FetchTourContentUseCase : ITourFilesListFetcher{ public class FetchTourContentUseCase : ITourFilesListFetcher{
[Inject] public HttpClient HttpClient{ get; set; } [Inject] public HttpClient HttpClient{ get; set; }
[Inject] public Db Database{ get; set; }
public ContentLoadingTask FetchTourContentFromServerAsync(long tourId){ public ContentLoadingTask FetchTourContentFromServerAsync(long tourId){
Debug.Log($"ContentLoadingTask queued for tour {tourId}"); Debug.Log($"ContentLoadingTask queued for tour {tourId}");
...@@ -28,6 +29,11 @@ namespace TourDataManager { ...@@ -28,6 +29,11 @@ namespace TourDataManager {
return contentLoadingTask; return contentLoadingTask;
} }
/// <summary>
/// Получить список файлов
/// </summary>
/// <param name="tourId"></param>
/// <returns></returns>
public async Task<File[]> FetchFilesAsync(long tourId){ public async Task<File[]> FetchFilesAsync(long tourId){
var response = await HttpClient.GetAsync( var response = await HttpClient.GetAsync(
$"https://biganto.com/api-novus/tours.getFiles?client=desktopplayer&client_version=3.0&v=2.0&id={tourId}"); $"https://biganto.com/api-novus/tours.getFiles?client=desktopplayer&client_version=3.0&v=2.0&id={tourId}");
...@@ -39,20 +45,20 @@ namespace TourDataManager { ...@@ -39,20 +45,20 @@ namespace TourDataManager {
var regex = new Regex(@"\/assets.+?(?=\?|$)"); var regex = new Regex(@"\/assets.+?(?=\?|$)");
// TODO Что если не сматчится?
foreach (var file in files){ foreach (var file in files){
file.TourId = tourId; // Id AutoIncrement
file.LocalUrl = TourDataManager.GetPathInPersistent(regex.Match(file.Url).Value); var val = regex.Match(file.Url).Value;
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(val));
file.LocalUrl = TourDataManager.GetPathInPersistent(val);
} }
Database.InsertFiles(files);
return files; return files;
} }
} }
public class ContentLoadingTask { public class ContentLoadingTask {
private string PersistentPath;
private ITourFilesListFetcher filesFetcher; private ITourFilesListFetcher filesFetcher;
private long tourId; private long tourId;
...@@ -63,9 +69,9 @@ namespace TourDataManager { ...@@ -63,9 +69,9 @@ namespace TourDataManager {
public async Task Run(){ public async Task Run(){
Debug.Log($"ContentLoadingTask.Run(){{ tourId = {tourId} }}");
var files = await filesFetcher.FetchFilesAsync(tourId); var files = await filesFetcher.FetchFilesAsync(tourId);
// add to db
var webClient = new WebClient(); var webClient = new WebClient();
......
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