Пример #1
0
        private async static Task TryInitPlayers(ApplicationDbContext context)
        {
            if (await context.Players.AnyAsync())
            {
                return;
            }

            var team = await context.Teams.FirstOrDefaultAsync();

            var francoNameResult    = CompleteName.Create("Franco", "Armani");
            var quinterosNameResult = CompleteName.Create("Franco", "Armani");

            var francoEmail    = Email.Create("*****@*****.**");
            var quinterosEmail = Email.Create("*****@*****.**");

            await context.Players.AddRangeAsync(new List <Player>()
            {
                new Player(francoNameResult.Value, new DateTime(1986, 10, 16), francoEmail.Value, "1132123123", team),
                new Player(quinterosNameResult.Value, new DateTime(1993, 1, 18), quinterosEmail.Value, "1132123123", team)
            });

            await context.SaveChangesAsync();
        }
Пример #2
0
        public async Task <IActionResult> Create([FromBody] PlayerCreateDto player)
        {
            var team = await _teamRepository.GetByIdAsync(player.TeamId);

            if (team == null)
            {
                return(BadRequest($"Invalid country Id { player.TeamId }"));
            }

            if (await _playerRepository.ExistAsync(p => p.Email == player.Email))
            {
                return(BadRequest($"Invalid email - It is already registered in the system"));
            }

            var completeNameResult = CompleteName.Create(player.FirstName, player.LastName);

            if (completeNameResult.IsFailure)
            {
                return(BadRequest(completeNameResult.Error));
            }

            var emailResult = Email.Create(player.Email);

            if (emailResult.IsFailure)
            {
                return(BadRequest(emailResult.Error));
            }

            var newPlayer = new Player(completeNameResult.Value, player.BirthDate, emailResult.Value, player.Phone, team);

            await _playerRepository.AddAsync(newPlayer);

            await _unitOfWork.Commit();

            return(Ok());
        }