模型类

This commit is contained in:
橙子
2021-10-11 21:50:50 +08:00
parent 543800d0e7
commit 2093d1c78d
38 changed files with 652 additions and 185 deletions

View File

@@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
namespace Yi.Framework.ApiMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class UserController : ControllerBase
{
private readonly ILogger<UserController> _logger;
private IUserService _userService;
public UserController(ILogger<UserController> logger, IUserService userService)
{
_logger = logger;
_userService = userService;
}
[HttpGet]
public async Task<Result> GetUser()
{
return Result.Success().SetData(await _userService.GetAllEntitiesTrueAsync());
}
[HttpPut]
public async Task<Result> UpdateUser(user _user)
{
await _userService.UpdateAsync(_user);
return Result.Success();
}
[HttpDelete]
public async Task<Result> DelListUser(List<int> _ids)
{
await _userService.DelListByUpdateAsync(_ids);
return Result.Success();
}
[HttpPost]
public async Task<Result> AddUser(user _user)
{
await _userService.AddAsync(_user);
return Result.Success();
}
}
}

View File

@@ -1,39 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Yi.Framework.ApiMicroservice.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<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> 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();
}
}
}

View File

@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@@ -12,6 +13,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.IOCOptions;
using Yi.Framework.Interface;
using Yi.Framework.Model;
using Yi.Framework.Service;
namespace Yi.Framework.ApiMicroservice
{
@@ -30,9 +34,22 @@ namespace Yi.Framework.ApiMicroservice
{
services.AddControllers();
services.AddCors(options => options.AddPolicy("CorsPolicy",//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
builder =>
{
builder.AllowAnyMethod()
.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowCredentials();
}));
services.Configure<SqliteOptions>(this.Configuration.GetSection("SqliteConn"));
services.AddScoped<DbContext, DataContext>();
services.AddScoped(typeof(IBaseService<>),typeof(BaseService<>));
services.AddScoped<IUserService, UserService>();
services.AddScoped<IRoleService, RoleService>();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Yi.Framework.ApiMicroservice", Version = "v1" });
@@ -52,7 +69,7 @@ namespace Yi.Framework.ApiMicroservice
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();

View File

@@ -13,7 +13,9 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Interface\Yi.Framework.Interface.csproj" />
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
<ProjectReference Include="..\Yi.Framework.Service\Yi.Framework.Service.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Yi.Framework.Common.Enum
{
public enum AgrFlagEnum
{
wait = 0,
Agree = 1
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Yi.Framework.Common.Enum
{
public enum DelFlagEnum
{
Normal=0,
Deleted=1
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Common.Models
{
public class PageModel
{
}
}

View File

@@ -1,8 +0,0 @@
using System;
namespace Yi.Framework.Interface
{
public class Class1
{
}
}

View File

@@ -14,6 +14,11 @@ namespace Yi.Framework.Interface
#endregion
Task<T> GetEntityById(int id);
#region
//通过表达式得到实体
#endregion
Task<T> GetEntity(Expression<Func<T, bool>> whereLambda);
#region
//得到全部实体
#endregion
@@ -54,6 +59,11 @@ namespace Yi.Framework.Interface
#endregion
Task<bool> UpdateAsync(T entity);
#region
//更新多个实体
#endregion
Task<bool> UpdateListAsync(IEnumerable<T> entities);
#region
//更新实体部分属性
#endregion

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Interface
{
public interface IRoleService:IBaseService<role>
{
Task<bool> DelListByUpdateAsync(List<int> _ids);
Task<IEnumerable<role>> GetAllEntitiesTrueAsync();
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Interface
{
public interface IUserService:IBaseService<user>
{
Task<bool> DelListByUpdateAsync(List<int> _ids);
Task<IEnumerable<user>> GetAllEntitiesTrueAsync();
}
}

View File

@@ -4,4 +4,8 @@
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Model\Yi.Framework.Model.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,8 +0,0 @@
using System;
namespace Yi.Framework.Model
{
public class Class1
{
}
}

View File

@@ -34,6 +34,7 @@ namespace Yi.Framework.Model
optionsBuilder.UseSqlite(_connStr);
}
}
public virtual DbSet<user> user { get; set; }
public DbSet<user> user { get; set; }
public DbSet<role> role { get; set; }
}
}

View File

@@ -0,0 +1,78 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Yi.Framework.Model;
namespace Yi.Framework.Model.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20211011082334_yi-2")]
partial class yi2
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "5.0.10");
modelBuilder.Entity("Yi.Framework.Model.Models.role", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("introduce")
.HasColumnType("TEXT");
b.Property<int>("is_delete")
.HasColumnType("INTEGER");
b.Property<string>("role_name")
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("role");
});
modelBuilder.Entity("Yi.Framework.Model.Models.user", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int?>("age")
.HasColumnType("INTEGER");
b.Property<string>("email")
.HasColumnType("TEXT");
b.Property<string>("icon")
.HasColumnType("TEXT");
b.Property<string>("ip")
.HasColumnType("TEXT");
b.Property<int>("is_delete")
.HasColumnType("INTEGER");
b.Property<string>("nick")
.HasColumnType("TEXT");
b.Property<string>("password")
.HasColumnType("TEXT");
b.Property<string>("username")
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("user");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,120 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace Yi.Framework.Model.Migrations
{
public partial class yi2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "name",
table: "user",
newName: "username");
migrationBuilder.AlterColumn<int>(
name: "age",
table: "user",
type: "INTEGER",
nullable: true,
oldClrType: typeof(int),
oldType: "INTEGER");
migrationBuilder.AddColumn<string>(
name: "email",
table: "user",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "icon",
table: "user",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ip",
table: "user",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "is_delete",
table: "user",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "nick",
table: "user",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "password",
table: "user",
type: "TEXT",
nullable: true);
migrationBuilder.CreateTable(
name: "role",
columns: table => new
{
id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
role_name = table.Column<string>(type: "TEXT", nullable: true),
introduce = table.Column<string>(type: "TEXT", nullable: true),
is_delete = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_role", x => x.id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "role");
migrationBuilder.DropColumn(
name: "email",
table: "user");
migrationBuilder.DropColumn(
name: "icon",
table: "user");
migrationBuilder.DropColumn(
name: "ip",
table: "user");
migrationBuilder.DropColumn(
name: "is_delete",
table: "user");
migrationBuilder.DropColumn(
name: "nick",
table: "user");
migrationBuilder.DropColumn(
name: "password",
table: "user");
migrationBuilder.RenameColumn(
name: "username",
table: "user",
newName: "name");
migrationBuilder.AlterColumn<int>(
name: "age",
table: "user",
type: "INTEGER",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "INTEGER",
oldNullable: true);
}
}
}

View File

@@ -1,4 +1,5 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
@@ -15,16 +16,54 @@ namespace Yi.Framework.Model.Migrations
modelBuilder
.HasAnnotation("ProductVersion", "5.0.10");
modelBuilder.Entity("Yi.Framework.Model.Models.role", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("introduce")
.HasColumnType("TEXT");
b.Property<int>("is_delete")
.HasColumnType("INTEGER");
b.Property<string>("role_name")
.HasColumnType("TEXT");
b.HasKey("id");
b.ToTable("role");
});
modelBuilder.Entity("Yi.Framework.Model.Models.user", b =>
{
b.Property<int>("id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("age")
b.Property<int?>("age")
.HasColumnType("INTEGER");
b.Property<string>("name")
b.Property<string>("email")
.HasColumnType("TEXT");
b.Property<string>("icon")
.HasColumnType("TEXT");
b.Property<string>("ip")
.HasColumnType("TEXT");
b.Property<int>("is_delete")
.HasColumnType("INTEGER");
b.Property<string>("nick")
.HasColumnType("TEXT");
b.Property<string>("password")
.HasColumnType("TEXT");
b.Property<string>("username")
.HasColumnType("TEXT");
b.HasKey("id");

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class baseModel<T>
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public T id { get; set; }
public int is_delete { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class loopModel:baseModel<int>
{
public int? is_top { get; set; }
public int? sort { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class menu :loopModel
{
public string icon { get; set; }
public string router { get; set; }
public string menu_name { get; set; }
public menu children { get; set; }
public mould mould { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class mould:baseModel<int>
{
public string mould_name { get; set; }
public string url { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class role:baseModel
{
public string role_name { get; set; }
public string introduce { get; set; }
public ICollection<menu> menus { get; set; }
}
}

View File

@@ -7,11 +7,18 @@ using System.Threading.Tasks;
namespace Yi.Framework.Model.Models
{
public class user
public class user:baseModel<int>
{
[Key]
public int id { get; set; }
public string name { get; set; }
public int age { get; set; }
public string username { get; set; }
public string password { get; set; }
public string icon { get; set; }
public string nick { get; set; }
public string email { get; set; }
public string ip { get; set; }
public int? age { get; set; }
public string introduction { get; set; }
public ICollection<role> roles { get; set; }
}
}

View File

@@ -9,10 +9,10 @@ using Yi.Framework.Interface;
namespace Yi.Framework.Service
{
public class CCBaseServer<T> : IBaseService<T> where T : class, new()
public class BaseService<T> : IBaseService<T> where T : class, new()
{
public DbContext _Db;
public CCBaseServer(DbContext Db)
public BaseService(DbContext Db)
{
_Db = Db;
}
@@ -84,6 +84,12 @@ namespace Yi.Framework.Service
return await _Db.SaveChangesAsync() > 0;
}
public async Task<bool> UpdateListAsync(IEnumerable<T> entities)
{
_Db.Set<T>().UpdateRange(entities);
return await _Db.SaveChangesAsync() > 0;
}
public async Task<bool> DeleteAsync(T entity)
{
_Db.Set<T>().Remove(entity);
@@ -115,5 +121,10 @@ namespace Yi.Framework.Service
}
return false;
}
public async Task<T> GetEntity(Expression<Func<T, bool>> whereLambda)
{
return await _Db.Set<T>().Where(whereLambda).FirstOrDefaultAsync();
}
}
}

View File

@@ -1,8 +0,0 @@
using System;
namespace Yi.Framework.Service
{
public class Class1
{
}
}

View File

@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Service
{
public class RoleService:BaseService<role>, IRoleService
{
public RoleService(DbContext Db):base(Db)
{
}
public async Task<bool> DelListByUpdateAsync(List<int> _ids)
{
var userList = await GetEntitiesAsync(u=>_ids.Contains(u.id));
userList.ToList().ForEach(u => u.is_delete =(short)Common.Enum.DelFlagEnum.Deleted);
return await UpdateListAsync(userList);
}
public async Task<IEnumerable<role>> GetAllEntitiesTrueAsync()
{
return await _Db.Set<role>().Where(u => u.is_delete == (short)Common.Enum.DelFlagEnum.Normal).ToListAsync();
}
}
}

View File

@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
namespace Yi.Framework.Service
{
public class UserService: BaseService<user>,IUserService
{
public UserService(DbContext Db):base(Db)
{
}
public async Task<bool> DelListByUpdateAsync(List<int> _ids)
{
var userList = await GetEntitiesAsync(u => _ids.Contains(u.id));
userList.ToList().ForEach(u => u.is_delete = (short)Common.Enum.DelFlagEnum.Deleted);
return await UpdateListAsync(userList);
}
public async Task<IEnumerable<user>> GetAllEntitiesTrueAsync()
{
return await _Db.Set<user>().Where(u=>u.is_delete==(short)Common.Enum.DelFlagEnum.Normal).ToListAsync();
}
}
}