diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001.sln" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001.sln" new file mode 100644 index 0000000000000000000000000000000000000000..24f9953bd9cc9e41faeda47f2b33432eac2e31ac --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001.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}") = "WebApiTest0001", "WebApiTest0001\WebApiTest0001.csproj", "{21935392-DA76-4C06-BF80-107678DEEC05}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {21935392-DA76-4C06-BF80-107678DEEC05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {21935392-DA76-4C06-BF80-107678DEEC05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {21935392-DA76-4C06-BF80-107678DEEC05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {21935392-DA76-4C06-BF80-107678DEEC05}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {758C21EC-78A4-4C56-895A-294CA7C55FD5} + EndGlobalSection +EndGlobal diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Animals.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Animals.cs" new file mode 100644 index 0000000000000000000000000000000000000000..dd02a551a91032584284a5cfe00d7ef91675c8be --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Animals.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest0001 +{ + 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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Controllers/TestController.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Controllers/TestController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..2e0d4f97bad802c6a2b0ccf762f0f57787da6ec4 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Controllers/TestController.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore.Migrations; +using WebApiTest0001.Domain.Entity; +using WebApiTest0001.Helper; +using WebApiTest0001.Interface; + +namespace WebApiTest0001.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); + } + } +} \ No newline at end of file diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Controllers/WeatherForecastController.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..cbcc51f1d14be8097354f96ec02334bf676efd97 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/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.Extensions.Logging; +using WebApiTest0001.Domain; +using WebApiTest0001.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Microsoft.EntityFrameworkCore; +using WebApiTest0001.Helper; + +namespace WebApiTest0001.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 JsonConvert.SerializeObject(res,Formatting.Indented,new JsonSerializerSettings { + // ReferenceLoopHandling=ReferenceLoopHandling.Ignore, + // DateFormatString="yyy-MM-dd HH:mm:ss" + //}); + return JsonHelper.SerializeObject(res); + } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Admin3200DbContext.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Admin3200DbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5f210be191a374497e21a8260586b5287a3e374c --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Admin3200DbContext.cs" @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest0001.Domain.Entity; + +namespace WebApiTest0001.Domain +{ + public class Admin3200DbContext : DbContext + { + public Admin3200DbContext() + { + + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + //与数据库建立连接 + var connectionString = "server=.;database=Admin0001;uid=sa;pwd=123456"; + optionsBuilder.UseSqlServer(connectionString); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/BaseEntity.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..da064a800d258e9c176428cf1975d432f5f893cb --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/BaseEntity.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest0001.Domain +{ + 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; } + + public int MyProperty { get; set; } + + public string Remarks { get; set; } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/DbInitializer.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/DbInitializer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b991da621c93b84d1ffd498ee4ffb731c1b0f6be --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/DbInitializer.cs" @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest0001.Domain.Entity; + +namespace WebApiTest0001.Domain +{ + public class DbInitializer + { + public static void Seed() + { + using var db = new Admin3200DbContext(); + 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="hhm", + Password="6668", + RolesId=role.Id + }, + new Users + { + Username="zxy", + Password="99998", + RolesId=role.Id + } + }); + + db.SaveChanges(); + + } + } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Roles.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..da7c6e829101b9156512d5cb00277456b9678603 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Roles.cs" @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace WebApiTest0001.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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Users.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..e6019ae9cf669cc58e6f8ec6e176492500840098 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Domain/Entity/Users.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest0001.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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Helper/JsonHelper.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Helper/JsonHelper.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7dfb2a6b87ec897aa41c541cd8f65d464b264353 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Helper/JsonHelper.cs" @@ -0,0 +1,21 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest0001.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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Implementation/EfRespository.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Implementation/EfRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b342f34aeaab4f727146755f30d07001bf98505b --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Implementation/EfRespository.cs" @@ -0,0 +1,82 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApiTest0001.Domain; +using WebApiTest0001.Interface; + +namespace WebApiTest0001.Implementation +{ + 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(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 IEnumerable GetAllEntity() + //{ + // throw new NotImplementedException(); + //} + + 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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Interface/IRespository.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Interface/IRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..758e7e3bb9e97882b589c18e654b50071403acd8 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Interface/IRespository.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApiTest0001.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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Program.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3e8e8b5e67339ab355bb46f747f5f77922405dbf --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/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 WebApiTest0001 +{ + 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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Properties/launchSettings.json" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..7117dbd8684a646ee999ff404ccb869ef82e3438 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:52806", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApiTest0001": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Startup.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..ca983966b6b1399ae36d2f0e73634fa0543968c6 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/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 WebApiTest0001.Domain; +using WebApiTest0001.Implementation; +using WebApiTest0001.Interface; + +namespace WebApiTest0001 +{ + 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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WeatherForecast.cs" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..17b2a7c7ada4618e7c5ea3934c93972bb619ef73 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApiTest0001 +{ + 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/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WebApiTest0001.csproj" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WebApiTest0001.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..313f906681683ad2d2116c5d1e80da191d6b2639 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/WebApiTest0001.csproj" @@ -0,0 +1,15 @@ + + + + netcoreapp3.1 + + + + + + + + + + + diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.Development.json" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.json" "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\351\273\204\346\205\247\346\225\217/WebApiTest0001/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}