Beispiel #1
0
            public async Task <Unit> Handle(UpdateCitizenCommand request, CancellationToken cancellationToken)
            {
                var citizen = await _context.CitizenDetails.FindAsync(request.Id);

                if (citizen == null)
                {
                    throw new EntityNotFoundException("Citizen", request.Id);
                }

                if (request.Photo != null)
                {
                    if (citizen.PhotoUrl is null)
                    {
                        await request.Photo.SaveAsync(_directories.CitizenPhotosDirectory);

                        citizen.PhotoUrl = request.Photo.Name;
                    }
                    else
                    {
                        try
                        {
                            BinaryFile.Delete(_directories.CitizenPhotosDirectory, citizen.PhotoUrl);

                            await request.Photo.SaveAsync(_directories.CitizenPhotosDirectory);

                            citizen.PhotoUrl = request.Photo.Name;
                        }
                        catch (Exception)
                        {
                            throw new FileProcessingException(request.Photo.Name);
                        }
                    }
                }

                // update any changes

                if (request.FatherId != null)
                {
                    citizen.FatherId = request.FatherId;
                }
                if (request.MotherId != null)
                {
                    citizen.MotherId = request.MotherId;
                }

                if (request.FullName != null)
                {
                    citizen.FullName = request.FullName;
                }
                if (request.Address != null)
                {
                    citizen.Address = request.Address;
                }

                if (request.Gender != Gender.None)
                {
                    citizen.Gender = request.Gender;
                }
                if (request.Religion != Religion.None)
                {
                    citizen.Religion = request.Religion;
                }
                if (request.SocialStatus != SocialStatus.None)
                {
                    citizen.SocialStatus = request.SocialStatus;
                }
                if (request.DateOfBirth != default)
                {
                    citizen.DateOfBirth = request.DateOfBirth;
                }

                _context.CitizenDetails.Update(citizen);

                try
                {
                    await _context.SaveChangesAsync(cancellationToken);
                }
                catch (Exception)
                {
                    throw new DbUpdateException();
                }

                return(Unit.Value);
            }