Ejemplo n.º 1
0
        public Task HandleAsync(AssignCoachCommand command)
        {
            var group = _dbContext.TrainingGroups.Include(tg => tg.PlayerTrainingGroups)
                        .FirstOrDefault(tg => tg.Id == command.GroupId);
            var coach = _dbContext.Coaches.FirstOrDefault(c => c.Id == command.CoachId);

            if (coach == null)
            {
                throw new Exception("Requested coach does not exist");
            }

            if (group != null)
            {
                group.SetCoach(command.CoachId);
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not add player to group");
                }
            }
            else
            {
                throw new Exception("Requested group does not exist");
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        public Task HandleAsync(PlayerAssignedToGroupEvent @event)
        {
            var group = _dbContext.TrainingGroups.Include(tg => tg.PlayerTrainingGroups)
                        .FirstOrDefault(tg => tg.Id == @event.GroupId);

            var player = _dbContext.Players.FirstOrDefault(p => p.Id == @event.PlayerId);

            if (player == null)
            {
                var newPlayer = new Player()
                {
                    Id = @event.PlayerId
                };
                _dbContext.Players.Add(newPlayer);
            }

            if (group != null)
            {
                group.AssignPlayer(@event.PlayerId);
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not add player to group");
                }
            }
            else
            {
                throw new Exception("Requested group does not exist");
            }

            return(Task.CompletedTask);
        }
        public async Task HandleAsync(CoachCreatedEvent @event)
        {
            var coach = new Coach(@event.CoachId, @event.Name, @event.Surname);
            await _dbContext.Coaches.AddAsync(coach);

            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add new coach");
            }
        }
        public Task HandleAsync(CreateGroupCommand command)
        {
            var group = new TrainingGroup(command.Day, command.Hour, command.LevelName);

            group.SetCoach(command.CoachId);
            _dbContext.TrainingGroups.Add(group);
            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add created group");
            }

            return(Task.CompletedTask);
        }
        public Task HandleAsync(RemoveFromGroupCommand command)
        {
            var group = _dbContext.TrainingGroups.Include(tg => tg.PlayerTrainingGroups)
                        .FirstOrDefault(tg => tg.Id == command.GroupId);

            if (group != null)
            {
                group.RemovePlayer(command.PlayerId);
                if (_dbContext.SaveChanges() == 0)
                {
                    throw new Exception("Could not add player to group");
                }
            }
            else
            {
                throw new Exception("Requested group does not exist");
            }

            return(Task.CompletedTask);
        }
        public async Task HandleAsync(FillAttendanceCommand command)
        {
            var attendantPlayers = command.AttendantPlayers.Select(id => new Player()
            {
                Id = id
            });
            var date       = DateTime.Parse(command.Date);
            var attendance = new Attendance(date, command.GroupId, attendantPlayers);

            _dbContext.Attendances.Add(attendance);
            if (_dbContext.SaveChanges() == 0)
            {
                throw new Exception("Could not add attendance");
            }
            foreach (var player in attendance.AttendantPlayers)
            {
                var @event = new PlayerAttendanceEvent()
                {
                    PlayerId = player.PlayerId
                };
                await _busPublisher.PublishAsync(@event);
            }
        }