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 0000000000000000000000000000000000000000..a87a82e19722bc2e475326b410cdced705f1521b --- /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 0000000000000000000000000000000000000000..361041bcc55447e83d8f71b2cc97673fada53cbf --- /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 0000000000000000000000000000000000000000..3209bba849dee95b28b9005e65aba0d3a3734585 --- /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 0000000000000000000000000000000000000000..08d7e8c1c9cddb7e91007789a2d2723108e21f21 --- /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 0000000000000000000000000000000000000000..8961115b7ee322d355ae9839e92443958dcb8129 --- /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 0000000000000000000000000000000000000000..c0a388597d4facb02ee90987c686577f4b0a76b3 --- /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 0000000000000000000000000000000000000000..407ce66e371a700a3933c80bf69c514d0e63238e --- /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 0000000000000000000000000000000000000000..2cdb8ae0e2d5efadf00fa8c93b2d364a9ad1fe9a --- /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 0000000000000000000000000000000000000000..638ecf2d684205636dbd5c93cd2dbc282e44cc5f --- /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 0000000000000000000000000000000000000000..1888bd168d37f0af1a578d20c7466c36e33d5da5 --- /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 0000000000000000000000000000000000000000..d377a29670cb1cdd46f05fc35e61d3355c19cf6a --- /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 0000000000000000000000000000000000000000..9ee66407c05d354f21424bd9f63401a1443e8c9e --- /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 0000000000000000000000000000000000000000..9fba49ccf5153e4fe23d8bae3a65b3d412a6de22 --- /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 0000000000000000000000000000000000000000..14c7b87dfc1d748226435dc2276e8b52592300b2 --- /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 0000000000000000000000000000000000000000..e8d1db25b243f35d583499337779a581a88d94a4 --- /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 0000000000000000000000000000000000000000..da10d50a32b4ec10963d789edcdb8d045c0da572 --- /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 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /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 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /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": "*" +}