Skip to content

Commit

Permalink
feat(api): Repository仓库添加,还未结束
Browse files Browse the repository at this point in the history
  • Loading branch information
aehyok committed Jan 22, 2022
1 parent 25d995a commit 32314dc
Show file tree
Hide file tree
Showing 23 changed files with 163 additions and 46 deletions.
18 changes: 18 additions & 0 deletions dotnet6/aehyok.Base/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace aehyok.Base
{
public abstract class BaseEntity
{
[Key]
public string Id { get; set; }

public BaseEntity()
{
Id = Guid.NewGuid().ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;

namespace aehyok.Core.Data
namespace aehyok.Base
{
/// <summary>
/// 依赖注入接口,表示该接口的实现类将自动注册到IoC容器中
Expand Down
9 changes: 9 additions & 0 deletions dotnet6/aehyok.Base/aehyok.Base.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace aehyok.Core.EntityFrameCore.MySql.Data
{
public partial class MyDbConext : DbContext
public partial class MyDbContext : DbContext
{
public MyDbConext()
public MyDbContext()
{
}

public MyDbConext(DbContextOptions<MyDbConext> options)
public MyDbContext(DbContextOptions<MyDbContext> options)
: base(options)
{
}
Expand Down
27 changes: 27 additions & 0 deletions dotnet6/aehyok.Core.EntityFrameCore.MySql/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Microsoft.EntityFrameworkCore;
using aehyok.Base;

namespace aehyok.Core.EntityFrameCore.MySql
{
public interface IRepository<TEntity> where TEntity : BaseEntity
{
DbSet<TEntity> Entities { get; }

//增加单个实体
Task<int> Insert(TEntity entity);

//增加多个实体
//int Insert(IEnumerable<TEntity> entities);

//更新实体
Task<int> Update(TEntity entity);

//删除
Task<int> Delete(object id);

//根据逐渐获取实体
Task<TEntity> GetByKey(object key);
}
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using aehyok.Base;

namespace aehyok.Core.EntityFrameCore.MySql.Models
{
public partial class FlowEntityType
public partial class FlowEntityType: BaseEntity
{
public string Id { get; set; } = null!;
//public string Id { get; set; } = null!;
public string? FlowName { get; set; }
public string? Description { get; set; }
public int? DisplayOrder { get; set; }
Expand Down
55 changes: 55 additions & 0 deletions dotnet6/aehyok.Core.EntityFrameCore.MySql/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using aehyok.Base;
using aehyok.Core.EntityFrameCore.MySql.Data;
using Microsoft.EntityFrameworkCore;

namespace aehyok.Core.EntityFrameCore.MySql
{
public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
{
MyDbContext EF = new MyDbContext();

public DbSet<TEntity> Entities
{
get { return EF.Set<TEntity>(); }
}

DbSet<TEntity> IRepository<TEntity>.Entities => throw new NotImplementedException();

public async Task<int> Insert(TEntity entity)
{
Entities.Add(entity);
return await EF.SaveChangesAsync();
}

public async Task<int> Insert(IEnumerable<TEntity> entities)
{
Entities.AddRange(entities);
return await EF.SaveChangesAsync();
}

public async Task<int> Update(TEntity entity)
{
EF.Entry(entity).State = EntityState.Modified;
return await EF.SaveChangesAsync();
}

public async Task<int> Delete(object id)
{
///删除操作实现
var item = await Entities.FindAsync(id);
if(item != null)
{
EF.Remove<TEntity>(item);
return await EF.SaveChangesAsync();
}
return 0;
}

public async Task<TEntity> GetByKey(object key)
{
return await Entities.FindAsync(key);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\aehyok.Base\aehyok.Base.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions dotnet6/aehyok.Core.Manager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aehyok.Lib", "aehyok.Lib\ae
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aehyok.Core.EntityFrameCore.MySql", "aehyok.Core.EntityFrameCore.MySql\aehyok.Core.EntityFrameCore.MySql.csproj", "{E140DF87-F4B0-4263-89DB-C23F33F6D7B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aehyok.Base", "aehyok.Base\aehyok.Base.csproj", "{6690349F-F5F1-4674-B96B-1019810DD127}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +88,10 @@ Global
{E140DF87-F4B0-4263-89DB-C23F33F6D7B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E140DF87-F4B0-4263-89DB-C23F33F6D7B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E140DF87-F4B0-4263-89DB-C23F33F6D7B3}.Release|Any CPU.Build.0 = Release|Any CPU
{6690349F-F5F1-4674-B96B-1019810DD127}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6690349F-F5F1-4674-B96B-1019810DD127}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6690349F-F5F1-4674-B96B-1019810DD127}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6690349F-F5F1-4674-B96B-1019810DD127}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 14 additions & 14 deletions dotnet6/aehyok.Core.Repository/FlowRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FlowRepository : IFlowRepository
{
public async Task<int> DeleteFlowEntityType(string flowId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var item = context.FlowEntityTypes.FirstOrDefault(t => t.Id == flowId);
context.FlowEntityTypes.Remove(item);
var result = await context.SaveChangesAsync();
Expand All @@ -23,48 +23,48 @@ public async Task<int> DeleteFlowEntityType(string flowId)

public Task<FlowEntityState> GetFlowEntityState(string stateId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var result = context.FlowEntityStates.FirstOrDefaultAsync(t => t.Id == stateId);
return result;
}

public async Task<List<FlowEntityState>> GetFlowEntityStateList(string flowId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var list = await context.FlowEntityStates.Where(item => item.FlowId == flowId).ToListAsync();
return list;
}

public Task<List<FlowStateTransition>> GetFlowEntityTransitionList(string stateId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var result = context.FlowStateTransitions.Where(t => t.StateId == stateId).ToListAsync();
return result;
}

public async Task<List<FlowStateTransition>> GetFlowEntityTransitions(string stateId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var list = await context.FlowStateTransitions.Where(item => item.StateId == stateId).ToListAsync();
return list;
}

public Task<FlowEntityType> GetFlowEntityType(string flowId)
{
var context = new MyDbConext();
var context = new MyDbContext();
return context.FlowEntityTypes.FirstOrDefaultAsync(t => t.Id == flowId);
}

public async Task<List<FlowEntityType>> GetFlowEntityTypeList()
{
var context = new MyDbConext();
var context = new MyDbContext();
var list = await context.FlowEntityTypes.ToListAsync();
return list;
}

public Task<FlowStateTransition> GetFlowStateTransition(string actionId)
{
var context = new MyDbConext();
var context = new MyDbContext();
var result = context.FlowStateTransitions.FirstOrDefaultAsync(t => t.Id == actionId);
return result;
}
Expand All @@ -73,14 +73,14 @@ public async Task<int> SaveFlowEntityState(FlowEntityState flowEntityState)
{
if (flowEntityState != null && flowEntityState.Id != null)
{
var context = new MyDbConext();
var context = new MyDbContext();
context.Update(flowEntityState);
var result = await context.SaveChangesAsync();
return result;
}
else
{
var context = new MyDbConext();
var context = new MyDbContext();
await context.FlowEntityStates.AddAsync(flowEntityState);
var result = await context.SaveChangesAsync();
return result;
Expand All @@ -91,14 +91,14 @@ public async Task<int> SaveFlowEntityType(FlowEntityType flowEntityType)
{
if (flowEntityType != null && flowEntityType.Id != null)
{
var context = new MyDbConext();
var context = new MyDbContext();
context.Update(flowEntityType);
var result = await context.SaveChangesAsync();
return result;
}
else
{
var context = new MyDbConext();
var context = new MyDbContext();
await context.FlowEntityTypes.AddAsync(flowEntityType);
var result = await context.SaveChangesAsync();
return result;
Expand All @@ -110,14 +110,14 @@ public async Task<int> SaveFlowStateTransition(FlowStateTransition flowStateTran
{
if (flowStateTransition != null && flowStateTransition.Id != null)
{
var context = new MyDbConext();
var context = new MyDbContext();
context.Update(flowStateTransition);
var result = await context.SaveChangesAsync();
return result;
}
else
{
var context = new MyDbConext();
var context = new MyDbContext();
await context.FlowStateTransitions.AddAsync(flowStateTransition);
var result = await context.SaveChangesAsync();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\aehyok.Core\aehyok.Core.csproj" />
<ProjectReference Include="..\aehyok.Core.EntityFrameCore.MySql\aehyok.Core.EntityFrameCore.MySql.csproj" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion dotnet6/aehyok.Core.WebApi/Controllers/FlowController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using aehyok.Core.EntityFrameCore.MySql.Models;
using aehyok.Core.EntityFrameCore.MySql;
using aehyok.Core.EntityFrameCore.MySql.Models;
using aehyok.Core.IRepository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand All @@ -13,6 +14,7 @@ namespace aehyok.Core.WebApi.Controllers
public class FlowController : BaseApiController
{
private readonly IFlowRepository _flowRepository;
//private readonly IRepository<FlowEntityType> _repository;

public FlowController(IFlowRepository flowRepository)
{
Expand Down
10 changes: 5 additions & 5 deletions dotnet6/aehyok.Core.WebApi/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public List<BaseUser> GetUserList(int pageIndex =0 )
{
try
{
var context = new MyDbConext();
var context = new MyDbContext();
int pagesize = 10;
var list = context.BaseUsers.Where(item=> item.IsDeleted==false).OrderByDescending(item => item.CreatedAt).Skip(pagesize * pageIndex).Take(pagesize).ToList();
this._logger.Info(list.Count);
Expand All @@ -50,7 +50,7 @@ public async Task<BaseUser> GetUser(int id)
{
try
{
var context = new MyDbConext();
var context = new MyDbContext();
var user = await context.BaseUsers.FindAsync(id);
return user;
}
Expand All @@ -74,7 +74,7 @@ public async Task<bool> SaveUser(BaseUser user)
this._logger.Info(user.Id);
if(user!=null && user.Id > 0)
{
var context = new MyDbConext();
var context = new MyDbContext();
user.CreatedAt = DateTime.Now;
user.IsDeleted = false;
user.UpdatedAt = DateTime.Now;
Expand All @@ -85,7 +85,7 @@ public async Task<bool> SaveUser(BaseUser user)
else
{
this._logger.Info(user);
var context = new MyDbConext();
var context = new MyDbContext();
user.IsDeleted = false;
user.CreatedAt= DateTime.Now;
user.UpdatedAt = DateTime.Now;
Expand All @@ -111,7 +111,7 @@ public async Task<bool> SaveUser(BaseUser user)
[AllowAnonymous]
public async Task<bool> DeleteUser(int id)
{
var context = new MyDbConext();
var context = new MyDbContext();
var user = await context.BaseUsers.FindAsync(id);
user.IsDeleted = true;
context.Update(user);
Expand Down
14 changes: 0 additions & 14 deletions dotnet6/aehyok.Core/Data/BaseEntity.cs

This file was deleted.

3 changes: 2 additions & 1 deletion dotnet6/aehyok.Core/DataBase/IDbAccossor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using aehyok.Core.Data;
using aehyok.Base;
using aehyok.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down
3 changes: 2 additions & 1 deletion dotnet6/aehyok.Core/IRepository/IAccountRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using aehyok.Core.Data;
using aehyok.Base;
using aehyok.Core.Data;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down
Loading

0 comments on commit 32314dc

Please sign in to comment.