125 lines
3.1 KiB
C#
125 lines
3.1 KiB
C#
using Bunny.Common.Context.Database;
|
|
using Bunny.Common.Exception;
|
|
using Bunny.Common.Utils.Net;
|
|
using Bunny.Dao.Common.Result;
|
|
using Bunny.Dao.Dto.System;
|
|
using Bunny.Dao.Entity.System.User;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace Bunny.Service.IService.Service;
|
|
|
|
public class UserService : IUserService
|
|
{
|
|
[Inject] public required EfCoreContext DbContext { get; set; }
|
|
|
|
/// <summary>
|
|
/// 添加Blog
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public void AddBlog(Users dto)
|
|
{
|
|
DbContext.Add(dto);
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询BLog
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<Users> QueryBlog()
|
|
{
|
|
return DbContext.Users.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新Blog内容
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
public void UpdateBlog(BlogUpdateDto dto)
|
|
{
|
|
var blog = new Users();
|
|
NetUtil.CopyProperties(dto, blog);
|
|
|
|
DbContext.Users.Update(blog);
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除BLog
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
public void DeleteBlog(string id)
|
|
{
|
|
var blog = new Users { Id = Guid.NewGuid().ToString() };
|
|
DbContext.Users.Remove(blog);
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量添加数据
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
public void AddBatchBlogs(string url)
|
|
{
|
|
var list = new List<Users>();
|
|
|
|
for (var i = 0; i <= 100; i++)
|
|
list.Add(new Users { Username = $"Blog{i}" });
|
|
|
|
DbContext.Users.AddRange(list);
|
|
DbContext.SaveChanges();
|
|
throw new BunnyException("测试事务注解");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除BLog
|
|
/// </summary>
|
|
public void DeleteBatchBlogs()
|
|
{
|
|
var list = new List<Users>();
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
var blog = new Users();
|
|
list.Add(blog);
|
|
}
|
|
|
|
DbContext.Users.RemoveRange(list);
|
|
DbContext.SaveChanges();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量更新_带事务
|
|
/// </summary>
|
|
public void UseTransaction()
|
|
{
|
|
// 还可以使用异步的
|
|
var transaction = DbContext.Database.BeginTransaction();
|
|
|
|
// 执行批量更新操作
|
|
var list = new List<Users>();
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
var blog = new Users { Username = "transactions" };
|
|
list.Add(blog);
|
|
}
|
|
|
|
// 更新内容
|
|
DbContext.Users.UpdateRange(list);
|
|
DbContext.SaveChanges();
|
|
|
|
// 还可以使用异步方式
|
|
transaction.Commit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
public Task<PageResult<Users>> QueryPage(int page, int limit)
|
|
{
|
|
var pagedListAsync = GetPagedResultAsync.GetPagedListFromSqlAsync<Users>(DbContext, "select * from System_Users ", "System_Users", page, limit);
|
|
return pagedListAsync;
|
|
}
|
|
} |