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); } }
private static PollingStationEntity MapToPollingStationEntity(ImportedPollingStationEntity ips) { var entity = new PollingStationEntity { Address = ips.Address, County = ips.County, Institution = ips.Institution, Locality = ips.Locality, PollingStationNumber = ips.PollingStationNumber, Latitude = ips.Latitude ?? -999, Longitude = ips.Longitude ?? -999, PollingStationAddresses = new List <PollingStationAddressEntity>() }; if (ips.AssignedAddresses != null && ips.AssignedAddresses.Any()) { foreach (var assignedAddress in ips.AssignedAddresses) { entity.PollingStationAddresses.Add(new PollingStationAddressEntity { Locality = assignedAddress.Locality, HouseNumbers = assignedAddress.HouseNumbers, Remarks = assignedAddress.Remarks, Street = assignedAddress.Street, StreetCode = assignedAddress.StreetCode }); } } return(entity); }