diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001.sln" "b/\345\275\255\350\210\222\345\251\267/WebApi001.sln" new file mode 100644 index 0000000000000000000000000000000000000000..f8a5582f2b0c531578c499277da0afba9f13721e --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001.sln" @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi001", "WebApi001\WebApi001.csproj", "{7294D209-D1AE-41D9-AC99-BF23065BD08F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7294D209-D1AE-41D9-AC99-BF23065BD08F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7294D209-D1AE-41D9-AC99-BF23065BD08F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7294D209-D1AE-41D9-AC99-BF23065BD08F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7294D209-D1AE-41D9-AC99-BF23065BD08F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {71F544B9-9AD2-4535-B35A-93EB9931C304} + EndGlobalSection +EndGlobal diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Controllers/WeatherForecastController.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5033c07d3d596adaff5c26920aa3ceb51c69b82b --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Controllers/WeatherForecastController.cs" @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using WebApi001.Domain; +using WebApi001.Domain.Entity; +using Microsoft.Extensions.DependencyInjection; +using System.Runtime.Serialization; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel; +using WebApi001.Domain.Helper; + +namespace WebApi001.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + //依赖注入 + //方法一 + private readonly Admin5000DbContext _db; + + [ActivatorUtilitiesConstructor] + public WeatherForecastController(Admin5000DbContext 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; + } + + //方法二:直接注释构造函数logger + + //方法三:将两个构造函数写在一起 + //public WeatherForecastController(ILogger logger,Admin5000DbContext _db) + //{ + // _logger = logger; + // _db = dbContext; + //} + [HttpGet] + public string Get() + { + //var res = _db.Products.Include(x=>x.Brand).ToList();加Include(x=>x.Brand.)就会有一大串的Brand值 + var res = _db.Products.ToList(); + //json 串行 + return JsonHelper.SerializeObject(res); + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Admin5000DbContext.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Admin5000DbContext.cs" new file mode 100644 index 0000000000000000000000000000000000000000..e7d781f1a506357a179542b9d4650e7252b79cba --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Admin5000DbContext.cs" @@ -0,0 +1,21 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi001.Domain.Entity; + +namespace WebApi001.Domain +{ + public class Admin5000DbContext:DbContext + { + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("server=.;database=Admin5000;uid=sa;pwd=123456;"); + } + public DbSet Products { get; set; } + + public DbSet Brands { get; set; } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/BaseEntity.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..36e1a7e62219b7d5659f0bd130d21694c6957dad --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/BaseEntity.cs" @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi001.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 string Remarks { get; set; } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/DbInitializeHelper.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/DbInitializeHelper.cs" new file mode 100644 index 0000000000000000000000000000000000000000..82398e3f030ffa49da079fbea03599fc0e49a0a3 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/DbInitializeHelper.cs" @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using WebApi001.Domain.Entity; + +namespace WebApi001.Domain +{ + public class DbInitializeHelper + { + public static void Initialize() + { + using (var db = new Admin5000DbContext()) + { + var dbExist = db.Database.EnsureCreated(); + + var tempBrand = db.Brands.Any(); + if (!tempBrand) + { + var brand = new Brand + { + BrandName = "杰克琼斯", + Description = "丹麦品牌" + }; + + db.Brands.Add(brand); + + db.SaveChanges(); + + db.Products.AddRange(new Product[] + { + new Product + { + ProductName="牛仔裤", + ShortDesc="潮流新款", + FullDesc="这是一个详情描述,敷衍", + BrandId=brand.Id + }, + new Product + { + ProductName="衬衣_白色", + ShortDesc="宽松休闲", + FullDesc="这是一个详情描述,敷衍+1", + BrandId=brand.Id + }, + }); + db.SaveChanges(); + } + + + } + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Brand.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Brand.cs" new file mode 100644 index 0000000000000000000000000000000000000000..afe5b70ee5a03605ff94c143fc431fc206ca4173 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Brand.cs" @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi001.Domain.Entity +{ + public class Brand:BaseEntity + { + public string BrandName { get; set; } + + public string Description { get; set; } + + public IEnumerable Products { get; set; } + + + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Product.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Product.cs" new file mode 100644 index 0000000000000000000000000000000000000000..dab49c8ebcb5c6b97c1f940046604c020639b636 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Entity/Product.cs" @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi001.Domain.Entity +{ + public class Product:BaseEntity + { + public string ProductName { get; set; } + + public string ShortDesc { get; set; } + + public string FullDesc { get; set; } + + public int BrandId { get; set; } + public Brand Brand { get; set; } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Helper/JsonHelper.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Helper/JsonHelper.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7e00c7664c3bd07f04e49e4428cc6aee1b948ac3 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Domain/Helper/JsonHelper.cs" @@ -0,0 +1,23 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi001.Domain.Helper +{ + public class JsonHelper + { + //序列化 + public static string SerializeObject(object obj) + { + //json 串行 + return JsonConvert.SerializeObject(obj, Formatting.Indented, + new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + DateFormatString = "yyyy-MM-dd HH-mm-ss" + }); + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Interface/IRespository.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Interface/IRespository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..011397b0d718012b31f592474b6b7f03ca226e3e --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Interface/IRespository.cs" @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi001.Interface +{ + interface IRespository + { + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Program.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..02f63b4327ea274bbb68e8ba9d2230d561da8e40 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/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 WebApi001 +{ + 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\275\255\350\210\222\345\251\267/WebApi001/Properties/launchSettings.json" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..2f6ea349cdd636bf05452d000f895c18ba15fdda --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56703", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WebApi001": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/Startup.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..e9d2eb39f27aeeeb66b5e0ff2ef98935cb33b6fa --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/Startup.cs" @@ -0,0 +1,54 @@ +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 WebApi001.Domain; + +namespace WebApi001 +{ + 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.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(); + }); + + DbInitializeHelper.Initialize(); + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/WeatherForecast.cs" "b/\345\275\255\350\210\222\345\251\267/WebApi001/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..f7e7578768d8494b9a8d1e6c5513003e3036bca9 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace WebApi001 +{ + 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\275\255\350\210\222\345\251\267/WebApi001/WebApi001.csproj" "b/\345\275\255\350\210\222\345\251\267/WebApi001/WebApi001.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..f4e07678e5e8d7820d3a592bac34b497fa73f215 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/WebApi001.csproj" @@ -0,0 +1,30 @@ + + + + netcoreapp3.1 + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.Development.json" "b/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.json" "b/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\345\275\255\350\210\222\345\251\267/WebApi001/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +}