Skip to content

Commit

Permalink
refactor: make some method async
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiwen committed May 29, 2020
1 parent 7a90a79 commit e2533df
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using AutoMapper;
using Doublelives.Api.Models.Account;
using Doublelives.Api.Models.Account.Requests;
Expand Down Expand Up @@ -29,11 +30,11 @@ public AccountController(
/// <summary>账户登陆</summary>
[AllowAnonymous]
[HttpPost("login")]
public IActionResult Login(AccountLoginRequest request)
public async Task<IActionResult> Login(AccountLoginRequest request)
{
ModelValidator<AccountLoginRequest, LoginRequestValidator>.Validate(request);

var (valid, token) = _userService.Login(request.UserName, request.Password);
var (valid, token) = await _userService.Login(request.UserName, request.Password);

if (!valid) return NotFound("用户名或密码错误,请检查");

Expand Down
5 changes: 3 additions & 2 deletions dl-api-csharp/Doublelives.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Doublelives.Api.Controllers
{
Expand All @@ -31,9 +32,9 @@ public UserController(
/// 获取分页用户列表
/// </summary>
[HttpGet("list")]
public IActionResult List([FromQuery] UserListSearchRequest request)
public async Task<IActionResult> List([FromQuery] UserListSearchRequest request)
{
var result = _userService.GetPagedList(UserMapper.ToUserSearchDto(request));
var result = await _userService.GetPagedList(UserMapper.ToUserSearchDto(request));

var viewModel = new UserPagedListViewModel
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Doublelives.Domain.Pictures;
using System.Threading.Tasks;
using Doublelives.Domain.Pictures;
using Doublelives.Domain.Sys;
using Doublelives.Domain.Users;
using Doublelives.Persistence.Repositories;
Expand Down Expand Up @@ -26,5 +27,7 @@ public interface IUnitOfWork
IMenuRepository MenuRepository { get; }

void Commit();

Task CommitAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Doublelives.Domain.Pictures;
using System.Threading.Tasks;
using Doublelives.Domain.Pictures;
using Doublelives.Domain.Sys;
using Doublelives.Domain.Users;
using Doublelives.Persistence.Repositories;
Expand Down Expand Up @@ -45,5 +46,10 @@ public void Commit()
{
_albumDbContext.SaveChanges();
}

public async Task CommitAsync()
{
await _albumDbContext.SaveChangesAsync();
}
}
}
12 changes: 6 additions & 6 deletions dl-api-csharp/Doublelives.Service/Users/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace Doublelives.Service.Users
{
public interface IUserService
{
(bool, string) Login(string username, string pwd);
Task<(bool, string)> Login(string username, string pwd);

string GenerateToken(long id);

Task<SysUser> GetById(long id);

void Add(SysUser user);
Task Add(SysUser user);

void Update(UserUpdateDto dto);
Task Update(UserUpdateDto dto);

void Delete(long id);
Task Delete(long id);

AccountInfoDto GetInfo(long userid);
Task<AccountInfoDto> GetInfo(long userid);

PagedModel<AccountProfileDto> GetPagedList(UserSearchDto userSearchDto);
Task<PagedModel<AccountProfileDto>> GetPagedList(UserSearchDto userSearchDto);
}
}
33 changes: 18 additions & 15 deletions dl-api-csharp/Doublelives.Service/Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public UserService(
_roleService = roleService;
}

public (bool, string) Login(string account, string pwd)
public async Task<(bool, string)> Login(string account, string pwd)
{
var user = GetByAccountName(account);
var user = await GetByAccountName(account);

if (user == null) return (false, null);

Expand Down Expand Up @@ -86,9 +86,9 @@ public string GenerateToken(long id)
return tokenString;
}

public AccountInfoDto GetInfo(long userid)
public async Task<AccountInfoDto> GetInfo(long userid)
{
var user = GetById(userid).Result;
var user = await GetById(userid);
if (user == null) throw new UserNotFoundException();

var roles = new List<SysRole>();
Expand All @@ -112,7 +112,7 @@ public AccountInfoDto GetInfo(long userid)
return UserMapper.ToAccountInfoDto(user, dept, roles, permissions);
}

public PagedModel<AccountProfileDto> GetPagedList(UserSearchDto criteria)
public async Task<PagedModel<AccountProfileDto>> GetPagedList(UserSearchDto criteria)
{
// 排除"应用系统"用户
Expression<Func<SysUser, bool>> condition = it => it.Id > 0;
Expand All @@ -123,7 +123,7 @@ public PagedModel<AccountProfileDto> GetPagedList(UserSearchDto criteria)
if (!string.IsNullOrWhiteSpace(criteria.Name))
condition = condition.And(it => it.Name.Contains(criteria.Name));

var result = _unitOfWork.UserRepository.Paged(
var result = await _unitOfWork.UserRepository.PagedAsync(
criteria.Page,
criteria.Limit,
condition,
Expand Down Expand Up @@ -157,24 +157,27 @@ public async Task<SysUser> GetById(long id)
return user;
}

public SysUser GetByAccountName(string account)
public async Task<SysUser> GetByAccountName(string account)
{
var user = _unitOfWork.UserRepository.GetAsQueryable().FirstOrDefault(it => it.Account == account);
var user = await _unitOfWork
.UserRepository
.GetAsQueryable()
.FirstOrDefaultAsync(it => it.Account == account);

return user;
}

public void Add(SysUser user)
public async Task Add(SysUser user)
{
_unitOfWork.UserRepository.Insert(user);
_unitOfWork.Commit();
await _unitOfWork.UserRepository.InsertAsync(user);
await _unitOfWork.CommitAsync();

_cacheManager.Remove(GetUserCacheKey(user.Id));
}

public void Update(UserUpdateDto request)
public async Task Update(UserUpdateDto request)
{
var user = GetById(request.Id).Result;
var user = await GetById(request.Id);
if (user == null) throw new NotFoundException();

user.Account = request.Account;
Expand All @@ -194,10 +197,10 @@ public void Update(UserUpdateDto request)
_cacheManager.Remove(GetUserCacheKey(user.Id));
}

public void Delete(long id)
public async Task Delete(long id)
{
_unitOfWork.UserRepository.DeleteById(id);
_unitOfWork.Commit();
await _unitOfWork.CommitAsync();

_cacheManager.Remove(GetUserCacheKey(id));
}
Expand Down

0 comments on commit e2533df

Please sign in to comment.