Skip to content

Commit fb9d607

Browse files
committed
Example project with controllers
1 parent c15ab42 commit fb9d607

File tree

10 files changed

+732
-0
lines changed

10 files changed

+732
-0
lines changed

.gitignore

Lines changed: 484 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using ProjectMap.WebApi.Models;
3+
using ProjectMap.WebApi.Repositories;
4+
using System;
5+
using static System.Runtime.InteropServices.JavaScript.JSType;
6+
7+
namespace ProjectMap.WebApi.Controllers;
8+
9+
[ApiController]
10+
[Route("WeatherForecasts")]
11+
public class WeatherForecastController : ControllerBase
12+
{
13+
private readonly WeatherForecastRepository _weatherForecastRepository;
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(WeatherForecastRepository weatherForecastRepository, ILogger<WeatherForecastController> logger)
17+
{
18+
_weatherForecastRepository = weatherForecastRepository;
19+
_logger = logger;
20+
}
21+
22+
[HttpGet(Name = "ReadWeatherForecasts")]
23+
public async Task<ActionResult<IEnumerable<WeatherForecast>>> Get()
24+
{
25+
var weatherForecasts = await _weatherForecastRepository.ReadAsync();
26+
return Ok(weatherForecasts);
27+
}
28+
29+
[HttpGet("{weatherForecastId}", Name = "ReadWeatherForecast")]
30+
public async Task<ActionResult<WeatherForecast>> Get(Guid weatherForecastId)
31+
{
32+
var weatherForeCast = await _weatherForecastRepository.ReadAsync(weatherForecastId);
33+
if (weatherForeCast == null)
34+
return NotFound();
35+
36+
return Ok(weatherForeCast);
37+
}
38+
39+
[HttpPost(Name = "CreateWeatherForecast")]
40+
public async Task<ActionResult> Add(WeatherForecast weatherForecast)
41+
{
42+
weatherForecast.Id = Guid.NewGuid();
43+
44+
var createdWeatherForecast = await _weatherForecastRepository.InsertAsync(weatherForecast);
45+
return Created();
46+
}
47+
48+
[HttpPut("{weatherForecastId}", Name = "UpdateWeatherForecast")]
49+
public async Task<ActionResult> Update(Guid weatherForecastId, WeatherForecast newWeatherForeCast)
50+
{
51+
var existingWeatherForecast = await _weatherForecastRepository.ReadAsync(weatherForecastId);
52+
53+
if (existingWeatherForecast == null)
54+
return NotFound();
55+
56+
await _weatherForecastRepository.UpdateAsync(newWeatherForeCast);
57+
58+
return Ok(newWeatherForeCast);
59+
}
60+
61+
[HttpDelete("{weatherForecastId}", Name = "DeleteWeatherForecastByDate")]
62+
public async Task<IActionResult> Update(Guid weatherForecastId)
63+
{
64+
var existingWeatherForecast = await _weatherForecastRepository.ReadAsync(weatherForecastId);
65+
66+
if (existingWeatherForecast == null)
67+
return NotFound();
68+
69+
await _weatherForecastRepository.DeleteAsync(weatherForecastId);
70+
71+
return Ok();
72+
}
73+
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ProjectMap.WebApi.Models;
4+
5+
public class WeatherForecast
6+
{
7+
public Guid Id { get; set; }
8+
9+
public DateOnly Date { get; set; }
10+
11+
[Range(-75, 75)]
12+
public int TemperatureC { get; set; }
13+
14+
[Required]
15+
public string? Summary { get; set; }
16+
}
17+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.Identity.Client;
2+
using ProjectMap.WebApi.Repositories;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
8+
builder.Services.AddControllers();
9+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
10+
builder.Services.AddOpenApi();
11+
12+
builder.Services.Configure<RouteOptions>(o => o.LowercaseUrls = true);
13+
14+
var sqlConnectionString = builder.Configuration["SqlConnectionString"];
15+
16+
if (string.IsNullOrWhiteSpace(sqlConnectionString))
17+
throw new InvalidProgramException("Configuration variable SqlConnectionString not found");
18+
19+
builder.Services.AddTransient<WeatherForecastRepository, WeatherForecastRepository>(o => new WeatherForecastRepository(sqlConnectionString));
20+
21+
var app = builder.Build();
22+
23+
// Configure the HTTP request pipeline.|
24+
app.MapOpenApi();
25+
26+
app.UseHttpsRedirection();
27+
28+
app.UseAuthorization();
29+
30+
app.MapControllers();
31+
32+
app.Run();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>55900447-1105-4771-9aad-843f4381b24e</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Dapper" Version="2.1.66" />
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
13+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.2" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5225",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7222;http://localhost:5225",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Dapper;
2+
using Microsoft.Data.SqlClient;
3+
using ProjectMap.WebApi.Models;
4+
5+
namespace ProjectMap.WebApi.Repositories
6+
{
7+
public class WeatherForecastRepository
8+
{
9+
private readonly string sqlConnectionString;
10+
11+
public WeatherForecastRepository(string sqlConnectionString)
12+
{
13+
this.sqlConnectionString = sqlConnectionString;
14+
}
15+
16+
public async Task<WeatherForecast> InsertAsync(WeatherForecast weatherForecast)
17+
{
18+
using (var sqlConnection = new SqlConnection(sqlConnectionString))
19+
{
20+
var environmentId = await sqlConnection.ExecuteAsync("INSERT INTO [WeatherForecast] (Id, TemperatureC, Summary) VALUES (@Id, @TemperatureC, @Summary)", weatherForecast);
21+
return weatherForecast;
22+
}
23+
}
24+
25+
public async Task<WeatherForecast?> ReadAsync(Guid id)
26+
{
27+
using (var sqlConnection = new SqlConnection(sqlConnectionString))
28+
{
29+
return await sqlConnection.QuerySingleOrDefaultAsync<WeatherForecast>("SELECT * FROM [WeatherForecast] WHERE Id = @Id", new { id });
30+
}
31+
}
32+
33+
public async Task<IEnumerable<WeatherForecast>> ReadAsync()
34+
{
35+
using (var sqlConnection = new SqlConnection(sqlConnectionString))
36+
{
37+
return await sqlConnection.QueryAsync<WeatherForecast>("SELECT * FROM [WeatherForecast]");
38+
}
39+
}
40+
41+
public async Task UpdateAsync(WeatherForecast environment)
42+
{
43+
using (var sqlConnection = new SqlConnection(sqlConnectionString))
44+
{
45+
await sqlConnection.ExecuteAsync("UPDATE [WeatherForecast] SET " +
46+
"TemperatureC = @TemperatureC, " +
47+
"Summary = @Summary"
48+
, environment);
49+
50+
}
51+
}
52+
53+
public async Task DeleteAsync(Guid id)
54+
{
55+
using (var sqlConnection = new SqlConnection(sqlConnectionString))
56+
{
57+
await sqlConnection.ExecuteAsync("DELETE FROM [WeatherForecast] WHERE Id = @Id", new { id });
58+
}
59+
}
60+
61+
}
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is my project
2+
3+
* My First Commit
4+
* My Second Commit
5+
* My Third Commit
6+
* Some other commit

0 commit comments

Comments
 (0)