-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockServer.cs
More file actions
45 lines (34 loc) · 1.21 KB
/
MockServer.cs
File metadata and controls
45 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#:sdk Microsoft.NET.Sdk.Web
#:package Microsoft.AspNetCore.OpenApi 10.0.0-preview.4.25258.110
#:package Scalar.AspNetCore 2.4.4
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi();
app.MapScalarApiReference("/");
var mockdata = File.ReadAllText("people.json");
var people = JsonSerializer.Deserialize<List<Person>>(mockdata, JsonSerializerOptions.Web) ?? [];
app.MapGet("/people", (string? search) =>
{
var matchingPeople = string.IsNullOrEmpty(search)
? people
: [.. people.Where(p => p.FullName.Contains(search, StringComparison.OrdinalIgnoreCase))];
return TypedResults.Ok(matchingPeople);
});
app.Run();
record Person
{
public required string FirstName { get; set; }
public required string LastName { get; set; }
public required string Email { get; set; }
public required string Address { get; set; }
[JsonIgnore]
public string FullName => $"{FirstName} {LastName}";
}