public async Task <ApproveCharityResponse> Handle(ApproveCharityRequest message)
        {
            var charity = await _context.Charities.FirstOrDefaultAsync(b => b.CharityKey.Equals(message.CharityKey));

            if (charity != null)
            {
                if (charity.IsApproved)
                {
                    return new ApproveCharityResponse
                           {
                               ErrorType = ErrorType.AlreadyActive
                           }
                }
                ;

                charity.IsApproved = true;
                if (!await _context.TrySaveChangesAsync())
                {
                    return(new ApproveCharityResponse());
                }

                var charityCreatedEvent = _mapper.Map <CharityCreatedEvent>(charity);
                await _bus.PublishAsync(charityCreatedEvent);

                return(ApproveCharityResponse.Success());
            }

            return(new ApproveCharityResponse
            {
                ErrorType = ErrorType.NotFound
            });
        }
    }
        public override async Task <ApproveCharityResponse> Handle(ApproveCharityRequest message)
        {
            var charity = await _context.Charities.FirstOrDefaultAsync(b => b.CharityKey.Equals(message.CharityKey));

            if (charity != null)
            {
                if (charity.IsApproved)
                {
                    return(Error(ErrorType.AlreadyActive));
                }

                charity.IsApproved = true;

                if (await _context.TrySaveChangesAsync())
                {
                    await _bus.PublishAsync(_mapper.Map <CharityCreatedEvent>(charity));

                    return(Success());
                }

                return(Error(ErrorType.DatabaseError));
            }

            return(Error(ErrorType.NotFound));
        }
Esempio n. 3
0
        public override async Task <CreateCharityResponse> Handle(CreateCharityRequest message)
        {
            var charity = _mapper.Map <Charity>(message);

            if (charity.CharityKey == default)
            {
                return(Error(ErrorType.InvalidKey));
            }

            _context.Charities.Add(charity);

            return(await _context.TrySaveChangesAsync <CreateCharityResponse>());
        }
Esempio n. 4
0
        public override async Task <UpdateCharityResponse> Handle(UpdateCharityRequest message)
        {
            var charity = await _context.Charities.FirstOrDefaultAsync(u => u.CharityKey == message.CharityKey);

            if (charity == null)
            {
                return(Error(ErrorType.NotFound));
            }

            _context.Entry(charity).CurrentValues.SetValues(message);

            if (await _context.TrySaveChangesAsync())
            {
                await _bus.PublishAsync(_mapper.Map <CharityUpdatedEvent>(charity));

                return(Success());
            }

            return(Error(ErrorType.DatabaseError));
        }
        public async Task <UpdateCharityResponse> Handle(UpdateCharityRequest message)
        {
            var charity = await _context.Charities.FirstOrDefaultAsync(u => u.CharityKey == message.CharityKey);

            if (charity == null)
            {
                return(new UpdateCharityResponse
                {
                    ErrorType = ErrorType.NotFound
                });
            }

            _context.Entry(charity).CurrentValues.SetValues(message);
            if (!await _context.TrySaveChangesAsync())
            {
                return(new UpdateCharityResponse());
            }

            var charityUpdatedEvent = _mapper.Map <CharityUpdatedEvent>(charity);
            await _bus.PublishAsync(charityUpdatedEvent);

            return(UpdateCharityResponse.Success());
        }