36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using TabletTester2025.Data;
|
|||
|
|
using TabletTester2025.Helpers;
|
|||
|
|
using TabletTester2025.Models;
|
|||
|
|
|
|||
|
|
namespace TabletTester2025.Services
|
|||
|
|
{
|
|||
|
|
public class DatabaseService
|
|||
|
|
{
|
|||
|
|
private readonly string _connectionString;
|
|||
|
|
public DatabaseService(string connectionString)
|
|||
|
|
{
|
|||
|
|
_connectionString = connectionString;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void InsertBatch(TestBatch batch)
|
|||
|
|
{
|
|||
|
|
using var db = new AppDbContext(_connectionString);
|
|||
|
|
db.TestBatches.Add(batch);
|
|||
|
|
db.SaveChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<TestBatch> GetBatches(int? stationId = null, int limit = 100)
|
|||
|
|
{
|
|||
|
|
using var db = new AppDbContext(_connectionString);
|
|||
|
|
var query = db.TestBatches.AsQueryable();
|
|||
|
|
if (stationId.HasValue)
|
|||
|
|
query = query.Where(b => b.StationId == stationId.Value);
|
|||
|
|
query = query.OrderByDescending(b => b.TestTime).Take(limit);
|
|||
|
|
return query.ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|