diff --git a/AutoCat/ViewModels/HistoryViewModel.cs b/AutoCat/ViewModels/HistoryViewModel.cs index f9b8317..84c7978 100644 --- a/AutoCat/ViewModels/HistoryViewModel.cs +++ b/AutoCat/ViewModels/HistoryViewModel.cs @@ -1,12 +1,24 @@ using System.Threading.Tasks; using AutoCatCore.Model; +using AutoCatCore.Services.Storage; namespace AutoCat.ViewModels; public class HistoryViewModel: ViewModelBase { + #region Dependencies + + private readonly IStorageService _storageService; + + #endregion + + public HistoryViewModel(IStorageService storageService) + { + _storageService = storageService; + } + public async Task AddVehicle(Vehicle vehicle) { - + await _storageService.AddVehicle(vehicle); } } \ No newline at end of file diff --git a/AutoCatCore/Model/AutoCatDbContext.cs b/AutoCatCore/Model/AutoCatDbContext.cs index 9e0cb88..c4960c2 100644 --- a/AutoCatCore/Model/AutoCatDbContext.cs +++ b/AutoCatCore/Model/AutoCatDbContext.cs @@ -6,9 +6,18 @@ namespace AutoCatCore.Model { public DbSet Users { get; private set; } + public DbSet Vehicles { get; private set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("filename=autocat.db"); } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasOne(v => v.Brand); + + modelBuilder.Entity().HasNoKey().ToView("VehicleBrandView"); + } } } diff --git a/AutoCatCore/Model/Osago.cs b/AutoCatCore/Model/Osago.cs index a025a36..cd8f541 100644 --- a/AutoCatCore/Model/Osago.cs +++ b/AutoCatCore/Model/Osago.cs @@ -1,9 +1,12 @@ -namespace AutoCatCore.Model +using System.ComponentModel.DataAnnotations; + +namespace AutoCatCore.Model { public class Osago { - public double? Date { get; set; } + [Key] public string? Number { get; set; } + public double? Date { get; set; } public string? Vin { get; set; } public string? PlateNumber { get; set; } public string? Name { get; set; } diff --git a/AutoCatCore/Services/Storage/IStorageService.cs b/AutoCatCore/Services/Storage/IStorageService.cs index 6baca4b..6e82269 100644 --- a/AutoCatCore/Services/Storage/IStorageService.cs +++ b/AutoCatCore/Services/Storage/IStorageService.cs @@ -9,4 +9,6 @@ public interface IStorageService public string? AuthToken { get; } public Task SetUser(User user); + + public Task AddVehicle(Vehicle vehicle); } \ No newline at end of file diff --git a/AutoCatCore/Services/Storage/StorageService.cs b/AutoCatCore/Services/Storage/StorageService.cs index 35f8985..b35d4b7 100644 --- a/AutoCatCore/Services/Storage/StorageService.cs +++ b/AutoCatCore/Services/Storage/StorageService.cs @@ -20,6 +20,12 @@ public class StorageService: IStorageService public string? AuthToken => _dbContext.Users.Any() ? _dbContext.Users.First().Token : null; + public StorageService() + { + _dbContext = new AutoCatDbContext(); + _dbContext.Database.EnsureCreated(); + } + public async Task SetUser(User user) { await _dbContext.Users.ExecuteDeleteAsync(); @@ -27,9 +33,9 @@ public class StorageService: IStorageService await _dbContext.SaveChangesAsync(); } - public StorageService() + public async Task AddVehicle(Vehicle vehicle) { - _dbContext = new AutoCatDbContext(); - _dbContext.Database.EnsureCreated(); + await _dbContext.Vehicles.AddAsync(vehicle); + await _dbContext.SaveChangesAsync(); } } \ No newline at end of file