-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarModelService.cs
More file actions
39 lines (32 loc) · 1.06 KB
/
CarModelService.cs
File metadata and controls
39 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace CarServiceAPI.Services;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using CarServiceAPI.Models;
public class CarModelService : ICarModelService
{
private readonly List<CarModel> _carModels;
public CarModelService()
{
// Load the car models from the data.json file
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var jsonFilePath = Path.Combine(assemblyLocation, "data.json");
var json = File.ReadAllText(jsonFilePath);
_carModels = JsonConvert.DeserializeObject<List<CarModel>>(json);
}
public CarModel GetById(string id) => _carModels.FirstOrDefault(cm => cm.Id == id);
public void Add(CarModel carModel)
{
_carModels.Add(carModel);
}
public bool Delete(string id)
{
var carModel = GetById(id);
if (carModel == null) return false;
_carModels.Remove(carModel);
return true;
}
public List<CarModel> GetAll() => _carModels;
}