From 0b53aceca14c78df0ba892185ab7079d9e93701d Mon Sep 17 00:00:00 2001 From: luolan Date: Wed, 2 Jul 2025 18:27:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=A1=B9=E7=9B=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E6=9E=84=E5=BB=BA=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=EF=BC=8C=E6=9E=84=E5=BB=BA=E8=BF=94=E5=9B=9E=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGSS/AGSS.csproj | 8 ++++- AGSS/Controllers/User/UserInfoController.cs | 33 +++++++++++++++++++++ AGSS/Models/DBContext.cs | 14 +++++++++ AGSS/Models/Entities/User.cs | 8 +++++ AGSS/Models/Template/ReturnTemplate.cs | 8 +++++ AGSS/Program.cs | 9 +++--- AGSS/appsettings.Development.json | 9 ++++++ AGSS/appsettings.json | 3 ++ 8 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 AGSS/Controllers/User/UserInfoController.cs create mode 100644 AGSS/Models/DBContext.cs create mode 100644 AGSS/Models/Entities/User.cs create mode 100644 AGSS/Models/Template/ReturnTemplate.cs diff --git a/AGSS/AGSS.csproj b/AGSS/AGSS.csproj index a799f8c..32f5c9f 100644 --- a/AGSS/AGSS.csproj +++ b/AGSS/AGSS.csproj @@ -10,13 +10,19 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + + + diff --git a/AGSS/Controllers/User/UserInfoController.cs b/AGSS/Controllers/User/UserInfoController.cs new file mode 100644 index 0000000..981931f --- /dev/null +++ b/AGSS/Controllers/User/UserInfoController.cs @@ -0,0 +1,33 @@ +using AGSS.Models; +using AGSS.Models.Entities; +using AGSS.Models.Template; +using Microsoft.AspNetCore.Mvc; + +namespace AGSS.Controllers.User; + +[ApiController] +[Route("api/[controller]/[action]")] +public class UserInfoController : ControllerBase +{ + private readonly DBContext _context; + + public UserInfoController(DBContext context) + { + _context = context; + } + + [HttpGet] + public async Task My() + { + UserModel user = new UserModel + { + AuthId = "123", + Email = "123", + Password = "111" + }; + + _context.UserModels.Add(user); + await _context.SaveChangesAsync(); + return Ok(user); + } +} \ No newline at end of file diff --git a/AGSS/Models/DBContext.cs b/AGSS/Models/DBContext.cs new file mode 100644 index 0000000..b14151b --- /dev/null +++ b/AGSS/Models/DBContext.cs @@ -0,0 +1,14 @@ +using AGSS.Models.Entities; +using Microsoft.EntityFrameworkCore; + +namespace AGSS.Models; + +public class DBContext : DbContext +{ + public DBContext(DbContextOptions options) + : base(options) + { + } + + public DbSet UserModels { get; set; } +} \ No newline at end of file diff --git a/AGSS/Models/Entities/User.cs b/AGSS/Models/Entities/User.cs new file mode 100644 index 0000000..5c4f2d0 --- /dev/null +++ b/AGSS/Models/Entities/User.cs @@ -0,0 +1,8 @@ +namespace AGSS.Models.Entities; + +public class UserModel +{ + public Guid Id { get; set; } + public required string AuthId { get; set; } + +} \ No newline at end of file diff --git a/AGSS/Models/Template/ReturnTemplate.cs b/AGSS/Models/Template/ReturnTemplate.cs new file mode 100644 index 0000000..16cbbaa --- /dev/null +++ b/AGSS/Models/Template/ReturnTemplate.cs @@ -0,0 +1,8 @@ +namespace AGSS.Models.Template; + +public struct ReturnTemplate +{ + public int Code{get;set;} + public string Msg{get;set;} + public object Data{get;set;} +} \ No newline at end of file diff --git a/AGSS/Program.cs b/AGSS/Program.cs index 1b71f31..415bc5b 100644 --- a/AGSS/Program.cs +++ b/AGSS/Program.cs @@ -1,4 +1,6 @@ +using AGSS.Models; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -16,10 +18,11 @@ var builder = WebApplication.CreateBuilder(args); -// ¶ÁÈ¡ÅäÖà var domain = builder.Configuration["Auth0:Domain"]; var audience =builder.Configuration["Auth0:Audience"]; +builder.Services.AddDbContext(opt => + opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext"))); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => @@ -59,10 +62,6 @@ app.UseAuthentication(); app.UseAuthorization(); -#pragma warning disable ASP0014 // Suggest using top level route registrations -app.UseEndpoints(endpoints => endpoints.MapControllers()); -#pragma warning restore ASP0014 // Suggest using top level route registrations - app.MapControllers(); diff --git a/AGSS/appsettings.Development.json b/AGSS/appsettings.Development.json index 0c208ae..bda9442 100644 --- a/AGSS/appsettings.Development.json +++ b/AGSS/appsettings.Development.json @@ -4,5 +4,14 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "AllowedHosts": "*", + "Auth0": { + "Domain": "", + "Audience": "", + "ClientId": "" + }, + "DataBase": { + "ConnectionString": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;" } } diff --git a/AGSS/appsettings.json b/AGSS/appsettings.json index ec10538..99ba56a 100644 --- a/AGSS/appsettings.json +++ b/AGSS/appsettings.json @@ -10,5 +10,8 @@ "Domain": "", "Audience": "", "ClientId": "" + }, + "ConnectionStrings": { + "DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;" } }