using System.Collections.ObjectModel; using System.Collections.Specialized; using AutoCatCore.Model; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; namespace AutoCatCore.Services.Storage; public class StorageService: IStorageService { private readonly AutoCatDbContext _dbContext; public bool IsLoggedIn { get { if (_dbContext.Users.Any()) return _dbContext.Users.First().Token != null; else return false; } } 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(); await _dbContext.Users.AddAsync(user); await _dbContext.SaveChangesAsync(); } public async Task AddVehicle(Vehicle vehicle) { await _dbContext.Vehicles.AddAsync(vehicle); await _dbContext.SaveChangesAsync(); } public ObservableCollection GetVehicles() { _dbContext.Vehicles.Load(); return _dbContext.Vehicles.Local.ToObservableCollection(); } }