mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using CliFx.Demo.Models;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CliFx.Demo.Services
|
|
{
|
|
public class LibraryService
|
|
{
|
|
private string StorageFilePath => Path.Combine(Directory.GetCurrentDirectory(), "Data.json");
|
|
|
|
private void StoreLibrary(Library library)
|
|
{
|
|
var data = JsonConvert.SerializeObject(library);
|
|
File.WriteAllText(StorageFilePath, data);
|
|
}
|
|
|
|
public Library GetLibrary()
|
|
{
|
|
if (!File.Exists(StorageFilePath))
|
|
return Library.Empty;
|
|
|
|
var data = File.ReadAllText(StorageFilePath);
|
|
|
|
return JsonConvert.DeserializeObject<Library>(data);
|
|
}
|
|
|
|
public Book GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
|
|
|
public void AddBook(Book book)
|
|
{
|
|
var updatedLibrary = GetLibrary().WithBook(book);
|
|
StoreLibrary(updatedLibrary);
|
|
}
|
|
|
|
public void RemoveBook(Book book)
|
|
{
|
|
var updatedLibrary = GetLibrary().WithoutBook(book);
|
|
StoreLibrary(updatedLibrary);
|
|
}
|
|
}
|
|
} |