diff --git "a/\351\273\204\345\256\207\347\205\214/6.22 WebApi\347\224\237\346\210\220.md" "b/\351\273\204\345\256\207\347\205\214/6.22 WebApi\347\224\237\346\210\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d25b2abc591f4bfd66c6b5aa5c5db3d972c8e38 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/6.22 WebApi\347\224\237\346\210\220.md" @@ -0,0 +1,53 @@ +# 使用dotnet创建webapi项目 + +``` +dotnet new 创建命令 +-n|-name 设置创建目录/文件 的名称 +add 将包或引用添加到 .NET 项目。 +sln 修改 Visual Studio 解决方案文件 +classlib 表示类库的模板 +--no-https 取消https协议 +``` + + +## 生成项目 + +1. 创建webapi解决方案 + + ``` + dotnet new sln + ``` + +2. 创建webapi项目 + + ``` + // -n 是生成项目名称 + dotnet new webapi -n 项目名 --no-https + ``` + +3. 运行项目 + + ``` + // -p 要运行的项目文件的路径(如果只有一个项目,则默认使用当前目录)。 + dotnet run -p 项目文件夹 + ``` + +4. 将项目添加到解决方案 + + ``` + dotnet sln add 项目文件夹 + ``` + + ++ 创建类库 + + ``` + dotnet new classlib -n 文件名.Entity + ``` + ++ 将项目与类库添加到解决方案中,需要指定添加到哪个解决方案 + + ``` + dotnet sln 文件名.sln add 文件名.webapi + dotnet sln 文件名.sln add 文件名.Entity + ``` \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/6.23 WebApi-RESTful.md" "b/\351\273\204\345\256\207\347\205\214/6.23 WebApi-RESTful.md" new file mode 100644 index 0000000000000000000000000000000000000000..fcc496b5584b699c90dad8dcb7f7a3f417673049 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/6.23 WebApi-RESTful.md" @@ -0,0 +1,20 @@ +# WebApi-RESTful + +## 安装REST CLient + +VSCode 安装REST Client 插件 + +## 控制器中用法 +``` +// GET +[HttpGet("{id}")] + +// POST +[HttpPost] + +// PUT +[HttpPut("{id}")] + +// Delete +[HttpDelete("{id}")] +``` \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/6.26 WebApi\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\351\273\204\345\256\207\347\205\214/6.26 WebApi\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..b784b0fbf918eb132729a78adcc2ff1d161ee967 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/6.26 WebApi\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,76 @@ +# WebApi 连接数据库 + +## 安装 Entity Framework Core + +``` +// sqlserver +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver + +// sqlite +dotnet add package Microsoft.EntityFrameworkCore.Sqlite +``` + +## 数据库迁移 + +``` +// 安装dotnet-ef +dotnet tool install --global dotnet-ef + +// 安装EFCore.Design +dotnet add package Microsoft.EntityFrameworkCore.Design + +// 创建迁移,指示 EF Core 创建名为 InitialCreate 的迁移 +dotnet ef migrations add InitialCreate + +// 创建数据库和架构 +dotnet ef database update +``` + +### Entity 部分配置演示 + +``` +using System; + +namespace MyApi.Api.Entity +{ + public class Users{ + + public Users(){ + CreateTime=DateTime.Now; + UpdateTime=DateTime.Now; + } + public int Id{get;set;} + public String UserName{get;set;} + public string PassWord{get;set;} + public DateTime CreateTime{get;set;} + public DateTime UpdateTime{get;set;} + } +} +``` + +### Data 部分配置演示 + +``` +// 引用EFCore +using Microsoft.EntityFrameworkCore; +// 引用实体 +using MyApi.Api.Entity; + +namespace MyApi.Api.Db +{ + public class ShowDb : DbContext + { + // 实体中的类,将要在数据库中生成的表 + public DbSet Users { get; set; } + + // SqlSever配置用法 + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + + // Sqlite配置用法 + // protected override void OnConfiguring(DbContextOptionsBuilder options) + // => options.UseSqlite(@"Data Source=C:\blogging.db"); + + } +} +``` diff --git "a/\351\273\204\345\256\207\347\205\214/6.29 WebApi\345\260\201\350\243\205\350\241\250\345\273\272\347\253\213\345\205\263\347\263\273.md" "b/\351\273\204\345\256\207\347\205\214/6.29 WebApi\345\260\201\350\243\205\350\241\250\345\273\272\347\253\213\345\205\263\347\263\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..1cf921162d02df40692b52327062369aec8c4cf0 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/6.29 WebApi\345\260\201\350\243\205\350\241\250\345\273\272\347\253\213\345\205\263\347\263\273.md" @@ -0,0 +1,91 @@ +# WebApi封装表建立关系 + +## 封装表 +``` +using System; + +namespace MyApi.Api.Entity +{ + // abstract只能被继承 + public abstract class BaseEntity + { + // 一些主要表字段 + + // Id + 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 DisplayOrder { get; set; } + // 备注 + public string Remarks { get; set; } + } +} +``` + +## 表关系 + +Users表 + +``` +using System; + +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class Users:BaseEntity + { + // 用户名 + public String UserName { get; set; } + // 用户密码 + public string PassWord { get; set; } + } +} +``` + +UserRoles表 + +``` +using System; + +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class UserRoles:BaseEntity + { + // 用户Id + public int UserId { get; set; } + // 角色Id + public int RoleId { get; set; } + + public virtual Users User { get; set; } + + public virtual Roles Role { get; set; } + } +} +``` + +Roles表 + +``` +using System; +using System.Collections.Generic; + +namespace MyApi.Api.Entity +{ + // 继承BaseEntity + public class Roles:BaseEntity + { + // 角色名称 + public string RoleName { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + } +} +``` \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/6.30 WebApi\345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\351\273\204\345\256\207\347\205\214/6.30 WebApi\345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..138fbf21f955c671ffd30954db8f01c429a0299d --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/6.30 WebApi\345\260\201\350\243\205\346\216\245\345\217\243\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,194 @@ +# WebApi封装接口增删改查 + +## IRepository 配置 + +``` +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace MyApi.Api.Repository +{ + public interface IRepository + { + IQueryable Table { get; } + + /// + /// 根据Id获取指定实体 + /// + /// + /// + T GetById(int id); + + + /// + /// 使用实体对象,插入数据 + /// + /// 需要插入的对象 + void Insert(T entity); + + + /// + /// 使用实体对象,插入数据(异步) + /// + /// 需要插入的对象 + Task InsertAsync(T entity); + + + /// + /// 根据对象更新数据 + /// + /// 要更新的对象 + void Update(T entity); + + + /// + /// 根据对象更新数据(异步) + /// + /// 要更新的对象 + /// + Task UpdateAsync(T entity); + + + /// + /// 根据Id删除对应的记录 + /// + /// 主键id + void Delete(int id); + + } +} +``` + +## EFRepository配置 + +``` +using System; +using System.Threading.Tasks; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; +using MyApi.Api.Db; + +namespace MyApi.Api.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + /// + /// 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + /// + /// + private ShowDb db = new ShowDb(); + + + /// + /// 一个字段成员,用于内部Entity属性 + /// + private DbSet _entity; + + + /// + /// 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + /// + /// + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + + + /// + /// 代表一个可以用于查询T类型的表 + /// + /// + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + + /// + /// 删除(指定T类型,在数据库当中代表指定的表)指定Id的记录 + /// + /// + public void Delete(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + Entity.Remove(t); + db.SaveChanges(); + } + + public T GetById(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + return t; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + db.SaveChanges(); + } + + public async Task InsertAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await db.SaveChangesAsync(); + } + + public void Update(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + db.SaveChanges(); + } + + public async Task UpdateAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + await db.SaveChangesAsync(); + } + } +} +``` \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/7.2 WebApi\345\256\271\345\231\250.md" "b/\351\273\204\345\256\207\347\205\214/7.2 WebApi\345\256\271\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..f50b54b5e28d22cd169ec830f8133af941c42e79 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/7.2 WebApi\345\256\271\345\231\250.md" @@ -0,0 +1,337 @@ +# WebApi容器 + +## Startup配置 + +``` +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 Microsoft.EntityFrameworkCore; +using MyApi.Api.Repository; + +namespace MyApi.Api +{ + 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(o=>o.UseSqlServer()); + // 注册对数据库的基本操作服务到容器 + services.AddScoped(typeof(IRepository<>),typeof(EFRepository<>)); + + 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(); + }); + } + } +} +``` + + + +# 使用容器修改 + +## Data配置 + +``` +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; + +namespace MyApi.Api.Db +{ + public class ShowDb : DbContext + { + // 因为我们使用AddDbContext到容器,所以此处必须得有带参数的构造函数(而且必须传递DbContextOptions类型的参数,同时父类也得调用这个参数) + public ShowDb(DbContextOptions options) : base(options) + { + + } + public DbSet Users { get; set; } + public DbSet UserRoles { get; set; } + public DbSet Roles { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + } +} + +``` + +## IRepository 配置 + +``` +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; + +namespace MyApi.Api.Repository +{ + public interface IRepository + { + IQueryable Table { get; } + + /// + /// 根据Id获取指定实体 + /// + /// + /// + T GetById(int id); + + + /// + /// 使用实体对象,插入数据 + /// + /// 需要插入的对象 + void Insert(T entity); + + + /// + /// 使用实体对象,插入数据(异步) + /// + /// 需要插入的对象 + Task InsertAsync(T entity); + + /// + /// 使用实体对象,插入多条数据 + /// + /// 待插入的若干实体数据 + void InsertBulk(IEnumerable entities); + /// + /// 使用实体对象,插入多条数据(异步) + /// + /// 待插入的若干实体数据 + Task InsertBulkAsync(IEnumerable entities); + + + /// + /// 根据对象更新数据 + /// + /// 要更新的对象 + void Update(T entity); + + + /// + /// 根据对象更新数据(异步) + /// + /// 要更新的若干个实体 + /// + void UpdateBulk(IEnumerable entities); + + + /// + /// 根据Id删除对应的记录 + /// + /// 主键id + void Delete(int id); + } +} +``` + +## EFRepository配置 + +``` +using System; +using System.Threading.Tasks; +using System.Linq; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; +using MyApi.Api.Db; + +namespace MyApi.Api.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + /// + /// 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + /// + /// + // private ShowDb _db = new ShowDb(); + private ShowDb _db; + + public EFRepository(ShowDb db) + { + _db = db; + } + + + /// + /// 一个字段成员,用于内部Entity属性 + /// + private DbSet _entity; + + + /// + /// 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + /// + /// + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = _db.Set(); + } + return _entity; + } + } + + + /// + /// 代表一个可以用于查询T类型的表 + /// + /// + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + + /// + /// 删除(指定T类型,在数据库当中代表指定的表)指定Id的记录 + /// + /// + public void Delete(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + Entity.Remove(t); + _db.SaveChanges(); + } + + public T GetById(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + return t; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + _db.SaveChanges(); + } + + public void InsertBulk(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + } + + + Entity.AddRange(entities); + _db.SaveChanges(); + } + + public async Task InsertAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await _db.SaveChangesAsync(); + } + + public async Task InsertBulkAsync(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + } + + + + await Entity.AddRangeAsync(entities); + await _db.SaveChangesAsync(); + } + + + public void Update(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + Entity.Update(entity); + _db.SaveChanges(); + } + + public void UpdateBulk(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.UpdatedTime = DateTime.Now; + } + + _db.UpdateRange(entities); + _db.SaveChanges(); + } + } +} +``` \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/7.3 C#\345\237\272\347\241\200\345\233\236\351\241\276.md" "b/\351\273\204\345\256\207\347\205\214/7.3 C#\345\237\272\347\241\200\345\233\236\351\241\276.md" new file mode 100644 index 0000000000000000000000000000000000000000..fd48827f1e86b86547729032e182e910923304ed --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/7.3 C#\345\237\272\347\241\200\345\233\236\351\241\276.md" @@ -0,0 +1,5 @@ +# C# 基础回顾 + +## 思维导图 + +![图片](./img/csharpbasic.png) \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/launch.json" "b/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/launch.json" new file mode 100644 index 0000000000000000000000000000000000000000..5a7f77c9e04d8e80206472a4d8dea8c75f954bd3 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/launch.json" @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/MyApi.Api/bin/Debug/netcoreapp3.1/MyApi.Api.dll", + "args": [], + "cwd": "${workspaceFolder}/MyApi.Api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/tasks.json" "b/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/tasks.json" new file mode 100644 index 0000000000000000000000000000000000000000..9d87fd9d320bf3fa0298194df33ae30822ec3a00 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/.vscode/tasks.json" @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/UsersController.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7695d3012cc25ecd1aff9bf0542759e693211b1c --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/UsersController.cs" @@ -0,0 +1,141 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using MyApi.Api.Entity; +using MyApi.Api.ParamModel; +using MyApi.Api.Repository; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + + // private IRepository _usersRepository = new EFRepository(); + + private IRepository _usersRepository; + + public UsersController(IRepository usersRepository) + { + _usersRepository=usersRepository; + } + + + [HttpGet] + public dynamic Get() + { + // var users = db.Users; + // return users; + + var users = _usersRepository.Table.ToList(); + return new + { + Code = 1000, + Data = users, + Msg = "获取用户列表成功!!!" + }; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + // var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + var user = _usersRepository.GetById(id); + return user; + } + + [HttpPost] + public dynamic Post(NewUser model) + { + var user = + new Users + { + UserName = model.UserName, + PassWord = model.PassWord + }; + + // db.Users.Add(user); + // db.SaveChanges(); + _usersRepository.Insert(user); + return new { Data = user, Msg = "成功了" }; + } + + [HttpPut("{id}")] + public dynamic Put(int id, NewUser model) + { + // var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + var user = _usersRepository.GetById(id); + if (user != null) + { + user.UserName = model.UserName; + user.PassWord = model.PassWord; + // db.Users.Update(user); + // db.SaveChanges(); + _usersRepository.Update(user); + return new + { + Data = user, + Msg = string.Format("要修改的Id为{0},", id) + }; + } + else + { + return new + { + Data = user, + Msg = string.Format("找不到的Id为{0}的数据执行修改操作,", id) + }; + } + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + // var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + var user = _usersRepository.GetById(id); + if (user != null) + { + // db.Users.Remove(user); + // db.SaveChanges(); + _usersRepository.Delete(id); + return new + { + Data = user, + Msg = string.Format("要删除的Id为{0},", id) + }; + } + else + { + return new + { + Data = "", + Msg = string.Format("找不到的Id为{0}的数据执行删除操作,", id) + }; + } + } + + private IEnumerable GetUsers() + { + var users = + new List { + new Users { + Id = 1, + UserName = "你好", + PassWord = "确实好啊" + }, + new Users { + Id = 2, + UserName = "真的吗", + PassWord = "不啊" + }, + new Users { + Id = 3, + UserName = "可以吗", + PassWord = "我觉得不行" + } + }; + return users; + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..06d511383656773e86e68a7bf53273c63955d93a --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + 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 IEnumerable 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(); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Data/ShowDb.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Data/ShowDb.cs" new file mode 100644 index 0000000000000000000000000000000000000000..f834ac4b147404e000acf23a35001566513f2fa6 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Data/ShowDb.cs" @@ -0,0 +1,20 @@ +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; + +namespace MyApi.Api.Db +{ + public class ShowDb : DbContext + { + // 因为我们使用AddDbContext到容器,所以此处必须得有带参数的构造函数(而且必须传递DbContextOptions类型的参数,同时父类也得调用这个参数) + public ShowDb(DbContextOptions options) : base(options) + { + + } + public DbSet Users { get; set; } + public DbSet UserRoles { get; set; } + public DbSet Roles { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/BaseEntity.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b639c3988971ed590ed0ac8326f3a461caec45ce --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/BaseEntity.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyApi.Api.Entity +{ + public abstract class BaseEntity + { + 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 DisplayOrder { get; set; } + public string Remarks { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Roles.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b81323696fd09d50bf5f31e6ef1c4b5c03fc38ad --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Roles.cs" @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +namespace MyApi.Api.Entity +{ + public class Roles:BaseEntity + { + public string RoleName { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/UserRoles.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/UserRoles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..33f6516b522eebf641a6d07fc715be24dd47d96b --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/UserRoles.cs" @@ -0,0 +1,12 @@ +using System; + +namespace MyApi.Api.Entity +{ + public class UserRoles:BaseEntity + { + public int UserId { get; set; } + public int RoleId { get; set; } + public virtual Users User { get; set; } + public virtual Roles Role { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Users.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..22e12b0ce7d5ae70071ae7644776ac5a4585263d --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Entity/Users.cs" @@ -0,0 +1,10 @@ +using System; + +namespace MyApi.Api.Entity +{ + public class Users:BaseEntity + { + public String UserName { get; set; } + public string PassWord { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..28249b80e39dbaa870910d49db6c32a947a739d0 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" @@ -0,0 +1,159 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyApi.Api.Db; + +namespace MyApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + [Migration("20210629030340_建立表关系")] + partial class 建立表关系 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleName") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId"); + + b.ToTable("UserRoles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.HasOne("MyApi.Api.Entity.Roles", "Role") + .WithMany("UserRoles") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MyApi.Api.Entity.Users", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Navigation("UserRoles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" new file mode 100644 index 0000000000000000000000000000000000000000..df3e1e0549d439725096e4fa56d4dfb5d7648656 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" @@ -0,0 +1,104 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyApi.Api.Migrations +{ + public partial class 建立表关系 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Roles", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RoleName = table.Column(type: "nvarchar(max)", nullable: true), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Roles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserName = table.Column(type: "nvarchar(max)", nullable: true), + PassWord = table.Column(type: "nvarchar(max)", nullable: true), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "UserRoles", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserId = table.Column(type: "int", nullable: false), + RoleId = table.Column(type: "int", nullable: false), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserRoles", x => x.Id); + table.ForeignKey( + name: "FK_UserRoles_Roles_RoleId", + column: x => x.RoleId, + principalTable: "Roles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_UserRoles_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_UserRoles_RoleId", + table: "UserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "IX_UserRoles_UserId", + table: "UserRoles", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserRoles"); + + migrationBuilder.DropTable( + name: "Roles"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0e2093be39b26b8c16402e747ce84dddfb70bd31 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" @@ -0,0 +1,157 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyApi.Api.Db; + +namespace MyApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + partial class ShowDbModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleName") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId"); + + b.ToTable("UserRoles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.HasOne("MyApi.Api.Entity.Roles", "Role") + .WithMany("UserRoles") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MyApi.Api.Entity.Users", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Navigation("UserRoles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/MyApi.Api.csproj" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/MyApi.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..4190345184c47a1d03da34647506f61affac026b --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/MyApi.Api.csproj" @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/ParamModel/NewUser.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/ParamModel/NewUser.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fb11a99676e4c9d5360bdcdf7869b7cac76a2149 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/ParamModel/NewUser.cs" @@ -0,0 +1,7 @@ +namespace MyApi.Api.ParamModel +{ + public class NewUser{ + public string UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Program.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..41100d392b0479034ecee725b022669d10aab720 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/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 MyApi.Api +{ + 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\345\256\207\347\205\214/MyApi/MyApi.Api/Properties/launchSettings.json" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..9d3877bcdce5539fade5cb7103a5222d2e59a124 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:35290", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "MyApi.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/EFRepository.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/EFRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..17e9c22f10a30716898001beaa06d863a2535915 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/EFRepository.cs" @@ -0,0 +1,170 @@ +using System; +using System.Threading.Tasks; +using System.Linq; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; +using MyApi.Api.Db; + +namespace MyApi.Api.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + /// + /// 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + /// + /// + // private ShowDb _db = new ShowDb(); + private ShowDb _db; + + public EFRepository(ShowDb db) + { + _db = db; + } + + + /// + /// 一个字段成员,用于内部Entity属性 + /// + private DbSet _entity; + + + /// + /// 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + /// + /// + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = _db.Set(); + } + return _entity; + } + } + + + /// + /// 代表一个可以用于查询T类型的表 + /// + /// + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + + /// + /// 删除(指定T类型,在数据库当中代表指定的表)指定Id的记录 + /// + /// + public void Delete(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + Entity.Remove(t); + _db.SaveChanges(); + } + + public T GetById(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + return t; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + _db.SaveChanges(); + } + + public void InsertBulk(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + } + + + Entity.AddRange(entities); + _db.SaveChanges(); + } + + public async Task InsertAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await _db.SaveChangesAsync(); + } + + public async Task InsertBulkAsync(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + } + + + + await Entity.AddRangeAsync(entities); + await _db.SaveChangesAsync(); + } + + + public void Update(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + Entity.Update(entity); + _db.SaveChanges(); + } + + public void UpdateBulk(IEnumerable entities) + { + foreach (var entity in entities) + { + entity.UpdatedTime = DateTime.Now; + } + + _db.UpdateRange(entities); + _db.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/IRepository.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/IRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3f38e3fefc2504151b6cbc7da7d4f3b2eb6fac57 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Repository/IRepository.cs" @@ -0,0 +1,66 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; + +namespace MyApi.Api.Repository +{ + public interface IRepository + { + IQueryable Table { get; } + + /// + /// 根据Id获取指定实体 + /// + /// + /// + T GetById(int id); + + + /// + /// 使用实体对象,插入数据 + /// + /// 需要插入的对象 + void Insert(T entity); + + + /// + /// 使用实体对象,插入数据(异步) + /// + /// 需要插入的对象 + Task InsertAsync(T entity); + + /// + /// 使用实体对象,插入多条数据 + /// + /// 待插入的若干实体数据 + void InsertBulk(IEnumerable entities); + /// + /// 使用实体对象,插入多条数据(异步) + /// + /// 待插入的若干实体数据 + Task InsertBulkAsync(IEnumerable entities); + + + /// + /// 根据对象更新数据 + /// + /// 要更新的对象 + void Update(T entity); + + + /// + /// 根据对象更新数据(异步) + /// + /// 要更新的若干个实体 + /// + void UpdateBulk(IEnumerable entities); + + + /// + /// 根据Id删除对应的记录 + /// + /// 主键id + void Delete(int id); + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Startup.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d140984eaf807d98471dcb1a01b23790a006c3e7 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/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 Microsoft.EntityFrameworkCore; +using MyApi.Api.Repository; + +namespace MyApi.Api +{ + 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(o=>o.UseSqlServer()); + // 注册对数据库的基本操作服务到容器 + services.AddScoped(typeof(IRepository<>),typeof(EFRepository<>)); + + 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(); + }); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/WeatherForecast.cs" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..9ee21b01ed0e625746a7b8c6ccd49f676d506644 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyApi.Api +{ + 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\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.Development.json" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.json" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.sln" "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.sln" new file mode 100644 index 0000000000000000000000000000000000000000..07e74c9cb3bde36ec201f86d1aac38b1fc546d20 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/MyApi.sln" @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApi.Api", "MyApi.Api\MyApi.Api.csproj", "{377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x64.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x64.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x86.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x86.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|Any CPU.Build.0 = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x64.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x64.Build.0 = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x86.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git "a/\351\273\204\345\256\207\347\205\214/MyApi/api.http" "b/\351\273\204\345\256\207\347\205\214/MyApi/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..55d711ec944e82bdca607a7dde0822001176a91c --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/MyApi/api.http" @@ -0,0 +1,32 @@ +### Get获取 + +GET http://localhost:5000/users HTTP/1.1 + +### +GET http://localhost:5000/users/2 HTTP/1.1 + + +### Post添加 + +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"李白爱吃鱼", + "PassWord":"杜甫也爱" +} + +### Put修改 + +PUT http://localhost:5000/users/2 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"这里", + "PassWord":"123" +} + + +### Delete删除 + +DELETE http://localhost:5000/users/2 HTTP/1.1 diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/launch.json" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/launch.json" new file mode 100644 index 0000000000000000000000000000000000000000..5a7f77c9e04d8e80206472a4d8dea8c75f954bd3 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/launch.json" @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/MyApi.Api/bin/Debug/netcoreapp3.1/MyApi.Api.dll", + "args": [], + "cwd": "${workspaceFolder}/MyApi.Api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/tasks.json" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/tasks.json" new file mode 100644 index 0000000000000000000000000000000000000000..9d87fd9d320bf3fa0298194df33ae30822ec3a00 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/.vscode/tasks.json" @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/MyApi.Api/MyApi.Api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/UsersController.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..787ae95db3109e4bd5d707f16e2482b0d5b10c61 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/UsersController.cs" @@ -0,0 +1,131 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using MyApi.Api.Entity; +using MyApi.Api.ParamModel; +using MyApi.Api.Repository; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + + private MyApi.Api.Db.ShowDb db = new Api.Db.ShowDb(); + private IRepository _usersReponsitory = new EFRepository(); + + + + [HttpGet] + public dynamic Get() + { + // var users = db.Users; + // return users; + + var users = _usersReponsitory.Table.ToList(); + return new + { + Code = 1000, + Data = users, + Msg = "获取用户列表成功!!!" + }; + } + + [HttpGet("{id}")] + public dynamic Get(int id) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + return user; + } + + [HttpPost] + public dynamic Post(NewUser model) + { + var user = + new Users + { + UserName = model.UserName, + PassWord = model.PassWord + }; + + db.Users.Add(user); + db.SaveChanges(); + return new { Data = user, Msg = "成功了" }; + } + + [HttpPut("{id}")] + public dynamic Put(int id, NewUser model) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + if (user != null) + { + user.UserName = model.UserName; + user.PassWord = model.PassWord; + db.Users.Update(user); + db.SaveChanges(); + return new + { + Data = user, + Msg = string.Format("要修改的Id为{0},", id) + }; + } + else + { + return new + { + Data = user, + Msg = string.Format("找不到的Id为{0}的数据执行修改操作,", id) + }; + } + } + + [HttpDelete("{id}")] + public dynamic Delete(int id) + { + var user = db.Users.Where(x => x.Id == id).FirstOrDefault(); + + if (user != null) + { + db.Users.Remove(user); + db.SaveChanges(); + return new + { + Data = user, + Msg = string.Format("要删除的Id为{0},", id) + }; + } + else + { + return new + { + Data = "", + Msg = string.Format("找不到的Id为{0}的数据执行删除操作,", id) + }; + } + } + + private IEnumerable GetUsers() + { + var users = + new List { + new Users { + Id = 1, + UserName = "你好", + PassWord = "确实好啊" + }, + new Users { + Id = 2, + UserName = "真的吗", + PassWord = "不啊" + }, + new Users { + Id = 3, + UserName = "可以吗", + PassWord = "我觉得不行" + } + }; + return users; + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..06d511383656773e86e68a7bf53273c63955d93a --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Controllers/WeatherForecastController.cs" @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MyApi.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + 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 IEnumerable 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(); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Data/ShowDb.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Data/ShowDb.cs" new file mode 100644 index 0000000000000000000000000000000000000000..eea082b052360d03e134ef68d414dc4c7f8e5736 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Data/ShowDb.cs" @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; + +namespace MyApi.Api.Db +{ + public class ShowDb : DbContext + { + public DbSet Users { get; set; } + public DbSet UserRoles { get; set; } + public DbSet Roles { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer(@"server=.;database=ShowDb;uid=sa;pwd=123456;"); + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/BaseEntity.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/BaseEntity.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b639c3988971ed590ed0ac8326f3a461caec45ce --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/BaseEntity.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyApi.Api.Entity +{ + public abstract class BaseEntity + { + 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 DisplayOrder { get; set; } + public string Remarks { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Roles.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Roles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b81323696fd09d50bf5f31e6ef1c4b5c03fc38ad --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Roles.cs" @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; + +namespace MyApi.Api.Entity +{ + public class Roles:BaseEntity + { + public string RoleName { get; set; } + + public virtual IEnumerable UserRoles { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/UserRoles.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/UserRoles.cs" new file mode 100644 index 0000000000000000000000000000000000000000..33f6516b522eebf641a6d07fc715be24dd47d96b --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/UserRoles.cs" @@ -0,0 +1,12 @@ +using System; + +namespace MyApi.Api.Entity +{ + public class UserRoles:BaseEntity + { + public int UserId { get; set; } + public int RoleId { get; set; } + public virtual Users User { get; set; } + public virtual Roles Role { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Users.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Users.cs" new file mode 100644 index 0000000000000000000000000000000000000000..22e12b0ce7d5ae70071ae7644776ac5a4585263d --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Entity/Users.cs" @@ -0,0 +1,10 @@ +using System; + +namespace MyApi.Api.Entity +{ + public class Users:BaseEntity + { + public String UserName { get; set; } + public string PassWord { get; set; } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" new file mode 100644 index 0000000000000000000000000000000000000000..28249b80e39dbaa870910d49db6c32a947a739d0 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.Designer.cs" @@ -0,0 +1,159 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyApi.Api.Db; + +namespace MyApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + [Migration("20210629030340_建立表关系")] + partial class 建立表关系 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleName") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId"); + + b.ToTable("UserRoles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.HasOne("MyApi.Api.Entity.Roles", "Role") + .WithMany("UserRoles") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MyApi.Api.Entity.Users", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Navigation("UserRoles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" new file mode 100644 index 0000000000000000000000000000000000000000..df3e1e0549d439725096e4fa56d4dfb5d7648656 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/20210629030340_\345\273\272\347\253\213\350\241\250\345\205\263\347\263\273.cs" @@ -0,0 +1,104 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MyApi.Api.Migrations +{ + public partial class 建立表关系 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Roles", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RoleName = table.Column(type: "nvarchar(max)", nullable: true), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Roles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserName = table.Column(type: "nvarchar(max)", nullable: true), + PassWord = table.Column(type: "nvarchar(max)", nullable: true), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "UserRoles", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserId = table.Column(type: "int", nullable: false), + RoleId = table.Column(type: "int", nullable: false), + IsActived = table.Column(type: "bit", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + CreatedTime = table.Column(type: "datetime2", nullable: false), + UpdatedTime = table.Column(type: "datetime2", nullable: false), + DisplayOrder = table.Column(type: "int", nullable: false), + Remarks = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserRoles", x => x.Id); + table.ForeignKey( + name: "FK_UserRoles_Roles_RoleId", + column: x => x.RoleId, + principalTable: "Roles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_UserRoles_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_UserRoles_RoleId", + table: "UserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "IX_UserRoles_UserId", + table: "UserRoles", + column: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserRoles"); + + migrationBuilder.DropTable( + name: "Roles"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" new file mode 100644 index 0000000000000000000000000000000000000000..0e2093be39b26b8c16402e747ce84dddfb70bd31 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Migrations/ShowDbModelSnapshot.cs" @@ -0,0 +1,157 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using MyApi.Api.Db; + +namespace MyApi.Api.Migrations +{ + [DbContext(typeof(ShowDb))] + partial class ShowDbModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.7") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleName") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId"); + + b.ToTable("UserRoles"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Users", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CreatedTime") + .HasColumnType("datetime2"); + + b.Property("DisplayOrder") + .HasColumnType("int"); + + b.Property("IsActived") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("PassWord") + .HasColumnType("nvarchar(max)"); + + b.Property("Remarks") + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedTime") + .HasColumnType("datetime2"); + + b.Property("UserName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.UserRoles", b => + { + b.HasOne("MyApi.Api.Entity.Roles", "Role") + .WithMany("UserRoles") + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("MyApi.Api.Entity.Users", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("MyApi.Api.Entity.Roles", b => + { + b.Navigation("UserRoles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/MyApi.Api.csproj" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/MyApi.Api.csproj" new file mode 100644 index 0000000000000000000000000000000000000000..4190345184c47a1d03da34647506f61affac026b --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/MyApi.Api.csproj" @@ -0,0 +1,16 @@ + + + + netcoreapp3.1 + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/ParamModel/NewUser.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/ParamModel/NewUser.cs" new file mode 100644 index 0000000000000000000000000000000000000000..fb11a99676e4c9d5360bdcdf7869b7cac76a2149 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/ParamModel/NewUser.cs" @@ -0,0 +1,7 @@ +namespace MyApi.Api.ParamModel +{ + public class NewUser{ + public string UserName{get;set;} + public string PassWord{get;set;} + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Program.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Program.cs" new file mode 100644 index 0000000000000000000000000000000000000000..41100d392b0479034ecee725b022669d10aab720 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/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 MyApi.Api +{ + 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\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Properties/launchSettings.json" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Properties/launchSettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..9d3877bcdce5539fade5cb7103a5222d2e59a124 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Properties/launchSettings.json" @@ -0,0 +1,30 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:35290", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "MyApi.Api": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/EFRepository.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/EFRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..6b5403c7fb6ffa853feb5d85ed28a01264f0d21e --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/EFRepository.cs" @@ -0,0 +1,128 @@ +using System; +using System.Threading.Tasks; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using MyApi.Api.Entity; +using MyApi.Api.Db; + +namespace MyApi.Api.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + /// + /// 数据库上下文的变量,此处是使用常规手段,直接初始化一个数据库上下文的实例对象 + /// + /// + private ShowDb db = new ShowDb(); + + + /// + /// 一个字段成员,用于内部Entity属性 + /// + private DbSet _entity; + + + /// + /// 一个访问受限的属性,只有访问器,总是返回一个代表T类型的表对象 + /// + /// + protected DbSet Entity + { + get + { + if (_entity == null) + { + _entity = db.Set(); + } + return _entity; + } + } + + + /// + /// 代表一个可以用于查询T类型的表 + /// + /// + public IQueryable Table + { + get + { + return Entity.AsQueryable(); + } + } + + + /// + /// 删除(指定T类型,在数据库当中代表指定的表)指定Id的记录 + /// + /// + public void Delete(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + Entity.Remove(t); + db.SaveChanges(); + } + + public T GetById(int id) + { + var t = Entity.Where(x => x.Id == id).FirstOrDefault(); + return t; + } + + public void Insert(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + Entity.Add(entity); + db.SaveChanges(); + } + + public async Task InsertAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.IsActived = true; + entity.IsDeleted = false; + entity.DisplayOrder = 0; + entity.CreatedTime = DateTime.Now; + entity.UpdatedTime = DateTime.Now; + + await Entity.AddAsync(entity); + await db.SaveChangesAsync(); + } + + public void Update(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + db.SaveChanges(); + } + + public async Task UpdateAsync(T entity) + { + if (entity == null) + { + throw new NotImplementedException(); + } + + entity.UpdatedTime = DateTime.Now; + await db.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/IRepository.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/IRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..324fb21dc056fe346de13bbc29ed79794be9f578 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Repository/IRepository.cs" @@ -0,0 +1,55 @@ +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace MyApi.Api.Repository +{ + public interface IRepository + { + IQueryable Table { get; } + + /// + /// 根据Id获取指定实体 + /// + /// + /// + T GetById(int id); + + + /// + /// 使用实体对象,插入数据 + /// + /// 需要插入的对象 + void Insert(T entity); + + + /// + /// 使用实体对象,插入数据(异步) + /// + /// 需要插入的对象 + Task InsertAsync(T entity); + + + /// + /// 根据对象更新数据 + /// + /// 要更新的对象 + void Update(T entity); + + + /// + /// 根据对象更新数据(异步) + /// + /// 要更新的对象 + /// + Task UpdateAsync(T entity); + + + /// + /// 根据Id删除对应的记录 + /// + /// 主键id + void Delete(int id); + + } +} \ No newline at end of file diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Startup.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Startup.cs" new file mode 100644 index 0000000000000000000000000000000000000000..3e729e15bf7933d375378760681be160e4dce056 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/Startup.cs" @@ -0,0 +1,48 @@ +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; + +namespace MyApi.Api +{ + 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.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(); + }); + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/WeatherForecast.cs" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/WeatherForecast.cs" new file mode 100644 index 0000000000000000000000000000000000000000..9ee21b01ed0e625746a7b8c6ccd49f676d506644 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/WeatherForecast.cs" @@ -0,0 +1,15 @@ +using System; + +namespace MyApi.Api +{ + 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\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.Development.json" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.Development.json" new file mode 100644 index 0000000000000000000000000000000000000000..8983e0fc1c5e2795ccfde0c771c6d66c88ef4a42 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.Development.json" @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.json" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.json" new file mode 100644 index 0000000000000000000000000000000000000000..d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.Api/appsettings.json" @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.sln" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.sln" new file mode 100644 index 0000000000000000000000000000000000000000..07e74c9cb3bde36ec201f86d1aac38b1fc546d20 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/MyApi.sln" @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApi.Api", "MyApi.Api\MyApi.Api.csproj", "{377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x64.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x64.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x86.ActiveCfg = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Debug|x86.Build.0 = Debug|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|Any CPU.Build.0 = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x64.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x64.Build.0 = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x86.ActiveCfg = Release|Any CPU + {377A0A24-E6C8-4A5A-A0F8-1562C9E6DAAF}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git "a/\351\273\204\345\256\207\347\205\214/Old/MyApi/api.http" "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..18bac33cfa761c010557b64c51d0dd3e0d8b2661 --- /dev/null +++ "b/\351\273\204\345\256\207\347\205\214/Old/MyApi/api.http" @@ -0,0 +1,32 @@ +### Get获取 + +GET http://localhost:5000/users HTTP/1.1 + +### +GET http://localhost:5000/users/2 HTTP/1.1 + + +### Post添加 + +POST http://localhost:5000/users HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"李白爱吃鱼", + "PassWord":"杜甫也爱" +} + +### Put修改 + +PUT http://localhost:5000/users/1 HTTP/1.1 +Content-Type: application/json + +{ + "UserName":"这里", + "PassWord":"123" +} + + +### Delete删除 + +DELETE http://localhost:5000/users/1 HTTP/1.1 diff --git "a/\351\273\204\345\256\207\347\205\214/img/csharpbasic.png" "b/\351\273\204\345\256\207\347\205\214/img/csharpbasic.png" new file mode 100644 index 0000000000000000000000000000000000000000..b9aae4203a27edc850d7ff076c5882bd5c94bc87 Binary files /dev/null and "b/\351\273\204\345\256\207\347\205\214/img/csharpbasic.png" differ