diff --git "a/\350\256\270\345\230\211/WebApi0003.sln" "b/\350\256\270\345\230\211/WebApi0003.sln" new file mode 100644 index 0000000000000000000000000000000000000000..c3e1c2939cf04e043bf248cdedfe5f998556b4bc --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30011.22 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi0003", "WebApi0003\WebApi0003.csproj", "{17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {17DFBBDC-5C5D-4823-BE08-1A1C19ABD5F8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {97A5B6B1-659A-4BF8-813C-35777A3D8512} + EndGlobalSection +EndGlobal diff --git "a/\350\256\270\345\230\211/WebApi0003/Animals.cs" "b/\350\256\270\345\230\211/WebApi0003/Animals.cs" new file mode 100644 index 0000000000000000000000000000000000000000..4fad45dc3284f4d5fdd65242abaf1c8ee1999506 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Animals.cs" @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003 +{ + public class Animals + { + public virtual void Eat() + { + Console.WriteLine("动物世界"); + } + } + public class Dog : Animals + { + public override void Eat() + { + Console.WriteLine("小狗狗,听话"); + } + } + + public class Cat : Animals + { + public override void Eat() + { + Console.WriteLine("小猫咪,咪咪咪"); + } + } + + +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Controllers/Domain/Entity/BaesEntity.cs" "b/\350\256\270\345\230\211/WebApi0003/Controllers/Domain/Entity/BaesEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..05bbcfe243dd2f6518c7eb5844b461c083f5b2f2 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Controllers/Domain/Entity/BaesEntity.cs" @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Controllers.Domain.Entity +{ + public class BaesEntity + { + + public BaesEntity() + { + IsActived = true; + IsDeled = false; + CreateTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + public int Id { get; set; } + + public bool IsActived { get; set; } + + public bool IsDeled { get; set; } + + public DateTime CreateTime { get; set; } + + public DateTime UpdatedTime { get; set; } + + public int DisplayOrder { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" "b/\350\256\270\345\230\211/WebApi0003/Controllers/TestController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5a9dd60f8ea5f7e90b9626c86a51d7edce3243a8 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/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 WebApi0003.Domain.Entity; +using WebApi0003.Helper; +using WebApi0003.Helper.imolementation; + +namespace WebApi0003.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : ControllerBase + { + private readonly IRespository _usersRepository; + + public TestController(IRespository usersRepository) + { + _usersRepository = usersRepository; + } + public string Get() + { + var list = _usersRepository.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + } +} \ No newline at end of file diff --git "a/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" "b/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7e968a720724fb29bab465fa2a7c9826bbe33ac0 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Controllers/WeatherForecastController.cs" @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using WebApi0003.Domain; +using WebApi0003.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using System.Data.Common; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using WebApi0003.Helper; + +namespace WebApi0003.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + + private readonly Admin3200DbContext _db; + + //[ActivatorUtilitiesConstructor] + //public WeatherForecastController(Admin3200DbContext 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,Admin3200DbContext 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).ToList(); + + return JsonHelper.SerializeObject(res); + } + } + } + diff --git "a/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" "b/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fd8f1e566e7d57161fcd63f1836c884ca1ea6958 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Domain/Admin3200DbContext.cs" @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain.Entity; + +namespace WebApi0003.Domain +{ + public class Admin3200DbContext:DbContext + { + public Admin3200DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + var connectionString = "server=.;database=Admin3200;uid=sa;pwd=123456;"; + optionsBuilder.UseSqlServer(connectionString); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" "b/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7fe5dddf1fc39cb40aaefb652368d84b82de6d40 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Domain/BaseEntity.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Domain +{ + public class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDeled = false; + CreateTime = DateTime.Now; + UpdatedTime = DateTime.Now; + } + public int Id { get; set; } + + public bool IsActived { get; set; } + + public bool IsDeled { get; set; } + + public DateTime CreateTime { get; set; } + + public DateTime UpdatedTime { get; set; } + + public int DisplayOrder { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" "b/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..bfa036bb0ccd80ba3d0342e2d3ba524ae570e46c --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Domain/DbInitializer.cs" @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain.Entity; + +namespace WebApi0003.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using var db = new Admin3200DbContext(); + 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="abd", + Password="123456", + RolesId=role.Id + }, + new Users + { + Username="abd01", + Password="abcdef", + RolesId=role.Id + } + }); + db.SaveChanges(); + } + } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" "b/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..078daf45f203ae80cf4f39ddb8fd426d35150cc9 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Roles.cs" @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace WebApi0003.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\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" "b/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..a756cc187eba6b56cc490138a1b1b0cf493ee3e7 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Domain/Entity/Users.cs" @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.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\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" "b/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" new file mode 100644 index 0000000000000000000000000000000000000000..38e63dd7b0bef20d20f9e91a478215ce3f97bceb --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Helper/JsonHelper.cs" @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Helper +{ + public class JsonHelper + { + public static string SerializeObject(object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyy-MM-dd HH:mm:ss" + }); + } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" "b/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..522cc2ec626d104c919232b145576a4caba7deb1 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/EfRespository.cs" @@ -0,0 +1,79 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi0003.Domain; + +namespace WebApi0003.Helper.imolementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin3200DbContext db; + + private DbSet _entity; + + protected DbSet Entity { get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + public IQueryable Table + { + get + { + return Entity; + } + } + + + public EfRespository(Admin3200DbContext dbContext) + { + db=dbContext; + } + public void Delete(int id) + { + var row= Table.Where(x => x.Id == id).FirstOrDefault(); + Delete(row); + } + + public void Delete(T entity) + { + this._entity.Remove(entity); + + db.SaveChanges(); + } + + + + public T GetById(int id) + { + return _entity.Where(x => x.Id == id).FirstOrDefault(); + } + + public void InserBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + + public void Update(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/IRespository.cs" "b/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/IRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3bbc918da35d58382a38a10aa3a753c0f39335fa --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Helper/imolementation/IRespository.cs" @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0003.Helper.imolementation +{ + public interface IRespository + { + IQueryable Table { get; } + T GetById(int id); + + void Insert(T entity); + + void InserBulk(IEnumerable list); + + void Update(T entity); + + void Delete(T entity); + + void Delete(int id); + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Program.cs" "b/\350\256\270\345\230\211/WebApi0003/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..2add2e748eee901e6e1a8980d34ba884ceca2541 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/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 WebApi0003 +{ + 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\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" "b/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..e08633a4592e8853652d7013868e42d20ba1b3b9 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:36839", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApi0003": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/Startup.cs" "b/\350\256\270\345\230\211/WebApi0003/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d8199afb5cf3567b8082ee7f0da1043cb8766f96 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/Startup.cs" @@ -0,0 +1,55 @@ +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 WebApi0003.Domain; +using WebApi0003.Helper.imolementation; + +namespace WebApi0003 +{ + 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\256\270\345\230\211/WebApi0003/WeatherForecast.cs" "b/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..ee032e7d726adad244272990dc3954ec966a119e --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApi0003 +{ + 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\256\270\345\230\211/WebApi0003/WebApi0003.csproj" "b/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..aa6526276ddf541a81d7d198955d2e0117dc1a8f --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/WebApi0003.csproj" @@ -0,0 +1,26 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" "b/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\350\256\270\345\230\211/WebApi0003/appsettings.json" "b/\350\256\270\345\230\211/WebApi0003/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\350\256\270\345\230\211/WebApi0003/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}