diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api.sln" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api.sln" new file mode 100644 index 0000000000000000000000000000000000000000..2f92ebd9bda9292e2c0238f7ef2c03e5e1a019a9 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api.sln" @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..da2013171f9ef687974a4076fa92b8f3dfb6dd48 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/UsersController.cs" @@ -0,0 +1,78 @@ +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using MyWebApi.Api.Entity; +using System.Linq; + +namespace MyWebApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase { + [HttpGet] + // public dynamic Get(){ + // return "唯独你没懂"; + // } + + [HttpGet] + public IEnumerable Get(){ + var users = GetUsers(); + return users; + } + [HttpGet("{id}")] + public dynamic Get(int id){ + var users = GetUsers(); + var user = users.Where(x=>x.Id==id).FirstOrDefault(); + return user; + } + + + [HttpPost] + public dynamic Post(dynamic model){ + + return new{ + Data=model, + Msg="成功了" + }; + } + + [HttpPut("{id}")] + public dynamic Put(int id,dynamic model){ + + return new{ + Data=model, + Msg=string.Format("要修改的Id为{0},",id) + }; + } + + [HttpDelete("{id}")] + public dynamic Delete(int id,dynamic model){ + + return new{ + Data=model, + Msg=string.Format("要删除的Id为{0},",id) + }; + } + + + private IEnumerable GetUsers(){ + var users = new List{ + new Users{ + Id=1, + UserName="你好", + PassWord="确实好啊" + }, + new Users{ + Id=2, + UserName="真的吗", + PassWord="不啊" + }, + new Users{ + Id=3, + UserName="可以吗", + PassWord="我觉得不行" + }, + }; + return users; + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/WeatherForecastController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..4e01cb2ad30d97a1d82b387fd068eb71d3773625 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Data/ShowDb.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Data/ShowDb.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d8fcc9b0eb8d9be0c397937aa37de886b5a2d211 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Data/ShowDb.cs" @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using MyWebApi.Api.Entity; + +namespace MyWebApi.Api.Db +{ + public class ShowDb : DbContext + { + public DbSet Users { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/BaseEntity.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b8c3337032199e320986e2cf02379588c01f4ce0 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/BaseEntity.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public abstract class BaseEntity + { + public int Id{get;set;} + public bool IsActived { get; set; } + public bool IsDeleted { get; set; } + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } + public int DisplayOrder { get; set; } + public string Remarks { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Roles.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0e8a360b5dab81f94cba804073368e6e15880175 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Roles.cs" @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace MyWebApi.Api.Entity +{ + public class Roles : BaseEntity + { + public string RoleName{get;set;} + public virtual IEnumerable UserRoles { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/UserRoles.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/UserRoles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3769546994af2cc51eee61542257c188e985e306 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/UserRoles.cs" @@ -0,0 +1,13 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public class UserRoles : BaseEntity + { + public int UserId { get; set; } + public int RoleId { get; set; } + + public virtual Users User { get; set; } + public virtual Roles Role { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Users.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b97cb408346d36bed0e8864022a2facf5fb1680f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Entity/Users.cs" @@ -0,0 +1,10 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public class Users{ + public int Id{get;set;} + public String UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..bc23c650f1e443b696d60c85a8a675264ec86cf7 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" @@ -0,0 +1,43 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyWebApi.Api.Db; + +namespace MyWebApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + [Migration("20210629024032_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyWebApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d5ff20be4197359722a420552246ffc2fc8e937f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyWebApi.Api.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserName = table.Column(type: "nvarchar(max)", nullable: true), + PassWord = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" new file mode 100644 index 0000000000000000000000000000000000000000..83a875e2f3fec4cdfcb67011320d872f6c3c91ab --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" @@ -0,0 +1,41 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyWebApi.Api.Db; + +namespace MyWebApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + partial class ShowDbModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyWebApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/MyWebApi.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/MyWebApi.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..a1ec8beb2cbf668109f82c63e92056b74f2cf7c3 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/MyWebApi.Api.csproj" @@ -0,0 +1,17 @@ + + + + netcoreapp3.1 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/ParamModel/NewUser.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/ParamModel/NewUser.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5a9820b56bc3e9372c379041102652ceb510ea33 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/ParamModel/NewUser.cs" @@ -0,0 +1,7 @@ +namespace MyWebApi.Api.ParamModel +{ + public class NewUser{ + public string UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Program.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..49aa87cb41459c340fb650723ac6fe3b30053fe7 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Properties/launchSettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..549dce5e9757fe170af7b46626817b8f8b348413 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:62960", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "MyWebApi.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..32ca4a27285646336cfd5e7693fddac624622efe --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/Startup.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/WeatherForecast.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fe20bc77370d51a15a47b8f8d7cee03311599883 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyWebApi.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.Development.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/MyWebApi.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/api.http" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..95d5757152574d30b7105744cc823ae82da9cadb --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api/api.http" @@ -0,0 +1,35 @@ +###Get 获取 + +GET http://localhost:5000/users HTTP/1.1 + +### +GET http://localhost:5000/users/2 HTTP/1.1 + + +### Post 添加 + +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"里", + "PassWord":"123" +} +### Put 修改 + +PUT http://localhost:5000/users/1 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"这里", + "PassWord":"123" +} +### Delete 删除 + +DELETE http://localhost:5000/users/2 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"哪里", + "PassWord":"123" +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/.vscode/launch.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/.vscode/launch.json" new file mode 100644 index 0000000000000000000000000000000000000000..968653266b8f8830588c68f1405556726d7a0289 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/.vscode/launch.json" @@ -0,0 +1,17 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "启动程序", + "skipFiles": [ + "/**" + ], + "program": "${file}" + } + ] +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api.sln" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api.sln" new file mode 100644 index 0000000000000000000000000000000000000000..2f92ebd9bda9292e2c0238f7ef2c03e5e1a019a9 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api.sln" @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3cc13b10820e77e3dcb9a4e0c2770ebc22949d60 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/UsersController.cs" @@ -0,0 +1,78 @@ +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using MyApi.Api.Entity; +using System.Linq; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase { + [HttpGet] + // public dynamic Get(){ + // return "唯独你没懂"; + // } + + [HttpGet] + public IEnumerable Get(){ + var users = GetUsers(); + return users; + } + [HttpGet("{id}")] + public dynamic Get(int id){ + var users = GetUsers(); + var user = users.Where(x=>x.Id==id).FirstOrDefault(); + return user; + } + + + [HttpPost] + public dynamic Post(dynamic model){ + + return new{ + Data=model, + Msg="成功了" + }; + } + + [HttpPut("{id}")] + public dynamic Put(int id,dynamic model){ + + return new{ + Data=model, + Msg=string.Format("要修改的Id为{0},",id) + }; + } + + [HttpDelete("{id}")] + public dynamic Delete(int id,dynamic model){ + + return new{ + Data=model, + Msg=string.Format("要删除的Id为{0},",id) + }; + } + + + private IEnumerable GetUsers(){ + var users = new List{ + new Users{ + Id=1, + UserName="你好", + PassWord="确实好啊" + }, + new Users{ + Id=2, + UserName="真的吗", + PassWord="不啊" + }, + new Users{ + Id=3, + UserName="可以吗", + PassWord="我觉得不行" + }, + }; + return users; + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/WeatherForecastController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..4e01cb2ad30d97a1d82b387fd068eb71d3773625 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Entity/Users.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..995f8ddc036e6aaef7da38762ded3fa94e982324 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Entity/Users.cs" @@ -0,0 +1,10 @@ +using System; + +namespace MyApi.Api.Entity +{ + public class Users{ + public int Id{get;set;} + public String UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/MyWebApi.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/MyWebApi.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..d12c450b7be925549ec1738db82e35481fa8cbbc --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/MyWebApi.Api.csproj" @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Program.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..49aa87cb41459c340fb650723ac6fe3b30053fe7 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Properties/launchSettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..549dce5e9757fe170af7b46626817b8f8b348413 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:62960", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "MyWebApi.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..32ca4a27285646336cfd5e7693fddac624622efe --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/Startup.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/WeatherForecast.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fe20bc77370d51a15a47b8f8d7cee03311599883 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyWebApi.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.Development.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/MyWebApi.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/api.http" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..9fbde2b38b5d0d6ddf1765370aafe18f6fe9a467 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api0/api.http" @@ -0,0 +1,35 @@ +###Get + +GET http://localhost:5000/users HTTP/1.1 + +### +GET http://localhost:5000/users/2 HTTP/1.1 + + +### Post + +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"里", + "PassWord":"123" +} +### Put + +PUT http://localhost:5000/users/1 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"这里", + "PassWord":"123" +} +### Delete + +DELETE http://localhost:5000/users/2 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"哪里", + "PassWord":"123" +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api.sln" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api.sln" new file mode 100644 index 0000000000000000000000000000000000000000..2f92ebd9bda9292e2c0238f7ef2c03e5e1a019a9 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api.sln" @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/UsersController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..166fecdd40160c49fae1de4f0ef10e3419420ddc --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/UsersController.cs" @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using Admin3000.Backend.Api.Entity; +using Microsoft.AspNetCore.Mvc; +using System.Linq; +using MyWebApi.Api.ParamModel; +using Newtonsoft.Json; +using MyWebApi.Api.Repository; + + +namespace MyWebApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + private Api.Db.MyWebApiDb db=new Api.Db.MyWebApiDb(); + private IRepository _usersRespository=new EfRepository(); + + [HttpGet] + public dynamic Get() + { + + var users = _usersRespository.Table.ToList(); + return new { + Code =1000, + Data =users, + Msg ="获取用户列表成功^_^" + }; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + return user; + } + + [HttpPost] + public string Post(NewUser model) + { + JsonSerializerSettings settings = new JsonSerializerSettings(); + // settings.MissingMemberHandling = MissingMemberHandling.Ignore; + settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + var user = new Users + { + Username = model.Username, + Password = model.Password + }; + + db.Users.Add(user); + db.SaveChanges(); + + var res = new + { + Code = 1000, + Data = user, + Msg = "创建用户成功" + }; + return JsonConvert.SerializeObject(res, settings); + } + + [HttpPut("{id}")] + public dynamic Put(int id, NewUser model) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + + if (user != null) + { + user.Username = model.Username; + user.Password = model.Password; + user.UpdatedTime=DateTime.Now; + db.Update(user); + db.SaveChanges(); + return new + { + Code = 1000, + Data = user, + Msg = string.Format("你修改的用户的id为:{0},已经修改成功,请注意查收", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + if (user != null) + { + db.Users.Remove(user); + db.SaveChanges(); + return new + { + Code = 1000, + Msg = string.Format("删除指定id为{0}的用户成功", id) + }; + } + else + { + return new + { + Code = 104, + Data = "", + Msg = "指定Id的用户不存在,请确认后重试" + }; + } + + } + + private IEnumerable GetUsers() + { + var users = + new List { + new Users { + Id = 1, + Username = "admin", + Password = "芝麻开门" + }, + new Users { + Id = 2, + Username = "超级管理员", + Password = "芝麻开门" + }, + new Users { + Id = 3, + Username = "普通用户", + Password = "芝麻开门" + } + }; + + return users; + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/WeatherForecastController.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..4e01cb2ad30d97a1d82b387fd068eb71d3773625 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Data/ShowDb.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Data/ShowDb.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d8fcc9b0eb8d9be0c397937aa37de886b5a2d211 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Data/ShowDb.cs" @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using MyWebApi.Api.Entity; + +namespace MyWebApi.Api.Db +{ + public class ShowDb : DbContext + { + public DbSet Users { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/BaseEntity.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b8c3337032199e320986e2cf02379588c01f4ce0 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/BaseEntity.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public abstract class BaseEntity + { + public int Id{get;set;} + public bool IsActived { get; set; } + public bool IsDeleted { get; set; } + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } + public int DisplayOrder { get; set; } + public string Remarks { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Roles.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0e8a360b5dab81f94cba804073368e6e15880175 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Roles.cs" @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace MyWebApi.Api.Entity +{ + public class Roles : BaseEntity + { + public string RoleName{get;set;} + public virtual IEnumerable UserRoles { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/UserRoles.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/UserRoles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3769546994af2cc51eee61542257c188e985e306 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/UserRoles.cs" @@ -0,0 +1,13 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public class UserRoles : BaseEntity + { + public int UserId { get; set; } + public int RoleId { get; set; } + + public virtual Users User { get; set; } + public virtual Roles Role { get; set; } + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Users.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b97cb408346d36bed0e8864022a2facf5fb1680f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Entity/Users.cs" @@ -0,0 +1,10 @@ +using System; + +namespace MyWebApi.Api.Entity +{ + public class Users{ + public int Id{get;set;} + public String UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..bc23c650f1e443b696d60c85a8a675264ec86cf7 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.Designer.cs" @@ -0,0 +1,43 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyWebApi.Api.Db; + +namespace MyWebApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + [Migration("20210629024032_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyWebApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d5ff20be4197359722a420552246ffc2fc8e937f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/20210629024032_InitialCreate.cs" @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyWebApi.Api.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserName = table.Column(type: "nvarchar(max)", nullable: true), + PassWord = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" new file mode 100644 index 0000000000000000000000000000000000000000..83a875e2f3fec4cdfcb67011320d872f6c3c91ab --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Migrations/ShowDbModelSnapshot.cs" @@ -0,0 +1,41 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyWebApi.Api.Db; + +namespace MyWebApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + partial class ShowDbModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyWebApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/MyWebApi.Api.csproj" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/MyWebApi.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..a1ec8beb2cbf668109f82c63e92056b74f2cf7c3 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/MyWebApi.Api.csproj" @@ -0,0 +1,17 @@ + + + + netcoreapp3.1 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/ParamModel/NewUser.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/ParamModel/NewUser.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5a9820b56bc3e9372c379041102652ceb510ea33 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/ParamModel/NewUser.cs" @@ -0,0 +1,7 @@ +namespace MyWebApi.Api.ParamModel +{ + public class NewUser{ + public string UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Program.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..49aa87cb41459c340fb650723ac6fe3b30053fe7 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Program.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Properties/launchSettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..549dce5e9757fe170af7b46626817b8f8b348413 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:62960", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "MyWebApi.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Startup.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..32ca4a27285646336cfd5e7693fddac624622efe --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/Startup.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace MyWebApi.Api +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/WeatherForecast.cs" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fe20bc77370d51a15a47b8f8d7cee03311599883 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyWebApi.Api +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.Development.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.json" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/MyWebApi.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/api.http" "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..95d5757152574d30b7105744cc823ae82da9cadb --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/MyApi/MyWebApi.Api23/api.http" @@ -0,0 +1,35 @@ +###Get 获取 + +GET http://localhost:5000/users HTTP/1.1 + +### +GET http://localhost:5000/users/2 HTTP/1.1 + + +### Post 添加 + +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"里", + "PassWord":"123" +} +### Put 修改 + +PUT http://localhost:5000/users/1 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"这里", + "PassWord":"123" +} +### Delete 删除 + +DELETE http://localhost:5000/users/2 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"哪里", + "PassWord":"123" +} \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210Api dotnet \345\221\275\344\273\244\357\274\211.md" similarity index 100% rename from "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.19\357\274\210V dotnet \345\221\275\344\273\244\357\274\211.md" rename to "\346\242\201\350\211\272\347\274\244/Vue - 2021.06.22\357\274\210Api dotnet \345\221\275\344\273\244\357\274\211.md" diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210Api RESTful \346\217\222\344\273\266\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210Api RESTful \346\217\222\344\273\266\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..8707d9cca185fba0b14c60427ee38157a8bade38 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.23\357\274\210Api RESTful \346\217\222\344\273\266\357\274\211.md" @@ -0,0 +1,17 @@ +# 安装REST CLient 插件 + +## 控制器中用法 + +``` +// GET +[HttpGet("{id}")] + +// POST +[HttpPost] + +// PUT +[HttpPut("{id}")] + +// Delete +[HttpDelete("{id}")] +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210Api \350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210Api \350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..20e8641de51f7b2efd9685fb583e38d3172690cb --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.26\357\274\210Api \350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\357\274\211.md" @@ -0,0 +1,82 @@ +# Api 连接数据库 + +# 安装 EF Core + +### 获取 Entity Framework Core 运行时 + +把EF Core添加到项目中,要使用的数据库提供程序的 NuGet 包 + +命名 +``` +// sqlserver/sqlite + +dotnet add package Microsoft.EntityFrameworkCore.Sql数据库类型(sqlserver/sqlite) +``` + +## 数据库迁移 +``` +// 安装dotnet-ef +dotnet tool install --global dotnet-ef + +// 安装EFCore.Design +dotnet add package Microsoft.EntityFrameworkCore.Design + +// 创建迁移,指示 EF Core 创建名为 InitialCreate 的迁移 +dotnet ef migrations add InitialCreate + +// 创建数据库和架构 +dotnet ef database update +``` + +创建模型 Data 部分配置演示 + +``` +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; + +namespace EFGetStarted +{ + public class BloggingContext : DbContext + { + public DbSet Blogs { get; set; } + public DbSet Posts { get; set; } + + // The following configures EF to create a Sqlite database file as `C:\blogging.db`. + // For Mac or Linux, change this to `/tmp/blogging.db` or any other absolute path. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlite(@"Data Source=C:\blogging.db"); + } + + public class Blog + { + public int BlogId { get; set; } + public string Url { get; set; } + + public List Posts { get; } = new List(); + } + + public class Post + { + public int PostId { get; set; } + public string Title { get; set; } + public string Content { get; set; } + + public int BlogId { get; set; } + public Blog Blog { get; set; } + } +} +``` +创建数据库 + ++ 以下步骤使用迁移创建数据库 + +命令 +``` +dotnet tool install --global dotnet-ef + +dotnet add package Microsoft.EntityFrameworkCore.Design + +dotnet ef migrations add InitialCreate + +dotnet ef database update +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.29\357\274\210Api \345\260\201\350\243\205\350\241\250 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\345\205\263\347\263\273\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.29\357\274\210Api \345\260\201\350\243\205\350\241\250 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\345\205\263\347\263\273\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..2b21bd738dbda55909bfb238484f217038869118 --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.29\357\274\210Api \345\260\201\350\243\205\350\241\250 \345\273\272\347\253\213\350\241\250\344\271\213\351\227\264\345\205\263\347\263\273\357\274\211.md" @@ -0,0 +1,81 @@ +# Api封装表 建立表之间关系 + +## BaseEntity继承表 + +### 封装表 +``` +namespace MyApi.Api.Entity +{ + // abstract只能被继承 + public abstract class BaseEntity + { + // 一些主要表字段 + + // Id + public int Id { get; set; } + // 是否有效 + public bool IsActived { get; set; } + // 是否删除 + public bool IsDeleted { get; set; } + // 创建时间 + public DateTime CreatedTime { get; set; } + // 删除时间 + public DateTime UpdatedTime { get; set; } + // 显示顺序 + public int DisplayOrder { get; set; } + // 备注 + public string Remarks { get; set; } + } +} +``` + +## 表关系 + +### User表 +``` +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class Users:BaseEntity + { + // 用户名 + public String UserName { get; set; } + // 用户密码 + public string PassWord { get; set; } + } +} +``` + +### UserRoles表 +``` +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class UserRoles:BaseEntity + { + // 用户Id + public int UserId { get; set; } + // 角色Id + public int RoleId { get; set; } + + public virtual Users User { get; set; } + + public virtual Roles Role { get; set; } + } +} +``` + +### Roles表 +``` +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class Roles:BaseEntity + { + // 角色名称 + public string RoleName { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + } +} +``` \ No newline at end of file diff --git "a/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.30\357\274\210Api \345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245\357\274\211.md" "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.30\357\274\210Api \345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..9255451771e574d026c3f05ebf9acd1d12a91f0f --- /dev/null +++ "b/\346\242\201\350\211\272\347\274\244/Vue - 2021.06.30\357\274\210Api \345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245\357\274\211.md" @@ -0,0 +1,218 @@ +# Api 封装接口增删改查 + +### IRepository 配置 +``` +公共接口 IRepository 其中 T :类 + + 公共接口 IRepository 其中 T :类 + { + T 获取(对象 ID); + 无效附加(T实体); + IQueryable GetAll(); + 无效插入(T实体); + 无效删除(T实体); + void SubmitChanges(); + } + + 特点:IRepository 绑定到单个类型、IRepository 可能绑定到多个类型(一般情况使用 IRepository ) +``` + ++ MyWebApi +``` +namespace MyWebApi.Repository +{ + public interface IRepository + { + IQueryable Table { get; } + + /// + /// 根据Id获取指定实体 + /// + /// + /// + T GetById(int id); + + + /// + /// 使用实体对象,插入数据 + /// + /// 需要插入的对象 + void Insert(T entity); + + + /// + /// 使用实体对象,插入数据(异步) + /// + /// 需要插入的对象 + Task InsertAsync(T entity); + + + /// + /// 根据对象更新数据 + /// + /// 要更新的对象 + void Update(T entity); + + + /// + /// 根据对象更新数据(异步) + /// + /// 要更新的对象 + /// + Task UpdateAsync(T entity); + + + /// + /// 根据Id删除对应的记录 + /// + /// 主键id + void Delete(int id); + + } +} +``` + +### EFRepository配置 + +1. 用来封装存储,读取和查找行为的机制,它模拟了一个对象集合 + +2. 访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调 + ++ 提取为IRepository接口,比如常见的也就是增删改查等方法 + +``` + public interface IRepository where T : class + { + //改变模型中没有更新,里面的Save就取代更新功能 + IEnumerable FindAll(Func exp); + void Add(T entity); + void Delete(T entity); + void Save(); + } +``` + ++ MyWebApi + +``` +namespace MyApi.Api.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + /// + /// 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + /// + /// + private ShowDb db = new ShowDb(); + + + /// + /// 一个字段成员,用于内部Entity属性 + /// + private DbSet _entity; + + + /// + /// 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + /// + /// + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + + + /// + /// 代表一个可以用于查询T类型的表 + /// + /// + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + + /// + /// 删除(指定T类型,在数据库当中代表指定的表)指定Id的记录 + /// + /// + public void Delete(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + Entity.Remove(t); + db.SaveChanges(); + } + + public T GetById(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + return t; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + db.SaveChanges(); + } + + public async Task InsertAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await db.SaveChangesAsync(); + } + + public void Update(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + db.SaveChanges(); + } + + public async Task UpdateAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + await db.SaveChangesAsync(); + } + } +} +``` \ No newline at end of file