public async Task <Result <SurveyModel, Error> > Handle(CreateSurveyCommand request, CancellationToken cancellationToken) { try { var userInfo = await _userService.GetUserInfo(cancellationToken); var surveyOwner = await _surveyContext.Users.FirstAsync(user => user.ExternalUserId == userInfo.Id, cancellationToken); var survey = new Survey(surveyOwner, NonEmptyString.Create(request.SurveyTopic), request.NumberOfRespondents, NonEmptyString.Create(request.RespondentType)); survey.AddSurveyOptions(request.SurveyOptions.Select(option => new SurveyOption(NonEmptyString.Create(option.OptionText), option.PreferredNumberOfVotes))); survey.CalculateOutcome(); await _surveyContext.Surveys.AddAsync(survey, cancellationToken); await _surveyContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <SurveyModel>(survey)); } catch (SurveyDomainException e) { return(new Error("survey.domain.exception", e.Message)); } }
public async Task <Result <UserModel, Error> > Handle(RegisterUserCommand request, CancellationToken cancellationToken) { var userInfo = await _userService.GetUserInfo(cancellationToken); if (await _surveyContext.Users.AnyAsync(user => user.ExternalUserId == userInfo.Id, cancellationToken)) { return(Result.Failure <UserModel, Error>(Errors.General.UserAlreadyRegistered(userInfo.Id))); } var newUser = new User(NonEmptyString.Create(userInfo.DisplayName), NonEmptyString.Create(userInfo.EmailAddress), NonEmptyString.Create(userInfo.Id)); await _surveyContext.Users.AddAsync(newUser, cancellationToken); await _surveyContext.SaveChangesAsync(cancellationToken); return(Result.Success <UserModel, Error>(_mapper.Map <UserModel>(newUser))); }