вот мои объекты и их репозитории:
Код: Выделить всё
ApplicationUserКод: Выделить всё
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IList UserRewards { get; set; }
}
Код: Выделить всё
RewardКод: Выделить всё
public class Reward
{
public Guid RewardId { get; set; }
public string Name { get; set; }
public IList UserRewards { get; set; }
}
Код: Выделить всё
RewardRepository : BaseRepositoryКод: Выделить всё
public class BaseRepository : IAsyncRepository where T : class
{
private readonly AppDbContext context;
public BaseRepository(AppDbContext context)
{
this.context = context;
}
public async Task AddAsync(T entity)
{
await context.Set().AddAsync(entity);
await context.SaveChangesAsync();
return entity;
}
}
Код: Выделить всё
UserRewardКод: Выделить всё
public class UserReward
{
public Guid ApplicationUserId { get; set; }
public ApplicationUser User { get; set; }
public Guid RewardId { get; set; }
public Reward Reward { get; set; }
}
Код: Выделить всё
UserRewardRepository : BaseRepositoryКод: Выделить всё
public class BaseRepository : IAsyncRepository where T : class
{
private readonly AppDbContext context;
public BaseRepository(AppDbContext context)
{
this.context = context;
}
public async Task AddAsync(T entity)
{
await context.Set().AddAsync(entity);
await context.SaveChangesAsync();
return entity;
}
}
Код: Выделить всё
AppDbContextКод: Выделить всё
public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions options)
: base(options)
{
}
// This is used to return the UTC time when queried, knowing
// that SQL has an issue returning UTC time by not adding the "z"
// suffix character that tells JS that the time is UTC.
// It's added to entity configurations using .HasConversion(AppDbContext.utcConverter).
public static ValueConverter utcConverter = new ValueConverter(
toDb => toDb,
fromDb =>
DateTime.SpecifyKind(fromDb, DateTimeKind.Utc));
public DbSet Categories { get; set; }
public DbSet Rewards { get; set; }
public DbSet UserReward { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ApplicationUserConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration());
modelBuilder.ApplyConfiguration(new RewardConfiguration());
modelBuilder.ApplyConfiguration(new UserRewardConfiguration());
}
Код: Выделить всё
AddRewardCommandКод: Выделить всё
public class AddRewardCommand : IRequest
{
public string Name { get; set; }
}
Код: Выделить всё
AddRewardCommandHandlerКод: Выделить всё
public class AddRewardCommandHandler
: IRequestHandler
{
private readonly IMapper mapper;
private readonly IRewardRepository repository;
private readonly IUserRewardRepository userRewardRepository;
private readonly IAuthenticationService authenticationService;
public AddRewardCommandHandler(IMapper mapper,
IRewardRepository repository,
IUserRewardRepository userRewardRepository,
IAuthenticationService authenticationService)
{
this.mapper = mapper
?? throw new ArgumentNullException(nameof(mapper));
this.repository = repository
?? throw new ArgumentNullException(nameof(repository));
this.userRewardRepository = userRewardRepository
?? throw new ArgumentNullException(nameof(userRewardRepository));
this.authenticationService = authenticationService
?? throw new ArgumentNullException(nameof(authenticationService));
}
public async Task Handle(AddRewardCommand request, CancellationToken cancellationToken)
{
Domain.Entities.Reward reward;
// Get all users
var userList= await authenticationService.GetAllUsersWithPhoneConfirmedAsync();
// Create a new reward with the HasBeenUsed property false
var newReward = new Domain.Entities.Reward()
{
RewardId = Guid.NewGuid(),
Name = request.Name
};
// Foreach user in the DB, add this new reward to their account
foreach (GetUserResponse user in userList)
{
var userReward = new Domain.Entities.UserReward()
{
ApplicationUserId = user.UserId,
RewardId = newReward.RewardId
};
// /!\
// THIS IS WHERE IT DOESN'T WORK
var t = await userRewardRepository.AddAsync(userReward);
}
try
{
reward = mapper.Map(request);
}
catch (AutoMapperMappingException autoMapperException)
{
throw autoMapperException.InnerException;
}
reward = await repository.AddAsync(reward);
return reward.RewardId;
}
}
Код: Выделить всё
var t = await userRewardRepository.AddAsync(userReward); Подробнее здесь: https://stackoverflow.com/questions/791 ... rs-command
Мобильная версия