From be20caa528e8e63c427d42e4451bc4c3455dec5c Mon Sep 17 00:00:00 2001 From: yang_mr <3066678383@qq.com> Date: Mon, 8 Jun 2020 23:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=A8=E9=93=AD=E5=A6=82=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webapi.sln" | 25 ++++++ .../webapi/Controllers/DominController.cs" | 30 +++++++ .../webapi/Controllers/ValuesController.cs" | 71 ++++++++++++++++ .../webapi/Domin/Admin977Context.cs" | 25 ++++++ .../webapi/Domin/BaseEntity.cs" | 28 +++++++ .../webapi/Domin/Dblnitlizer.cs" | 38 +++++++++ .../webapi/Domin/Entity/Roles.cs" | 14 ++++ .../webapi/Domin/Entity/Users.cs" | 15 ++++ .../webapi/Helper/JsonHelper.cs" | 22 +++++ .../webapi/Implement/EfRespository.cs" | 83 +++++++++++++++++++ .../webapi/Interface/IRespository.cs" | 19 +++++ .../webapi/Program.cs" | 24 ++++++ .../webapi/Properties/launchSettings.json" | 30 +++++++ .../webapi/Startup.cs" | 52 ++++++++++++ .../webapi/appsettings.Development.json" | 9 ++ .../webapi/appsettings.json" | 8 ++ .../webapi/webapi.csproj" | 19 +++++ 17 files changed, 512 insertions(+) create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi.sln" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Controllers/DominController.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Controllers/ValuesController.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Domin/Admin977Context.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Domin/BaseEntity.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Domin/Dblnitlizer.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Roles.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Users.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Helper/JsonHelper.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Implement/EfRespository.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Interface/IRespository.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Program.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Properties/launchSettings.json" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/Startup.cs" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/appsettings.Development.json" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/appsettings.json" create mode 100644 "\346\235\250\351\223\255\345\246\202/webapi/webapi.csproj" diff --git "a/\346\235\250\351\223\255\345\246\202/webapi.sln" "b/\346\235\250\351\223\255\345\246\202/webapi.sln" new file mode 100644 index 0000000..d0736bf --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.1082 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "webapi", "webapi\webapi.csproj", "{DF8E4E92-162B-41FC-A8F5-B707D9F31FC8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DF8E4E92-162B-41FC-A8F5-B707D9F31FC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DF8E4E92-162B-41FC-A8F5-B707D9F31FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DF8E4E92-162B-41FC-A8F5-B707D9F31FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DF8E4E92-162B-41FC-A8F5-B707D9F31FC8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B98A6A34-771C-4699-B5A0-89F8E3FEFB5D} + EndGlobalSection +EndGlobal diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Controllers/DominController.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Controllers/DominController.cs" new file mode 100644 index 0000000..8a1a232 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Controllers/DominController.cs" @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using webapi.Domin.Entity; +using webapi.Helper; +using webapi.Interface; + +namespace webapi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DominController : ControllerBase + { + private readonly IRespository _userRespository; + + public DominController(IRespository userRespository) + { + _userRespository = userRespository; + } + + public string Get() + { + var list = _userRespository.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + } +} \ No newline at end of file diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Controllers/ValuesController.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Controllers/ValuesController.cs" new file mode 100644 index 0000000..7b25e1a --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Controllers/ValuesController.cs" @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using webapi.Domin; +using webapi.Helper; + +namespace webapi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ValuesController : ControllerBase + { + + private readonly Admin977Context _db; + [ActivatorUtilitiesConstructor] + + + public ValuesController(Admin977Context dbContext) + { + _db = dbContext; + } + + //GET api/values + [HttpGet] + public string Get() + { + var res = _db.Users.Include(x => x.Roles).ToList(); + + return JsonHelper.SerializeObject(res); + } + + + + + //// GET api/values + //[HttpGet] + //public ActionResult> Get() + //{ + // return new string[] { "value1", "value2" }; + //} + + //// GET api/values/5 + //[HttpGet("{id}")] + //public ActionResult Get(int id) + //{ + // return "value"; + //} + + //// POST api/values + //[HttpPost] + //public void Post([FromBody] string value) + //{ + //} + + //// PUT api/values/5 + //[HttpPut("{id}")] + //public void Put(int id, [FromBody] string value) + //{ + //} + + //// DELETE api/values/5 + //[HttpDelete("{id}")] + //public void Delete(int id) + //{ + //} + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Domin/Admin977Context.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Admin977Context.cs" new file mode 100644 index 0000000..a0d8727 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Admin977Context.cs" @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using webapi.Domin.Entity; + +namespace webapi.Domin +{ + public class Admin977Context:DbContext + { + public Admin977Context() + { + + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var con = "server=.;database=Admin977;uid=sa;pwd=123456"; + optionsBuilder.UseSqlServer(con); + } + + public DbSet Users { get; set; } + public DbSet Roles { get; set; } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Domin/BaseEntity.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/BaseEntity.cs" new file mode 100644 index 0000000..b8046a1 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/BaseEntity.cs" @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace webapi.Domin +{ + 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 DateTime CreatedTime { get; set; } + + public DateTime UpdatedTime { get; set; } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Domin/Dblnitlizer.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Dblnitlizer.cs" new file mode 100644 index 0000000..49b3a9d --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Dblnitlizer.cs" @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using webapi.Domin.Entity; + +namespace webapi.Domin +{ + public class Dblnitlizer + { + public static void Seed() + { + using(Admin977Context db=new Admin977Context()) + { + db.Database.EnsureCreated(); + var tepUser = db.Users.Any(); + if (!tepUser) + { + var roles = new Roles + { + RolesName = "有幸", + Description = "原来章鱼也有心脏", + }; + db.Roles.Add(roles); + db.SaveChanges(); + + db.Users.Add(new Users + { + UserName = "rmy", + Password="654321", + RolesId=roles.Id + }); + db.SaveChanges(); + } + } + } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Roles.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Roles.cs" new file mode 100644 index 0000000..0127b90 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Roles.cs" @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace webapi.Domin.Entity +{ + public class Roles:BaseEntity + { + public string RolesName { get; set; } + public string Description { get; set; } + public IEnumerable Users { get; set; } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Users.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Users.cs" new file mode 100644 index 0000000..f52524d --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Domin/Entity/Users.cs" @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace webapi.Domin.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\250\351\223\255\345\246\202/webapi/Helper/JsonHelper.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Helper/JsonHelper.cs" new file mode 100644 index 0000000..1de1bec --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Helper/JsonHelper.cs" @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace webapi.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/\346\235\250\351\223\255\345\246\202/webapi/Implement/EfRespository.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Implement/EfRespository.cs" new file mode 100644 index 0000000..3f49c1c --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Implement/EfRespository.cs" @@ -0,0 +1,83 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using webapi.Domin; +using webapi.Interface; + +namespace webapi.Implement +{ + public class EfRespositoryv : IRespository where T : BaseEntity + { + + private readonly Admin977Context db; + + + private DbSet _entity; + + private DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + + public IQueryable Table + { + get + { + + return Entity; + } + } + + public EfRespositoryv(Admin977Context 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 InserBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Update(T entity) + { + _entity.Update(entity); + db.SaveChanges(); + } + + + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Interface/IRespository.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Interface/IRespository.cs" new file mode 100644 index 0000000..86e9df1 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Interface/IRespository.cs" @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace webapi.Interface +{ + public interface IRespository + { + IQueryable Table { get; } + T GetById(int id); + + void Insert(T entity); + void InserBulk(IEnumerablelist); + void Update(T entity); + void Delete(T entity); + void Delete(int id); + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Program.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Program.cs" new file mode 100644 index 0000000..b8d8fb4 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Program.cs" @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace webapi +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Properties/launchSettings.json" "b/\346\235\250\351\223\255\345\246\202/webapi/Properties/launchSettings.json" new file mode 100644 index 0000000..a33d7d7 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:59000", + "sslPort": 44351 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "api/values", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "webapi": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/values", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/Startup.cs" "b/\346\235\250\351\223\255\345\246\202/webapi/Startup.cs" new file mode 100644 index 0000000..71ac17e --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/Startup.cs" @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using webapi.Domin; + +namespace webapi +{ + 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddDbContext(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + /* + else + { + app.UseHsts(); + } + + //app.UseHttpsRedirection(); + */ + Dblnitlizer.Seed(); + app.UseMvc(); + } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/appsettings.Development.json" "b/\346\235\250\351\223\255\345\246\202/webapi/appsettings.Development.json" new file mode 100644 index 0000000..e203e94 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/appsettings.json" "b/\346\235\250\351\223\255\345\246\202/webapi/appsettings.json" new file mode 100644 index 0000000..def9159 --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/appsettings.json" @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git "a/\346\235\250\351\223\255\345\246\202/webapi/webapi.csproj" "b/\346\235\250\351\223\255\345\246\202/webapi/webapi.csproj" new file mode 100644 index 0000000..b3f7fdc --- /dev/null +++ "b/\346\235\250\351\223\255\345\246\202/webapi/webapi.csproj" @@ -0,0 +1,19 @@ + + + + netcoreapp2.1 + + + + + + + + + + + + + + + -- Gitee