diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/api.http" "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" new file mode 100644 index 0000000000000000000000000000000000000000..1cbb5b97af62e7fa36e6313ed6922c9db3c2933e --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" @@ -0,0 +1,24 @@ +@url=http://localhost:5000 + +### +GET {{url}}/users HTTP/1.1 + +### +POST {{url}}/users HTTP/1.1 +Content-Type: application/json + +{ + "Username":"admin", + "Password":"113" +} + +### +PUT {{url}}/users/6C1FFF06-27DC-45C0-6CF8-08DA450D94C4 HTTP/1.1 +Content-Type: application/json + +{ + "Username":"1231231", + "Password":"sadfas" +} +### +DELETE {{url}}/users/6C1FFF06-27DC-45C0-6CF8-08DA450D94C4 HTTP/1.1 \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" index a9b52369d78683fc9e8264bd11a262d5bd12fac0..6cb8a69cd8df456ba6a36697640a354f75d8df98 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" @@ -1,20 +1,24 @@ using Microsoft.EntityFrameworkCore; using Webapi.Domain.Entity; -namespace webapi.Infrastructure.Db +namespace webapi.Infrastructure.Db { - public class webapiDbContext: DbContext + public class webapiDbContext : DbContext { - private readonly string - _conString = "server=.;database=webapi;uid=sa;pwd=123456;"; + private readonly string + _conString = "server=.;database=webapi;uid=sa;pwd=123456;"; + public webapiDbContext(DbContextOptions options) : base(options) + { + + } protected override void OnConfiguring(DbContextOptionsBuilder builder) { - builder.UseSqlServer (_conString); + builder.UseSqlServer(_conString); } - public DbSet Users {get;set;} - public DbSet Roles {get;set;} - public DbSet UserRoles {get;set;} + public DbSet Users { get; set; } + public DbSet Roles { get; set; } + public DbSet UserRoles { get; set; } } } \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" similarity index 98% rename from "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" rename to "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" index 45537ebf853a9f375147adf6b9bb926ebc679b1b..758b708f4d6d829bbb45b984914580cd4571fcc9 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" @@ -10,8 +10,8 @@ using webapi.Infrastructure.Db; namespace webapi.Infrastructure.Migrations { [DbContext(typeof(webapiDbContext))] - [Migration("20220531022514_1")] - partial class _1 + [Migration("20220602085932_模型默认值")] + partial class 模型默认值 { protected override void BuildTargetModel(ModelBuilder modelBuilder) { diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" similarity index 98% rename from "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" rename to "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" index 83ad2fb21c485b0bfa7c5347ff7d5a667a15695a..6088660ab2ca3a42883cc49670a6295f7f859568 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace webapi.Infrastructure.Migrations { - public partial class _1 : Migration + public partial class 模型默认值 : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..6df1b9858e6f5e8b85b9b75ec5a388544d757c06 --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" @@ -0,0 +1,69 @@ +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using webapi.Domain; +using webapi.Domain.Repository; +using webapi.Infrastructure.Db; + +namespace webapi.Infrastructure.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + private readonly webapiDbContext _db; + + private readonly DbSet _table; + public IQueryable Table + { + get + { + return _table.AsNoTracking(); + // return _table as IQueryable; + } + } + + public EFRepository(webapiDbContext db) + { + _db = db; + _table = _db.Set(); + } + + + public T Add(T entity) + { + entity.IsActived = true; + entity.IsDeleted = false; + entity.CreatedAt = DateTime.Now; + entity.UpdatedAt = DateTime.Now; + entity.DisplayOrder = 0; + + _table.Add(entity); + _db.SaveChanges(); + + return entity; + } + public T getById(Guid id) + { + // var entity = _table.Where(x => x.Id == id && x.IsDeleted == false).FirstOrDefault(); + var entity = + _db + .Set() + .AsQueryable() + .Where(x => x.Id == id) + .FirstOrDefault(); + return entity; + } + + public void Update(T entity) + { + entity.UpdatedAt = DateTime.Now; + _table.Update(entity); + _db.SaveChanges(); + } + + public void Delete(T entity) + { + _table.Update(entity); + _db.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..273c13968b5da2745e966c89eb281d51370ce868 --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" @@ -0,0 +1,81 @@ +using System; +using webapi.Infrastructure.Repository; +using Microsoft.AspNetCore.Mvc; +using webapi.Infrastructure.Db; +using Webapi.Domain.Entity; +using System.Collections.Generic; +using webapi.Domain.Repository; +using System.Linq; + +namespace webapi.api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + // private readonly webapiDbContext _db; + private readonly IRepository _users; + + public UsersController(IRepository users) + { + // _db = new webapiDbContext(); + // _users = new EFRepository(); + _users = users; + } + public IEnumerable Index() + { + var users = _users.Table.ToArray(); + return users; + } + + [HttpPost] + public Users CreateUser(Users entity) + { + System.Console.WriteLine(entity); + var user = + new Users + { + UserName = entity.UserName, + Password = entity.Password + }; + var list = _users.Add(user); + return list; + } + + [HttpPut] + [Route("{id}")] + public Users update(Guid id,Users entity) + { + var user = _users.getById(id); + if(user != null){ + user.UserName = entity.UserName; + user.Password = entity.Password; + _users.Update(user); + } + return user; + } + + [HttpDelete] + [Route("{id}")] + public Users deleteUser(Guid id) + { + var user = _users.getById(id); + if(user != null){ + user.IsDeleted = true; + _users.Update(user); + } + return user; + } + + // public class UserCto + // { + // public string UserName { get; set; } + + // public string Password { get; set; } + + // public bool IsActived { get; set; } + + // public bool IsDeleted { get; set; } + // } + } +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" index fc23443422b2cef25f675816b77f37da5760751f..71067300bc26f09320e013e3d7d67ca58a496a29 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" @@ -5,10 +5,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using webapi.Domain.Repository; +using webapi.Infrastructure.Db; +using webapi.Infrastructure.Repository; namespace webapi.api { @@ -24,6 +28,11 @@ namespace webapi.api // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + var Sqlserver = Configuration.GetConnectionString("Sqlserver"); + services.AddDbContext(options=>options.UseSqlServer(Sqlserver)); + + // 实现CRUD的依赖注入 + services.AddScoped(typeof(IRepository<>),typeof(EFRepository<>)); services.AddControllers(); } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" index d9d9a9bff6fd6f3ee7ea00de958f135a4a28c6e6..8864025e83daff887d22cebc17dd97f3320522c6 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "Sqlserver": "server=.;database=webapi;uid=sa;pwd=123456;" + } } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" index 8bc3ef760d701f532f45cd2b4feff3b65692f07f..3785b32b9b5ae9fd9f2f18b6d75a9e08da31319d 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" @@ -8,10 +8,10 @@ namespace webapi.Domain public Guid Id { get; set; } // 标识记录是否启用 - public bool IsActived { get; set; } + public bool IsActived { get; set; }=true; // 标识记录是否删除 - public bool IsDeleted { get; set; } + public bool IsDeleted { get; set; }=false; // 创建者的Id public Guid? CreatedBy { get; set; } @@ -20,10 +20,10 @@ namespace webapi.Domain public Guid? UpdatedBy { get; set; } // 创建时间 - public DateTime CreatedAt { get; set; } + public DateTime CreatedAt { get; set; }=DateTime.Now; // (最后)修改时间 - public DateTime UpdatedAt { get; set; } + public DateTime UpdatedAt { get; set; }=DateTime.Now; // 展示的顺序 public int DisplayOrder { get; set; } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" new file mode 100644 index 0000000000000000000000000000000000000000..5f5140bc073a124ca2cb03d492e4d6de50601e6d --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" @@ -0,0 +1,18 @@ +using System; +using System.Linq; + +namespace webapi.Domain.Repository +{ + public interface IRepository + { + T getById(Guid id); + + IQueryable Table{get;} + + T Add(T entity); + + void Update(T entity); + + void Delete(T entity); + } +} \ No newline at end of file