Пример #1
0
        public async Task <IActionResult> Create(
            [FromBody] PromoterRegistration registration)
        {
            if (registration == null)
            {
                return(BadRequest("Promoter registration not given"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("The given promoter registration is invalid"));
            }

            var result = await _promoterCreator.Create(registration);

            if (result.Successful())
            {
                return(Ok(result.Body()));
            }
            return(BadRequest(result.GetAggregatedErrors()));
        }
Пример #2
0
        public async Task <IServiceResult <PromoterViewModel> > Create(
            PromoterRegistration registration)
        {
            //var instituteResult = await _instituteGetter.IsExists(registration.InstituteId);
            if (!(await _instituteGetter.IsExists(registration.InstituteId)))
            {
                return(ServiceResult <PromoterViewModel> .Error($"Institute with id {registration.InstituteId} does not exist"));
            }

            var userResult =
                await _userCreator
                .CreateUserWithId(
                    registration.Email,
                    registration.Password,
                    new RoleType[] {
                RoleType.Promoter
            });

            if (!userResult.Successful())
            {
                return(ServiceResult <PromoterViewModel> .Error(userResult.GetAggregatedErrors()));
            }

            //var user = userResult.Body();
            var promoter = _mapper.Map <Promoter>(registration);

            //promoter.Id = Guid.NewGuid();
            promoter.UserId = userResult.Body();

            _context.Promoters.Add(promoter);
            await _context.SaveChangesAsync();

            var promoterViewModel = _mapper.Map <PromoterViewModel>(promoter);

            return(ServiceResult <PromoterViewModel> .Success(promoterViewModel));
        }