Skip to content

Commit

Permalink
New example
Browse files Browse the repository at this point in the history
  • Loading branch information
fakefeik committed May 9, 2023
1 parent a277778 commit fd504d7
Show file tree
Hide file tree
Showing 47 changed files with 739 additions and 519 deletions.
26 changes: 17 additions & 9 deletions AspNetCoreExample.Api/AspNetCoreExample.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TypeScript.ContractGenerator\TypeScript.ContractGenerator.csproj" />
</ItemGroup>

</Project>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>
29 changes: 13 additions & 16 deletions AspNetCoreExample.Api/Controllers/NotesController.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
using System;

using AspNetCoreExample.Api.Models;
using AspNetCoreExample.Api.Models;

using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreExample.Api.Controllers
namespace AspNetCoreExample.Api.Controllers;

[Route("v1/user/{userId}/blog")]
public class NotesController : ControllerBase
{
[Route("v1/user/{userId}/blog")]
public class NotesController : ControllerBase
[HttpPost]
public ActionResult AddEntry(Guid userId, [FromBody] BlogEntry entry)
{
[HttpPost]
public ActionResult AddEntry(Guid userId, [FromBody] BlogEntry entry)
{
return Ok();
}
return Ok();
}

[HttpPost("batch")]
public ActionResult AddEntries(Guid userId, [FromBody] BlogEntry[] entries)
{
return Ok();
}
[HttpPost("batch")]
public ActionResult AddEntries(Guid userId, [FromBody] BlogEntry[] entries)
{
return Ok();
}
}
50 changes: 29 additions & 21 deletions AspNetCoreExample.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
using System;

using AspNetCoreExample.Api.Models;
using AspNetCoreExample.Api.Models;

using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreExample.Api.Controllers
namespace AspNetCoreExample.Api.Controllers;

[Route("v1/users")]
public class UserController : ControllerBase
{
[Route("v1/user")]
public class UserController : ControllerBase
[HttpPost]
public ActionResult CreateUser([FromBody] User user)
{
return Ok();
}

[HttpDelete("{userId:guid}")]
public ActionResult DeleteUser(Guid userId)
{
return Ok();
}

[HttpGet("{userId:guid}")]
public ActionResult<User> GetUser(Guid userId)
{
[HttpPost]
public ActionResult CreateUser([FromBody] User user)
{
return Ok();
}
return Ok(null);
}

[HttpDelete("{userId}")]
public ActionResult DeleteUser(Guid userId)
{
return Ok();
}
[HttpGet]
public ActionResult<User[]> SearchUsers([FromQuery] string name)
{
return Ok(TotallyNotHttpMethod(name));
}

[HttpGet("{userId}")]
public ActionResult<User> GetUser(Guid userId)
{
return Ok(null);
}
private User[] TotallyNotHttpMethod(string name)
{
return Array.Empty<User>();
}
}
62 changes: 62 additions & 0 deletions AspNetCoreExample.Api/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Text.Json;

using AspNetCoreExample.Api.Models;

using Microsoft.AspNetCore.Mvc;

using SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController;

namespace AspNetCoreExample.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
this.logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
}

[HttpPost("Update/{city}")]
public void Update(string city, [FromBody] WeatherForecast forecast, CancellationToken cancellationToken)
{
}

[HttpPost("~/[action]")]
public void Reset(int seed)
{
}

[UrlOnly]
[HttpGet("{city}")]
public ActionResult Download(string city)
{
var forecast = new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(1)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
};
return File(JsonSerializer.SerializeToUtf8Bytes(forecast), "application/json");
}

private static readonly string[] summaries =
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> logger;
}
11 changes: 5 additions & 6 deletions AspNetCoreExample.Api/Models/BlogEntry.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace AspNetCoreExample.Api.Models
namespace AspNetCoreExample.Api.Models;

public class BlogEntry
{
public class BlogEntry
{
public string Title { get; set; }
public string Content { get; set; }
}
public string Title { get; set; }
public string Content { get; set; }
}
11 changes: 4 additions & 7 deletions AspNetCoreExample.Api/Models/User.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
namespace AspNetCoreExample.Api.Models;

namespace AspNetCoreExample.Api.Models
public class User
{
public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public Guid Id { get; set; }
public string Name { get; set; }
}
15 changes: 15 additions & 0 deletions AspNetCoreExample.Api/Models/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SkbKontur.TypeScript.ContractGenerator.Attributes;

namespace AspNetCoreExample.Api.Models;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

[ContractGeneratorIgnore]
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
25 changes: 25 additions & 0 deletions AspNetCoreExample.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

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.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
20 changes: 20 additions & 0 deletions AspNetCoreExample.Api/TypeScriptConfiguration/CustomGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Mvc;

using SkbKontur.TypeScript.ContractGenerator;
using SkbKontur.TypeScript.ContractGenerator.Internals;
using SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController;

namespace AspNetCoreExample.Api.TypeScriptConfiguration;

public class CustomGenerator : CustomTypeGenerator
{
public CustomGenerator()
{
var controllerBase = TypeInfo.From<ControllerBase>();
WithTypeLocationRule(t => controllerBase.IsAssignableFrom(t), t => $"Api/{t.Name.Replace("Controller", "Api")}")
.WithTypeLocationRule(t => !controllerBase.IsAssignableFrom(t), t => $"DataTypes/{t.Name}")
.WithTypeRedirect(TypeInfo.From<Guid>(), "Guid", @"DataTypes\Guid")
.WithTypeRedirect(TypeInfo.From<DateOnly>(), "DateOnly", @"DataTypes\DateOnly")
.WithTypeBuildingContext(t => controllerBase.IsAssignableFrom(t), (u, t) => new ApiControllerTypeBuildingContext(u, t));
}
}
20 changes: 20 additions & 0 deletions AspNetCoreExample.Api/TypeScriptConfiguration/TypesProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AspNetCoreExample.Api.Controllers;

using SkbKontur.TypeScript.ContractGenerator;
using SkbKontur.TypeScript.ContractGenerator.Abstractions;
using SkbKontur.TypeScript.ContractGenerator.Internals;

namespace AspNetCoreExample.Api.TypeScriptConfiguration;

public class TypesProvider : IRootTypesProvider
{
public ITypeInfo[] GetRootTypes()
{
return new[]
{
TypeInfo.From<WeatherForecastController>(),
TypeInfo.From<UserController>(),
TypeInfo.From<NotesController>()
};
}
}
8 changes: 8 additions & 0 deletions AspNetCoreExample.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions AspNetCoreExample.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Loading

0 comments on commit fd504d7

Please sign in to comment.