示例#1
0
        /// <summary>
        /// Creates the specified league.
        /// </summary>
        /// <param name="leagueDto">The entity.</param>
        /// <returns>The league that was created.</returns>
        public async Task <LeagueDTO> Create(LeagueDTO leagueDto)
        {
            var result = await this.Handler.Execute(_log, async() =>
            {
                League league = _leagueFactory.CreateDomainObject(leagueDto);
                league.Validate();

                league = await _leagueRepository.Create(league);
                return(_leagueMapper.ToDto(league));
            });

            return(result);
        }
示例#2
0
        public League Create(League league)
        {
            var leages    = _leagueRepository.GetAll().ToList();
            var duplicate = leages.FirstOrDefault(l => l.Name == league.Name);

            if (duplicate != null)
            {
                return(duplicate);
            }
            var id = leages.Count;

            league.Id = id + 1;
            _leagueRepository.Create(league);
            return(league);
        }
        public async Task <IActionResult> AddLeague(LeagueForCreateDto league)
        {
            if (await leagueRepository.NameExists(league.Name))
            {
                return(BadRequest("League already exists. Choose different name."));
            }

            int loggedUserId;

            Int32.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out loggedUserId);

            if (await leagueRepository.CountUserLeagues(loggedUserId) >= 5)
            {
                return(BadRequest("You cannot be a member of more than 5 leagues."));
            }

            var leagueToCreate = mapper.Map <League>(league);

            leagueToCreate.Admin = await userRepository.GetUser(loggedUserId);

            leagueRepository.Create(leagueToCreate);

            var userInLeague = new UserInLeague
            {
                User   = leagueToCreate.Admin,
                League = leagueToCreate
            };

            leagueRepository.Join(userInLeague);

            if (await dataContext.Commit())
            {
                return(CreatedAtRoute("GetLeague", new { id = leagueToCreate.Id }, leagueToCreate));
            }

            return(BadRequest("Could not add league"));
        }