コード例 #1
0
        public async Task <MemberDto> Handle(AddMemberCommand request, CancellationToken cancellationToken)
        {
            var pesel = request.Member.Pesel;

            var memberEntity = request.Member.Adapt <Member>();

            memberEntity.BirthDate = BirthDayDateCalculator.ExtractFromPesel(memberEntity.Pesel);

            _ = await _dbContext.AddAsync(memberEntity, cancellationToken);

            _ = await _dbContext.SaveChangesAsync(cancellationToken);

            var newMember = await _dbContext
                            .Members
                            .SingleOrDefaultAsync(m => m.Pesel == pesel, cancellationToken);

            if (newMember is null)
            {
                throw new NullReferenceException("Failed to add member. Try again");
            }

            var newMemberDto = newMember.Adapt <MemberDto>();

            _addMemberSender.SendMember(newMember);

            return(newMemberDto);
        }
コード例 #2
0
        public async Task <MemberDto> Handle(EditMemberCommand request, CancellationToken cancellationToken)
        {
            var memberEntity = await _dbContext
                               .Members
                               .SingleOrDefaultAsync(m => m.MemberId == request.MemberId, cancellationToken);

            if (memberEntity is null)
            {
                throw new NullReferenceException($"Member [memberId: {request.MemberId}] not found");
            }

            var memberBeforeChange = memberEntity;

            var editedMember = request.Member;

            editedMember.ApplyTo(memberEntity, ModelState);

            memberEntity.BirthDate = BirthDayDateCalculator.ExtractFromPesel(memberEntity.Pesel);

            var memberAfterChange = memberEntity;
            var validator         =
                new MemberValidator(_dbContext, new Tuple <Member, Member>(memberBeforeChange, memberAfterChange));
            var validationResult = await validator.ValidateAsync(memberEntity, cancellationToken);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors.ToList());
            }

            _ = await _dbContext.SaveChangesAsync(cancellationToken);

            var patchedMemberEntity = await _dbContext
                                      .Members
                                      .SingleOrDefaultAsync(m => m.MemberId == request.MemberId, cancellationToken);

            if (patchedMemberEntity is null)
            {
                throw new NullReferenceException($"Failed to fetch patched membership [Id: {request.MemberId}]");
            }

            _editMemberSender.EditMember(patchedMemberEntity);

            var patchedMemberDto = patchedMemberEntity.Adapt <MemberDto>();

            return(patchedMemberDto);
        }