1
0

加入專案檔案。

This commit is contained in:
2024-03-25 16:13:35 +08:00
parent c16cecfc42
commit 739e92cee8
26 changed files with 459 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
namespace CoreProfilerExample.Common.Constants
{
public static class WeatherConstant
{
public readonly static string[] WeatherSummaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CoreProfilerExample.Common\CoreProfilerExample.Common.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
using CoreProfilerExample.Repository.Interfaces;
using CoreProfilerExample.Repository.Models.DataModels;
using static CoreProfilerExample.Common.Constants.WeatherConstant;
namespace CoreProfilerExample.Repository.Implements
{
public class WeatherForecastRepository : IWeatherForecastRepository
{
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days)
{
return Task.Run(() => days > 0 ? Enumerable.Range(1, days).Select(index => new WeatherForecastDataModel
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = WeatherSummaries[Random.Shared.Next(WeatherSummaries.Length)]
}) : []);
}
}
}

View File

@@ -0,0 +1,9 @@
using CoreProfilerExample.Repository.Models.DataModels;
namespace CoreProfilerExample.Repository.Interfaces
{
public interface IWeatherForecastRepository
{
public Task<IEnumerable<WeatherForecastDataModel>> GetAsync(int days);
}
}

View File

@@ -0,0 +1,11 @@
namespace CoreProfilerExample.Repository.Models.DataModels
{
public class WeatherForecastDataModel
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CoreProfilerExample.Repository\CoreProfilerExample.Repository.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,33 @@
using CoreProfilerExample.Repository.Models.DataModels;
using CoreProfilerExample.Service.Models.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Service.Extensions
{
public static class MapperExtension
{
public static async Task<GetWeatherForecastDto> ToDtoAsync(this Task<IEnumerable<WeatherForecastDataModel>> task)
{
var dataModels = await task;
return new GetWeatherForecastDto
{
WeatherForecasts = dataModels.Select(model => model.ToDto()),
};
}
public static GetWeatherForecastItemDto ToDto(this WeatherForecastDataModel dataModel)
{
return new GetWeatherForecastItemDto
{
Date = dataModel.Date,
Summary = dataModel.Summary,
TemperatureC = dataModel.TemperatureC,
};
}
}
}

View File

@@ -0,0 +1,21 @@
using CoreProfilerExample.Repository.Interfaces;
using CoreProfilerExample.Service.Extensions;
using CoreProfilerExample.Service.Interfaces;
using CoreProfilerExample.Service.Models.Dtos;
using CoreProfilerExample.Service.Models.ParameterDtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Service.Implements
{
public class WeatherService(IWeatherForecastRepository weatherForecast) : IWeatherService
{
public Task<GetWeatherForecastDto> GetWeatherForecastAsync(GetWeatherForecastParameterDto parameterDto)
{
return weatherForecast.GetAsync(parameterDto.ForecastDays).ToDtoAsync();
}
}
}

View File

@@ -0,0 +1,10 @@
using CoreProfilerExample.Service.Models.Dtos;
using CoreProfilerExample.Service.Models.ParameterDtos;
namespace CoreProfilerExample.Service.Interfaces
{
public interface IWeatherService
{
public Task<GetWeatherForecastDto> GetWeatherForecastAsync(GetWeatherForecastParameterDto parameterDto);
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Service.Models.Dtos
{
public class GetWeatherForecastDto
{
public IEnumerable<GetWeatherForecastItemDto> WeatherForecasts { get; set; } = [];
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Service.Models.Dtos
{
public class GetWeatherForecastItemDto
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreProfilerExample.Service.Models.ParameterDtos
{
public class GetWeatherForecastParameterDto
{
public int ForecastDays { get; set; }
}
}

43
CoreProfilerExample.sln Normal file
View File

@@ -0,0 +1,43 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample", "CoreProfilerExample\CoreProfilerExample.csproj", "{A7B95E40-1807-45E8-831C-88246897C9B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Repository", "CoreProfilerExample.Repository\CoreProfilerExample.Repository.csproj", "{F5114126-C965-4F36-9302-AC897C29D898}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Service", "CoreProfilerExample.Service\CoreProfilerExample.Service.csproj", "{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreProfilerExample.Common", "CoreProfilerExample.Common\CoreProfilerExample.Common.csproj", "{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A7B95E40-1807-45E8-831C-88246897C9B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7B95E40-1807-45E8-831C-88246897C9B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7B95E40-1807-45E8-831C-88246897C9B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7B95E40-1807-45E8-831C-88246897C9B5}.Release|Any CPU.Build.0 = Release|Any CPU
{F5114126-C965-4F36-9302-AC897C29D898}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5114126-C965-4F36-9302-AC897C29D898}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5114126-C965-4F36-9302-AC897C29D898}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5114126-C965-4F36-9302-AC897C29D898}.Release|Any CPU.Build.0 = Release|Any CPU
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69C081ED-D31A-4C66-9EC9-5CF46A16C8E8}.Release|Any CPU.Build.0 = Release|Any CPU
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDD6FCC3-454E-42CB-94D0-23A7EA57D039}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {626E928F-5EA0-4F58-BFD4-1A47EF4043B4}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,30 @@
using CoreProfilerExample.Infrastructure.Extensions;
using CoreProfilerExample.Models.Parameters;
using CoreProfilerExample.Models.ViewModels;
using CoreProfilerExample.Service.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace CoreProfilerExample.Controllers
{
[ApiController]
[Route("weather")]
public class WeatherController(IWeatherService weather) : ControllerBase
{
[HttpPost("forecast", Name = "GetWeatherForecast")]
[Produces("application/json")]
[ProducesResponseType(typeof(GetWeatherForecastItemViewModel), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ExceptionViewModel), (int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetWeatherForecastAsync(GetWeatherForecastParameter parameter)
{
try
{
return Ok(await weather.GetWeatherForecastAsync(parameter.ToDto()).ToViewModel());
}
catch (Exception ex)
{
return BadRequest(ex.ToViewModel());
}
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreProfilerExample.Service\CoreProfilerExample.Service.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,22 @@
using CoreProfilerExample.Repository.Implements;
using CoreProfilerExample.Repository.Interfaces;
using CoreProfilerExample.Service.Implements;
using CoreProfilerExample.Service.Interfaces;
namespace CoreProfilerExample.Infrastructure.Extensions
{
public static class DependencyInjectionExtension
{
public static IServiceCollection AddRepository(this IServiceCollection services)
{
services.AddScoped<IWeatherForecastRepository, WeatherForecastRepository>();
return services;
}
public static IServiceCollection AddService(this IServiceCollection services)
{
services.AddScoped<IWeatherService, WeatherService>();
return services;
}
}
}

View File

@@ -0,0 +1,47 @@
using CoreProfilerExample.Models.Parameters;
using CoreProfilerExample.Models.ViewModels;
using CoreProfilerExample.Service.Models.Dtos;
using CoreProfilerExample.Service.Models.ParameterDtos;
namespace CoreProfilerExample.Infrastructure.Extensions
{
public static class MapperExtension
{
public static ExceptionViewModel ToViewModel(this Exception exception)
{
return new ExceptionViewModel
{
Message = exception.Message,
};
}
public static async Task<GetWeatherForecastViewModel> ToViewModel(this Task<GetWeatherForecastDto> task)
{
var dto = await task;
return new GetWeatherForecastViewModel
{
WeatherForecasts = dto.WeatherForecasts.Select(forecast => forecast.ToViewModel())
};
}
public static GetWeatherForecastItemViewModel ToViewModel(this GetWeatherForecastItemDto dto)
{
return new GetWeatherForecastItemViewModel
{
Date = dto.Date,
Summary = dto.Summary,
TemperatureC = dto.TemperatureC,
TemperatureF = dto.TemperatureF,
};
}
public static GetWeatherForecastParameterDto ToDto(this GetWeatherForecastParameter parameter)
{
return new GetWeatherForecastParameterDto
{
ForecastDays = parameter.ForecastDays,
};
}
}
}

View File

@@ -0,0 +1,7 @@
namespace CoreProfilerExample.Models.Parameters
{
public class GetWeatherForecastParameter
{
public int ForecastDays { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace CoreProfilerExample.Models.ViewModels
{
public class ExceptionViewModel
{
public string? Message { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
namespace CoreProfilerExample.Models.ViewModels
{
public class GetWeatherForecastItemViewModel
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF { get; set; }
public string? Summary { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace CoreProfilerExample.Models.ViewModels
{
public class GetWeatherForecastViewModel
{
public IEnumerable<GetWeatherForecastItemViewModel> WeatherForecasts { get; set; } = [];
}
}

View File

@@ -0,0 +1,25 @@
using CoreProfilerExample.Infrastructure.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
#region CoreProfilerExample
builder.Services.AddRepository();
builder.Services.AddService();
#endregion
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,31 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:22953",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5100",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}