public async Task <Unit> Handle(DeletePersonCommand request, CancellationToken cancellationToken)
        {
            foreach (var id in request.Ids)
            {
                var person = await _context.Persons.FindAsync(id);

                if (person is null)
                {
                    throw new Exception("Kişi bulunamadı");
                }

                var hasPhoneNumbers = _context.PhoneNumbers.Any(pn => pn.PersonId == person.Id);
                if (hasPhoneNumbers)
                {
                    throw new Exception($"{person.FirstName} {person.LastName} kişisine " +
                                        "kayıtlı telefon numaraları var");
                }

                _context.Persons.Remove(person);
            }

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreatePhoneNumberCommand request, CancellationToken cancellationToken)
        {
            var phoneNumber = _mapper.Map <PhoneNumber>(request);

            _context.PhoneNumbers.Add(phoneNumber);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#3
0
        public async Task <Unit> Handle(
            CreatePersonCommand request,
            CancellationToken cancellationToken)
        {
            var person = _mapper.Map <Person>(request);

            _context.Persons.Add(person);
            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#4
0
        public async Task <Unit> Handle(UpdatePhoneNumberCommand request, CancellationToken cancellationToken)
        {
            var phoneNumber = await _context.PhoneNumbers.FindAsync(request.Id);

            if (phoneNumber is null)
            {
                throw new Exception("Telefon numarası bulunamadı.");
            }

            phoneNumber.Number = string.IsNullOrEmpty(request.Number) ? phoneNumber.Number : request.Number;
            phoneNumber.Type   = string.IsNullOrEmpty(request.Type) ? phoneNumber.Type : request.Type;

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#5
0
        public async Task <Unit> Handle(DeletePhoneNumberCommand request, CancellationToken cancellationToken)
        {
            foreach (var id in request.Ids)
            {
                var phoneNumber = await _context.PhoneNumbers.FindAsync(id);

                if (phoneNumber is null)
                {
                    throw new Exception("Telefon numarası bulunamadı.");
                }

                _context.PhoneNumbers.Remove(phoneNumber);
            }

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }
示例#6
0
        public async Task <Unit> Handle(UpdatePersonCommand request, CancellationToken cancellationToken)
        {
            var person = await _context.Persons.FindAsync(request.Id);

            if (person is null)
            {
                throw new Exception("Kişi bulunamadı");
            }

            person.FirstName = CheckIsChanged(person.FirstName, request.FirstName);
            person.LastName  = CheckIsChanged(person.LastName, request.LastName);
            person.Country   = CheckIsChanged(person.Country, request.Country);
            person.City      = CheckIsChanged(person.City, request.City);
            person.District  = CheckIsChanged(person.District, request.District);
            person.Street    = CheckIsChanged(person.Street, request.Street);
            person.ZipCode   = CheckIsChanged(person.ZipCode, request.ZipCode);

            await _context.SaveChangesAsync();

            return(Unit.Value);
        }