-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
91 lines (71 loc) · 2.32 KB
/
Program.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Microsoft.EntityFrameworkCore;
using UserStore.Data;
using Microsoft.OpenApi.Models;
using UserStore.Models;
// snippet ALLOW CORS
const string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("users") ?? "Data Source=users1.db";
builder.Services.AddDbContext<UserDb>(options => options.UseSqlite(connectionString));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Users API", Description = "Users user", Version = "v1" });
});
// snippet allow CORS
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
// use specific origins
//builder.WithOrigins("*");
// use all origins
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Users API V1");
});
// middleware to allow CORS
app.UseCors(MyAllowSpecificOrigins);
app.MapGet("/", () => "Users API working!");
app.MapGet("/user", async (UserDb db) => await db.Users.ToListAsync());
app.MapPost("/user", async (UserDb db, User user) =>
{
await db.Users.AddAsync(user);
await db.SaveChangesAsync();
return Results.Created($"/user/{user.Id}", user);
});
app.MapPut("/user/{id}", async (UserDb db, User updateUser, int id) =>
{
var userItem = await db.Users.FindAsync(id);
if (userItem is null) return Results.NotFound();
userItem.Firstname = updateUser.Firstname;
userItem.Lastname = updateUser.Lastname;
userItem.About = updateUser.About;
userItem.Username = updateUser.Username;
userItem.Usernumber = updateUser.Usernumber;
userItem.City = updateUser.City;
userItem.Province = updateUser.Province;
userItem.Country = updateUser.Country;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/user/{id}", async (UserDb db, int id) =>
{
var todo = await db.Users.FindAsync(id);
if (todo is null)
{
return Results.NotFound();
}
db.Users.Remove(todo);
await db.SaveChangesAsync();
return Results.Ok();
});
app.Run();