private ImportedPollingStationEntity MapToImportEntity(PollingStationModel ps, Guid jobId)
        {
            var entity = new ImportedPollingStationEntity
            {
                Address               = ps.Address,
                County                = ps.County,
                Institution           = ps.Institution,
                Locality              = ps.Locality,
                PollingStationNumber  = ps.PollingStationNumber,
                ResolvedAddressStatus = ResolvedAddressStatusType.NotProcessed,
                AssignedAddresses     = new List <ImportedPollingStationAddressEntity>(),
                JobId = jobId.ToString()
            };

            if (ps.AssignedAddresses != null && ps.AssignedAddresses.Any())
            {
                foreach (var assignedAddress in ps.AssignedAddresses)
                {
                    entity.AssignedAddresses.Add(new ImportedPollingStationAddressEntity
                    {
                        Locality     = assignedAddress.Locality,
                        HouseNumbers = assignedAddress.HouseNumbers,
                        Remarks      = assignedAddress.Remarks,
                        Street       = assignedAddress.Street,
                        StreetCode   = assignedAddress.StreetCode
                    });
                }
            }

            return(entity);
        }
        public async Task <(bool isSuccess, string errorMessage, int pollingStationId)> AddPollingStationAsync(PollingStationModel pollingStation)
        {
            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    var newId = await _context.PollingStations.MaxAsync(x => x.Id) + 1;

                    var entity = new PollingStationEntity
                    {
                        Id                   = newId,
                        Address              = pollingStation.Address,
                        Longitude            = pollingStation.Longitude,
                        Latitude             = pollingStation.Latitude,
                        County               = pollingStation.County,
                        PollingStationNumber = pollingStation.PollingStationNumber,
                        Locality             = pollingStation.Locality,
                        Institution          = pollingStation.Institution,
                    };

                    await _context.PollingStations.AddAsync(entity);

                    _context.SaveChanges();

                    transaction.Commit();
                    return(true, string.Empty, newId);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Could not add new polling station");
                return(false, e.Message, -1);
            }
        }
        public async Task <(bool isSuccess, string errorMessage)> UpdatePollingStationAsync(PollingStationModel pollingStation)
        {
            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    var entity = await _context.PollingStations
                                 .FirstOrDefaultAsync(x => x.Id == pollingStation.Id);

                    if (entity == null)
                    {
                        return(false, $"Could not find polling station with id = {pollingStation.Id}");
                    }

                    entity.Address              = pollingStation.Address;
                    entity.Longitude            = pollingStation.Longitude;
                    entity.Latitude             = pollingStation.Latitude;
                    entity.County               = pollingStation.County;
                    entity.PollingStationNumber = pollingStation.PollingStationNumber;
                    entity.Locality             = pollingStation.Locality;
                    entity.Institution          = pollingStation.Institution;

                    _context.SaveChanges();

                    transaction.Commit();
                    return(true, string.Empty);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Could not update entity with id = {pollingStation.Id}");
                return(false, e.Message);
            }
        }