From 0630406e1ffa35e1dbf38406f7318e3ce8c1145e Mon Sep 17 00:00:00 2001 From: mango <1620034128@qq.com> Date: Mon, 8 Jun 2020 17:51:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=A1=E5=AE=B6=E6=AC=A3=E7=9A=84=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApiTest001.sln" | 25 ++++++ .../Controllers/TestController.cs" | 29 +++++++ .../Controllers/WeatherForecastController.cs" | 64 +++++++++++++++ .../Domain/Admin4400DbContext.cs" | 27 +++++++ .../WebApiTest001/Domain/BaseEntity.cs" | 35 ++++++++ .../WebApiTest001/Domain/DbInitializer.cs" | 55 +++++++++++++ .../WebApiTest001/Domain/Entity/Roles.cs" | 13 +++ .../WebApiTest001/Domain/Entity/Users.cs" | 18 +++++ .../WebApiTest001/Helper/JsonHelper.cs" | 20 +++++ .../Implementation/EfRespository.cs" | 80 +++++++++++++++++++ .../WebApiTest001/Interface/IRespository.cs" | 24 ++++++ .../WebApiTest001/Program.cs" | 26 ++++++ .../Properties/launchSettings.json" | 30 +++++++ .../WebApiTest001/Startup.cs" | 57 +++++++++++++ .../WebApiTest001/WeatherForecast.cs" | 15 ++++ .../WebApiTest001/WebApiTest001.csproj" | 14 ++++ .../appsettings.Development.json" | 9 +++ .../WebApiTest001/appsettings.json" | 10 +++ 18 files changed, 551 insertions(+) create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001.sln" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/TestController.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/WeatherForecastController.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Admin4400DbContext.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/BaseEntity.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/DbInitializer.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Roles.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Users.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Helper/JsonHelper.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Implementation/EfRespository.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Interface/IRespository.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Program.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Properties/launchSettings.json" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/Startup.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/WeatherForecast.cs" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/WebApiTest001.csproj" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.Development.json" create mode 100644 "\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.json" diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001.sln" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001.sln" new file mode 100644 index 0000000..a87a82e --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30128.74 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiTest001", "WebApiTest001\WebApiTest001.csproj", "{8F80F9FB-D67A-4675-9EC2-0D9A08E314DB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8F80F9FB-D67A-4675-9EC2-0D9A08E314DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F80F9FB-D67A-4675-9EC2-0D9A08E314DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F80F9FB-D67A-4675-9EC2-0D9A08E314DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F80F9FB-D67A-4675-9EC2-0D9A08E314DB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {31917A23-B65E-4C8F-BD6E-A5F4DEF5B685} + EndGlobalSection +EndGlobal diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/TestController.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/TestController.cs" new file mode 100644 index 0000000..361041b --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/TestController.cs" @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using WebApiTest001.Domain.Entity; +using WebApiTest001.Helper; +using WebApiTest001.Interface; + +namespace WebApiTest001.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : ControllerBase + { + private readonly IRespository _userRepository; + + public TestController(IRespository userRepository) + { + _userRepository = userRepository; + } + public string Get() + { + var list = _userRepository.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/WeatherForecastController.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..3209bba --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Controllers/WeatherForecastController.cs" @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using WebApiTest001.Domain; +using WebApiTest001.Domain.Entity; +using WebApiTest001.Helper; +//using Microsoft.Extensions.DependencyInjection; + +namespace WebApiTest001.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private readonly Admin4400DbContext _db; + + //[ActivatorUtilitiesConstructor] + + public WeatherForecastController(Admin4400DbContext dbContext) + { + _db = dbContext; + } + + 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 string 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(); + + var res = _db.Users.Include(x=>x.Roles).ToList(); + + + + //return JsonConvert.SerializeObject(res,Formatting.Indented,new JsonSerializerSettings { + // ReferenceLoopHandling=ReferenceLoopHandling.Ignore, + // DateFormatString = "yyyy-MM-dd HH:mm:ss" + //}); + return JsonHelper.SerializeObject(res); + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Admin4400DbContext.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Admin4400DbContext.cs" new file mode 100644 index 0000000..08d7e8c --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Admin4400DbContext.cs" @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest001.Domain.Entity; + +namespace WebApiTest001.Domain +{ + public class Admin4400DbContext:DbContext + { + public Admin4400DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "server=DESKTOP-M14GQ0V;database=Admin4400;uid=sa;pwd=123456;"; + optionsBuilder.UseSqlServer(connectionString); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/BaseEntity.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/BaseEntity.cs" new file mode 100644 index 0000000..8961115 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/BaseEntity.cs" @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest001.Domain.Entity +{ + public abstract class BaseEntity + { + + public BaseEntity() + { + IsActived = true; + IsDeleted = false; + CreatedTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + + public int Id { get; set; } + + public bool IsActived { get; set; } + + public bool IsDeleted { get; set; } + + public int MyProperty { get; set; } + + public DateTime CreatedTime { get; set; } + + public DateTime UpdatedTime { get; set; } + + public int DisplayOrder { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/DbInitializer.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/DbInitializer.cs" new file mode 100644 index 0000000..c0a3885 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/DbInitializer.cs" @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest001.Domain.Entity; + +namespace WebApiTest001.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using (var db = new Admin4400DbContext()) + { + //db.Database.EnsureCreated(); + db.Database.EnsureCreated(); + + var hasUsers = db.Users.Any(); + + if (!hasUsers) + { + var role = new Roles + { + RoleName = "管理员", + Description = "这是一个管理员" + }; + + db.Roles.Add(role); + + db.SaveChanges(); + + db.Users.AddRange(new Users[] + { + new Users + { + UserName = "admin", + PassWord = "admin", + RolesId = role.Id + }, + new Users + { + UserName = "usr01", + PassWord = "123456", + RolesId = role.Id + } + }); + + db.SaveChanges(); + } + } + + + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Roles.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Roles.cs" new file mode 100644 index 0000000..407ce66 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Roles.cs" @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace WebApiTest001.Domain.Entity +{ + public class Roles:BaseEntity + { + public string RoleName { get; set; } + + public string Description { get; set; } + + public IEnumerable Users { get; set; } + } +} \ No newline at end of file diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Users.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Users.cs" new file mode 100644 index 0000000..2cdb8ae --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Domain/Entity/Users.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest001.Domain.Entity +{ + public class Users:BaseEntity + { + public string UserName { get; set; } + + public string PassWord { get; set; } + + public int RolesId { get; set; } + + public Roles Roles { get; set; } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Helper/JsonHelper.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Helper/JsonHelper.cs" new file mode 100644 index 0000000..638ecf2 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Helper/JsonHelper.cs" @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest001.Helper +{ + public class JsonHelper + { + public static string SerializeObject(object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyyy-MM-dd HH:mm:ss" + }); + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Implementation/EfRespository.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Implementation/EfRespository.cs" new file mode 100644 index 0000000..1888bd1 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Implementation/EfRespository.cs" @@ -0,0 +1,80 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest001.Domain; +using WebApiTest001.Domain.Entity; +using WebApiTest001.Interface; + +namespace WebApiTest001.Implementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin4400DbContext db; + + private DbSet _entity; + + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + + public IQueryable Table + { + get + { + + return Entity; + } + } + + public EfRespository(Admin4400DbContext dbContext) + { + db = dbContext; + } + + public void Delete(T entity) + { + this._entity.Remove(entity); + + db.SaveChanges(); + } + + public void Delete(int id) + { + var row=Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(row); + } + + public T GetById(int id) + { + return _entity.Where(x => x.Id == id).FirstOrDefault(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + public void InsertBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Update(T entity) + { + _entity.Update(entity); + db.SaveChanges(); + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Interface/IRespository.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Interface/IRespository.cs" new file mode 100644 index 0000000..d377a29 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Interface/IRespository.cs" @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest001.Interface +{ + public interface IRespository + { + IQueryable Table { get; } + T GetById(int id); + + + void Insert(T entity); + + void InsertBulk(IEnumerable list); + + void Update(T entity); + + void Delete(T entity); + + void Delete(int id); + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Program.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Program.cs" new file mode 100644 index 0000000..9ee6640 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/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 WebApiTest001 +{ + 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/\350\203\241\345\256\266\346\254\243/WebApiTest001/Properties/launchSettings.json" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Properties/launchSettings.json" new file mode 100644 index 0000000..9fba49c --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49746", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApiTest001": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/Startup.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Startup.cs" new file mode 100644 index 0000000..14c7b87 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/Startup.cs" @@ -0,0 +1,57 @@ +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; +using WebApiTest001.Domain; +using WebApiTest001.Implementation; +using WebApiTest001.Interface; + +namespace WebApiTest001 +{ + 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.AddDbContext(); + + services.AddScoped(typeof(IRespository<>), typeof(EfRespository<>)); + + 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(); + }); + + DbInitializer.Seed(); + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/WeatherForecast.cs" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/WeatherForecast.cs" new file mode 100644 index 0000000..e8d1db2 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApiTest001 +{ + 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/\350\203\241\345\256\266\346\254\243/WebApiTest001/WebApiTest001.csproj" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/WebApiTest001.csproj" new file mode 100644 index 0000000..da10d50 --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/WebApiTest001.csproj" @@ -0,0 +1,14 @@ + + + + netcoreapp3.1 + + + + + + + + + + diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.Development.json" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.Development.json" new file mode 100644 index 0000000..8983e0f --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.json" "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\350\203\241\345\256\266\346\254\243/WebApiTest001/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} -- Gitee