private TeamAggregate GetValidTeam() { return(TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember }, new List <TournamentId>())); }
private static void UpdateMembers(TeamAggregate aggregate, List <UserTeam> currentMembers) { currentMembers.ForEach(uT => { var member = aggregate.Members.FirstOrDefault(m => m.Id.Value == uT.UserId); uT.IsAdmin = member?.IsAdmin ?? uT.IsAdmin; }); }
public async Task <string> Handle(CreateTeamCommand request, CancellationToken cancellationToken) { var adminMember = new MemberEntity(_currentUserService.UserId, true); var team = TeamAggregate.CreateNew(await _teamRepository.NextIdAsync(), request.Name, adminMember); await _teamRepository.SetAsync(team); return(team.Id.ToString()); }
private async Task <Team> GetTeam(TeamAggregate aggregate) { return(await GetIncludeQuery(_context.Teams) .FirstOrDefaultAsync(t => !t.IsDeleted && t.Id == aggregate.Id.Value) ?? new Team { Id = aggregate.Id.Value }); }
public async Task <TeamId> SetAsync(TeamAggregate aggregate) { var team = await ToModel(aggregate); _context.Teams.Upsert(team); await _context.SaveChangesAsync(); return(new TeamId(team.Id)); }
public void validate_member_deletion_should_work_if_same_user() { var targetedMember = GetCommonMember(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, targetedMember }, new List <TournamentId>()); team.ValidateMemberDeletionByMember(targetedMember.Id, targetedMember.Id); }
public void add_member_should_throw_if_user_already_exists() { var existingUser = GetCommonMember(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, existingUser }, new List <TournamentId>()); Assert.Throws <DomainException>(() => team.AddMember(existingUser)); }
private List <UserTeam> GetNewMembers(TeamAggregate aggregate, List <UserTeam> currentMembers) { var newMembers = aggregate.Members .Where(m => currentMembers.All(uT => uT.UserId != m.Id.Value)) .Select(m => new UserTeam { IsAdmin = m.IsAdmin, JoinDate = _timeService.Now(), UserId = m.Id.Value }) .ToList(); return(newMembers); }
public void leave_not_joined_tournament_should_throw() { var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember }, new List <TournamentId> { TestsHelper.GetTournamentId() }); Assert.Throws <DomainException>(() => team.LeaveTournament(TestsHelper.GetTournamentId(), _adminMember.Id)); }
public void elevate_member_should_work() { var targetMember = GetCommonMember(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, targetMember }, new List <TournamentId>()); team.ElevateMember(targetMember.Id); Assert.AreEqual(true, targetMember.IsAdmin); Assert.AreEqual(false, _adminMember.IsAdmin); }
public void remove_member_should_work() { var targetedMember = GetCommonMember(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, targetedMember }, new List <TournamentId>()); team.RemoveMember(targetedMember.Id); Assert.AreEqual(1, team.Members.Count); CollectionAssert.DoesNotContain(team.Members, targetedMember); }
public void leave_tournament_should_work() { var existingTournamentId = TestsHelper.GetTournamentId(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember }, new List <TournamentId> { existingTournamentId }); team.LeaveTournament(existingTournamentId, _adminMember.Id); CollectionAssert.DoesNotContain(team.TournamentIds, existingTournamentId); }
public void leave_tournament_without_admin_account_should_throw() { var commonMember = GetCommonMember(); var existingTournamentId = TestsHelper.GetTournamentId(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, commonMember }, new List <TournamentId> { existingTournamentId }); Assert.Throws <DomainException>(() => team.LeaveTournament(existingTournamentId, commonMember.Id)); }
public void validate_member_deletion_should_throw_if_not_admin_and_not_same_user() { var noRightsMember = GetCommonMember(); var targetedMember = GetCommonMember(); var team = TeamAggregate.Restore(_teamId, _teamName, new List <MemberEntity> { _adminMember, noRightsMember, targetedMember }, new List <TournamentId>()); Assert.Throws <DomainException>(() => { team.ValidateMemberDeletionByMember(noRightsMember.Id, targetedMember.Id); }); }
private async Task <Team> ToModel(TeamAggregate aggregate) { var team = await GetTeam(aggregate); var leavingMembers = GetLeavingMembers(aggregate, team); var currentMembers = GetCurrentMembers(team); var newMembers = GetNewMembers(aggregate, currentMembers); var leavingParticipations = GetLeavingParticipations(aggregate, team); UpdateMembers(aggregate, currentMembers); newMembers.ForEach(uT => team.UserTeams.Add(uT)); leavingMembers.ForEach(uT => uT.LeaveDate = _timeService.Now()); leavingParticipations.ForEach(p => p.Deactivated = true); team.Name = aggregate.Name; return(team); }
private Task <TeamAggregate> ToEntity(Team team) { var aggregate = TeamAggregate.Restore( new TeamId(team.Id), team.Name, team.UserTeams .Where(uT => uT.LeaveDate == null && !uT.User.IsDeleted) .Select(uT => new MemberEntity(new UserId(uT.UserId), uT.IsAdmin)) .ToList(), team.ActiveParticipations .Select(p => new TournamentId(p.Tournament.Id)) .Distinct() .ToList() ); return(aggregate.ToTask()); }
private List <Participation> GetLeavingParticipations(TeamAggregate aggregate, Team team) { return(team.ActiveParticipations .Where(p => aggregate.TournamentIds.All(t => t.Value != p.Tournament.Id)) .ToList()); }
private static List <UserTeam> GetLeavingMembers(TeamAggregate aggregate, Team team) { return(team.UserTeams .Where(uT => uT.LeaveDate == null && aggregate.Members.All(m => m.Id.Value != uT.UserId)) .ToList()); }
public void create_team_without_admin_should_throw() { Assert.Throws <DomainException>(() => { TeamAggregate.CreateNew(_teamId, _teamName, GetCommonMember()); }); }