練習資料庫紀錄完成
This commit is contained in:
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,23 +1,47 @@
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfiler;
|
||||
using CoreProfilerExample.Common;
|
||||
using CoreProfilerExample.Common.Extensions;
|
||||
using CoreProfilerExample.Repository.Interfaces;
|
||||
using CoreProfilerExample.Repository.Models.DataModels;
|
||||
using Dapper;
|
||||
using System.Reflection;
|
||||
using static CoreProfilerExample.Common.Constants.WeatherConstant;
|
||||
|
||||
namespace CoreProfilerExample.Repository.Implements
|
||||
{
|
||||
public class WeatherForecastRepository : IWeatherForecastRepository
|
||||
public class WeatherForecastRepository(IDatabaseOption option) : IWeatherForecastRepository
|
||||
{
|
||||
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
public async Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
|
||||
{
|
||||
var type = MethodBase.GetCurrentMethod()?.DeclaringType;
|
||||
|
||||
var name = MethodBase.GetCurrentMethod()?.Name;
|
||||
|
||||
using (MethodBase.GetCurrentMethod()?.ProfilingStep())
|
||||
{
|
||||
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
var forecasts = days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)).ToString(),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)]
|
||||
}) : []);
|
||||
}) : [];
|
||||
|
||||
var sql = @"INSERT INTO [WeatherForecast] (
|
||||
[Date],
|
||||
[TemperatureC],
|
||||
[Summary]
|
||||
)
|
||||
VALUES (
|
||||
@Date,
|
||||
@TemperatureC,
|
||||
@Summary
|
||||
)";
|
||||
|
||||
using var connection = option.SqliteConnection;
|
||||
|
||||
await connection.ExecuteAsync(sql, forecasts);
|
||||
|
||||
return forecasts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{
|
||||
public class WeatherForecastDataModel
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public string Date { get; set; } = null!;
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user