From f20dd7ed65529727d1ef8a3d0d7173b2933cbf7e Mon Sep 17 00:00:00 2001 From: schooi2462474163 Date: Thu, 9 Jul 2020 09:19:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApplication3.sln" | 25 +++++++ .../Controllers/WeatherForecastController.cs" | 57 ++++++++++++++++ .../Domain/Admin200DbContext.cs" | 25 +++++++ .../WebApplication3/Domain/BaseEntity.cs" | 25 +++++++ .../WebApplication3/Domain/DbInitializer.cs" | 52 ++++++++++++++ .../WebApplication3/Domain/Entity/Roles.cs" | 12 ++++ .../WebApplication3/Domain/Entity/Users.cs" | 16 +++++ .../WebApplication3/Helper/JosnHelper.cs" | 20 ++++++ .../Implementation/EfRespository.cs" | 68 +++++++++++++++++++ .../Interface/IRespository.cs" | 19 ++++++ .../WebApplication3/Program.cs" | 26 +++++++ .../Properties/launchSettings.json" | 30 ++++++++ .../WebApplication3/Startup.cs" | 56 +++++++++++++++ .../WebApplication3/WeatherForecast.cs" | 15 ++++ .../WebApplication3/WebApplication3.csproj" | 14 ++++ .../appsettings.Development.json" | 9 +++ .../WebApplication3/appsettings.json" | 10 +++ 17 files changed, 479 insertions(+) create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3.sln" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Controllers/WeatherForecastController.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Admin200DbContext.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/BaseEntity.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/DbInitializer.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Roles.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Users.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Helper/JosnHelper.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Implementation/EfRespository.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Interface/IRespository.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Program.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Properties/launchSettings.json" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/Startup.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/WeatherForecast.cs" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/WebApplication3.csproj" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.Development.json" create mode 100644 "\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.json" diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3.sln" "b/\346\235\216\347\233\233\345\277\227/WebApplication3.sln" new file mode 100644 index 0000000..9810ec5 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29613.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication3", "WebApplication3\WebApplication3.csproj", "{F667C061-3637-4364-8E08-9D99247096DE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F667C061-3637-4364-8E08-9D99247096DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F667C061-3637-4364-8E08-9D99247096DE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F667C061-3637-4364-8E08-9D99247096DE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F667C061-3637-4364-8E08-9D99247096DE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {94932AF8-90FF-4CD5-843A-6124789B59E4} + EndGlobalSection +EndGlobal diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Controllers/WeatherForecastController.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000..71e4450 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Controllers/WeatherForecastController.cs" @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using WebApplication3.Domain; +using WebApplication3.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Microsoft.EntityFrameworkCore; +using WebApplication3.Helper; + +namespace WebApplication3.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private readonly Admin200DbContext _db; + + //public WeatherForecastController(Admin200DbContext 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,Admin200DbContext dbContext) + { + _logger = logger; + _db = dbContext; + } + + [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).ToArray(); + + return JosnHelper.SerializeObject(res); + } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Admin200DbContext.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Admin200DbContext.cs" new file mode 100644 index 0000000..f994e97 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Admin200DbContext.cs" @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication3.Domain.Entity; + +namespace WebApplication3.Domain +{ + public class Admin200DbContext:DbContext + { + public Admin200DbContext() + { + + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "server=.;database=Admin200;uid=sa;pwd=123456;"; + optionsBuilder.UseSqlServer(connectionString); + } + public DbSet Users { get; set; } + public DbSet Roles { get; set; } + + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/BaseEntity.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/BaseEntity.cs" new file mode 100644 index 0000000..11cf97d --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/BaseEntity.cs" @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication3.Domain +{ + public class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDelete = false; + CreatedTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + public int Id { get; set; } + public bool IsActived { get; set; } + public bool IsDelete { get; set; } + public DateTime CreatedTime { get; set; } + public DateTime UpdatedTime { get; set; } + public int DisplauOtder { get; set; } + public string Remarks { get; set; } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/DbInitializer.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/DbInitializer.cs" new file mode 100644 index 0000000..756b6f8 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/DbInitializer.cs" @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication3.Domain.Entity; + +namespace WebApplication3.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using var db = new Admin200DbContext(); + db.Database.EnsureCreated(); + + var hasUser = db.Users.Any(); + if (!hasUser) + { + var role = new Roles + { + RoleName="你爹", + Description="没毛病,就是你爹" + }; + db.Roles.Add(role); + db.SaveChanges(); + db.Users.AddRange(new Users[] + { + new Users + { + Username="a", + password="1", + RolesId=role.Id + }, + new Users + { + Username="b", + password="2", + RolesId=role.Id + }, + new Users + { + Username="c", + password="3", + RolesId=role.Id + } + + }); + db.SaveChanges(); + } + } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Roles.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Roles.cs" new file mode 100644 index 0000000..65e5a63 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Roles.cs" @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace WebApplication3.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/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Users.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Users.cs" new file mode 100644 index 0000000..8a9e4f9 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Domain/Entity/Users.cs" @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication3.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/\346\235\216\347\233\233\345\277\227/WebApplication3/Helper/JosnHelper.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Helper/JosnHelper.cs" new file mode 100644 index 0000000..498f518 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Helper/JosnHelper.cs" @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication3.Helper +{ + public class JosnHelper + { + 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/\346\235\216\347\233\233\345\277\227/WebApplication3/Implementation/EfRespository.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Implementation/EfRespository.cs" new file mode 100644 index 0000000..5943cb6 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Implementation/EfRespository.cs" @@ -0,0 +1,68 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApplication3.Domain; +using WebApplication3.Interface; + +namespace WebApplication3.Implementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin200DbContext db; + + private DbSet entity; + public DbSet Table + { + get + { + if(entity == null) + { + entity = db.Set(); + } + return entity; + } + } + public EfRespository(Admin200DbContext dbContext) + { + db = dbContext; + } + public void Delete(T entity) + { + this.Table.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) + { + throw new NotImplementedException(); + } + + public IEnumerable GetTsAllEntity() + { + throw new NotImplementedException(); + } + + public int Insert(T entity) + { + throw new NotImplementedException(); + } + + public void InsertBulk(IEnumerable list) + { + throw new NotImplementedException(); + } + + public void Update(T entity) + { + throw new NotImplementedException(); + } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Interface/IRespository.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Interface/IRespository.cs" new file mode 100644 index 0000000..54af040 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Interface/IRespository.cs" @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApplication3.Interface +{ + interface IRespository + { + T GetById(int id); + IEnumerable GetTsAllEntity(); + + int Insert(T entity); + void InsertBulk(IEnumerable list); + void Update(T entity); + void Delete(T entity); + void Delete(int id); + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Program.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Program.cs" new file mode 100644 index 0000000..38709ab --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/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 WebApplication3 +{ + 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\235\216\347\233\233\345\277\227/WebApplication3/Properties/launchSettings.json" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Properties/launchSettings.json" new file mode 100644 index 0000000..78420ce --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51409", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApplication3": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/Startup.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Startup.cs" new file mode 100644 index 0000000..857d2ef --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/Startup.cs" @@ -0,0 +1,56 @@ +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 WebApplication3.Domain; + +namespace WebApplication3 +{ + 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.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/\346\235\216\347\233\233\345\277\227/WebApplication3/WeatherForecast.cs" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/WeatherForecast.cs" new file mode 100644 index 0000000..3be6e8b --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApplication3 +{ + 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\235\216\347\233\233\345\277\227/WebApplication3/WebApplication3.csproj" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/WebApplication3.csproj" new file mode 100644 index 0000000..09e58a3 --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/WebApplication3.csproj" @@ -0,0 +1,14 @@ + + + + netcoreapp3.1 + + + + + + + + + + diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.Development.json" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.Development.json" new file mode 100644 index 0000000..8983e0f --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.json" "b/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.json" new file mode 100644 index 0000000..d9d9a9b --- /dev/null +++ "b/\346\235\216\347\233\233\345\277\227/WebApplication3/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} -- Gitee