diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001.sln" "b/\345\221\250\344\270\200\345\270\206/DIText001.sln" new file mode 100644 index 0000000000000000000000000000000000000000..9f8612f64b71a234f1c1b660f2bf4265c5ec40e4 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001.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}") = "DIText001", "DIText001\DIText001.csproj", "{FC7570BD-1FAD-496C-A92C-3D8383FF2A2F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FC7570BD-1FAD-496C-A92C-3D8383FF2A2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FC7570BD-1FAD-496C-A92C-3D8383FF2A2F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FC7570BD-1FAD-496C-A92C-3D8383FF2A2F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FC7570BD-1FAD-496C-A92C-3D8383FF2A2F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {808919FA-4500-46A5-BAF2-1F914CC1BB7E} + EndGlobalSection +EndGlobal diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/TextController.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/TextController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..85d91c4535b3f93e7187ae00cbefd7fce6a2e925 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/TextController.cs" @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DIText001.DOMain.Entity; +using DIText001.Helper; +using DIText001.Interface; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace DIText001.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TextController : ControllerBase + { + private readonly IRespository _userRepository; + public TextController(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/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/WeatherForecastController.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fe7f1c644f0071b9e285943b5d6d2474b335de4c --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Controllers/WeatherForecastController.cs" @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using DIText001.DOMain; +using DIText001.DOMain.Entity; +using Newtonsoft.Json; +using Microsoft.EntityFrameworkCore; +using DIText001.Helper; + +namespace DIText001.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private readonly Admin3000Context _db; + //[ActivatorUtilitiesConstructor] + //public WeatherForecastController(Admin3000Context 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,Admin3000Context 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/\345\221\250\344\270\200\345\270\206/DIText001/DIText001.csproj" "b/\345\221\250\344\270\200\345\270\206/DIText001/DIText001.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..37cf19e372463070d640aabced7f568649e9e823 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DIText001.csproj" @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Admin3000Context.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Admin3000Context.cs" new file mode 100644 index 0000000000000000000000000000000000000000..85e0b7dfc2fa1f2d55981978e9a1eadafe07a8cc --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Admin3000Context.cs" @@ -0,0 +1,21 @@ +using DIText001.DOMain.Entity; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.DOMain +{ + public class Admin3000Context:DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("server=.;database=Admin3000;uid=sa;pwd=123456"); + } + + public DbSet Users { get; set; } + + public DbSet Roles { get; set; } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/BaseEntity.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7a9197f820934deb6ae305f518015e6aa5c7b5a0 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/BaseEntity.cs" @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.DOMain +{ + public abstract class BaseEntity + { + public BaseEntity() + { + IsActived = true; + IsDeleted = true; + 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 string Remarks { get; set; } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Dbinitializer.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Dbinitializer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..8842e2256e04d795e275e83cb9e6d1ccaac4e5ce --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Dbinitializer.cs" @@ -0,0 +1,47 @@ +using DIText001.DOMain.Entity; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.DOMain +{ + public class Dbinitializer + { + public static void Seed() + { + using (var db = new Admin3000Context()) + { + db.Database.EnsureCreated(); + var hasUser = db.Users.Any(); + if (!hasUser) + { + var role1 = new Roles + { + RoleName = "xxx", + Description = "aabb" + }; + db.Roles.Add(role1); + db.SaveChanges(); + db.Users.AddRange(new Users[] + { + new Users + { + Username="xxx", + Password="123456", + RoleId=role1.Id + + }, + new Users + { + Username="xxx", + Password="123456", + RoleId=role1.Id + } + }); + } + db.SaveChanges(); + } + } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Roles.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..023673cba54016334c96bb1f45b655c95dccb0ca --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Roles.cs" @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.DOMain.Entity +{ + public class Roles:BaseEntity + { + public string RoleName { get; set; } + + public string Description { get; set; } + + public IEnumerable Users { get; set; } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Users.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..21151645630a2dd5fb21dd1dec5b5d9be4a133d4 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/DOMain/Entity/Users.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.DOMain.Entity +{ + public class Users:BaseEntity + { + public string Username { get; set; } + + public string Password { get; set; } + + public int RoleId { get; set; } + + public Roles Roles { get; set; } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/Helper/JsonHelper.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Helper/JsonHelper.cs" new file mode 100644 index 0000000000000000000000000000000000000000..41508da267cbddee81fb73c8574a35d72025f783 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Helper/JsonHelper.cs" @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.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/\345\221\250\344\270\200\345\270\206/DIText001/Implementation/EfRespository.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Implementation/EfRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..094b105daebf0eade65792d7676e919024523175 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Implementation/EfRespository.cs" @@ -0,0 +1,79 @@ +using DIText001.DOMain; +using DIText001.Interface; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.Implementation +{ + public class EfRespository : IRespository where T : BaseEntity + { + private readonly Admin3000Context db; + private DbSet _entity; + + protected DbSet Entity { get + { + if (_entity==null) + { + _entity = db.Set(); + } + return _entity; + } + } + public IQueryable Table + { + get + { + + return Entity; + } + } + public EfRespository(Admin3000Context 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); + db.SaveChanges(); + } + + + + public T GetById(int id) + { + return _entity.Where(x => x.Id == id).FirstOrDefault(); + db.SaveChanges(); + } + + 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/\345\221\250\344\270\200\345\270\206/DIText001/Interface/IRespository.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Interface/IRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..6a4b9d9c4d42c5120e17dc891626ac8896bd5880 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Interface/IRespository.cs" @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DIText001.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/\345\221\250\344\270\200\345\270\206/DIText001/Program.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d3a6f46f00fff521fc2f6ce314713fb5b6d9dd9c --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/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 DIText001 +{ + 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/\345\221\250\344\270\200\345\270\206/DIText001/Properties/launchSettings.json" "b/\345\221\250\344\270\200\345\270\206/DIText001/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..bf4587871407c1220bb28e3a1f098c53de6c8746 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:49802", + "sslPort": 44368 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "DIText001": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/Startup.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..2306afcf77be787987bd6932226a532a1bc52ec8 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/Startup.cs" @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DIText001.DOMain; +using DIText001.Implementation; +using DIText001.Interface; +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.Hosting; +using Microsoft.Extensions.Logging; + +namespace DIText001 +{ + 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.UseHttpsRedirection(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + Dbinitializer.Seed(); + } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/WeatherForecast.cs" "b/\345\221\250\344\270\200\345\270\206/DIText001/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5dcce5d817629f8e16cdc87ada4c2c1bf0e60640 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace DIText001 +{ + 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/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.Development.json" "b/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.json" "b/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\345\221\250\344\270\200\345\270\206/DIText001/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}