-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from lazaro-ansaldi/master
Add support to ExecuteDelete and ExecuteUpdate
- Loading branch information
Showing
6 changed files
with
513 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,111 @@ | ||
using System; | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MockQueryable.Sample | ||
{ | ||
public class MyService | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
|
||
public static void Initialize() | ||
public class MyService | ||
{ | ||
Mapper.Initialize(cfg => cfg.CreateMap<UserEntity, UserReport>() | ||
.ForMember(dto => dto.FirstName, conf => conf.MapFrom(ol => ol.FirstName)) | ||
.ForMember(dto => dto.LastName, conf => conf.MapFrom(ol => ol.LastName))); | ||
Mapper.Configuration.AssertConfigurationIsValid(); | ||
private readonly IUserRepository _userRepository; | ||
|
||
public static void Initialize() | ||
{ | ||
Mapper.Initialize(cfg => cfg.CreateMap<UserEntity, UserReport>() | ||
.ForMember(dto => dto.FirstName, conf => conf.MapFrom(ol => ol.FirstName)) | ||
.ForMember(dto => dto.LastName, conf => conf.MapFrom(ol => ol.LastName))); | ||
Mapper.Configuration.AssertConfigurationIsValid(); | ||
} | ||
|
||
public MyService(IUserRepository userRepository) | ||
{ | ||
_userRepository = userRepository; | ||
} | ||
|
||
public async Task CreateUserIfNotExist(string firstName, string lastName, DateTime dateOfBirth) | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
|
||
if (await query.AnyAsync(x => x.LastName == lastName && x.DateOfBirth == dateOfBirth)) | ||
{ | ||
throw new ApplicationException("User already exist"); | ||
} | ||
|
||
var existUser = await query.FirstOrDefaultAsync(x => x.FirstName == firstName); | ||
if (existUser != null) | ||
{ | ||
throw new ApplicationException("User with FirstName already exist"); | ||
} | ||
|
||
if (await query.CountAsync(x => x.DateOfBirth == dateOfBirth.Date) > 3) | ||
{ | ||
throw new ApplicationException("Users with DateOfBirth more than limit"); | ||
} | ||
|
||
await _userRepository.CreateUser(new UserEntity | ||
{ | ||
FirstName = firstName, | ||
LastName = lastName, | ||
DateOfBirth = dateOfBirth.Date, | ||
}); | ||
|
||
} | ||
|
||
public async Task<List<UserReport>> GetUserReports(DateTime dateFrom, DateTime dateTo) | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
|
||
query = query.Where(x => x.DateOfBirth >= dateFrom.Date); | ||
query = query.Where(x => x.DateOfBirth <= dateTo.Date); | ||
|
||
return await query.Select(x => new UserReport | ||
{ | ||
FirstName = x.FirstName, | ||
LastName = x.LastName, | ||
}).ToListAsync(); | ||
} | ||
|
||
|
||
public async Task<List<UserReport>> GetUserReportsAutoMap(DateTime dateFrom, DateTime dateTo) | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
|
||
query = query.Where(x => x.DateOfBirth >= dateFrom.Date); | ||
query = query.Where(x => x.DateOfBirth <= dateTo.Date); | ||
|
||
return await query.ProjectTo<UserReport>().ToListAsync(); | ||
} | ||
} | ||
|
||
public MyService(IUserRepository userRepository) | ||
public interface IUserRepository | ||
{ | ||
_userRepository = userRepository; | ||
} | ||
IQueryable<UserEntity> GetQueryable(); | ||
|
||
public async Task CreateUserIfNotExist(string firstName, string lastName, DateTime dateOfBirth) | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
|
||
if (await query.AnyAsync(x => x.LastName == lastName && x.DateOfBirth == dateOfBirth)) | ||
{ | ||
throw new ApplicationException("User already exist"); | ||
} | ||
|
||
var existUser = await query.FirstOrDefaultAsync(x => x.FirstName == firstName); | ||
if (existUser != null) | ||
{ | ||
throw new ApplicationException("User with FirstName already exist"); | ||
} | ||
|
||
if (await query.CountAsync(x => x.DateOfBirth == dateOfBirth.Date) > 3) | ||
{ | ||
throw new ApplicationException("Users with DateOfBirth more than limit"); | ||
} | ||
|
||
await _userRepository.CreateUser(new UserEntity | ||
{ | ||
FirstName = firstName, | ||
LastName = lastName, | ||
DateOfBirth = dateOfBirth.Date, | ||
}); | ||
Task CreateUser(UserEntity user); | ||
|
||
} | ||
Task<List<UserEntity>> GetAll(); | ||
|
||
public async Task<List<UserReport>> GetUserReports(DateTime dateFrom, DateTime dateTo) | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
IAsyncEnumerable<UserEntity> GetAllAsync(); | ||
|
||
query = query.Where(x => x.DateOfBirth >= dateFrom.Date); | ||
query = query.Where(x => x.DateOfBirth <= dateTo.Date); | ||
Task<int> DeleteUserAsync(Guid id); | ||
|
||
return await query.Select(x => new UserReport | ||
{ | ||
FirstName = x.FirstName, | ||
LastName = x.LastName, | ||
}).ToListAsync(); | ||
Task<int> UpdateFirstNameByIdAsync(Guid id, string firstName); | ||
} | ||
|
||
|
||
public async Task<List<UserReport>> GetUserReportsAutoMap(DateTime dateFrom, DateTime dateTo) | ||
public class UserReport | ||
{ | ||
var query = _userRepository.GetQueryable(); | ||
|
||
query = query.Where(x => x.DateOfBirth >= dateFrom.Date); | ||
query = query.Where(x => x.DateOfBirth <= dateTo.Date); | ||
|
||
return await query.ProjectTo<UserReport>().ToListAsync(); | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} | ||
} | ||
|
||
public interface IUserRepository | ||
{ | ||
IQueryable<UserEntity> GetQueryable(); | ||
|
||
Task CreateUser(UserEntity user); | ||
|
||
Task<List<UserEntity>> GetAll(); | ||
|
||
IAsyncEnumerable<UserEntity> GetAllAsync(); | ||
} | ||
|
||
|
||
public class UserReport | ||
{ | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} | ||
|
||
public class UserEntity | ||
{ | ||
public Guid Id { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public DateTime DateOfBirth { get; set; } | ||
} | ||
public class UserEntity | ||
{ | ||
public Guid Id { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public DateTime DateOfBirth { get; set; } | ||
} | ||
} |
Oops, something went wrong.