예제 #1
0
        public void ReadTranspondersTest()
        {
            const string transponderType = "MYLAPS ProChip";

            var file           = Resources.AFile;
            var converter      = new MylapsTransponderCodeConverter();
            var labelConverter = new Func <string, long?>(s =>
            {
                long code;
                return(converter.TryConvertLabel(transponderType, s, out code) ? code : new long?());
            });

            IList <Transponder> transponders;

            using (var reader = new StringReader(file))
                transponders = AFile.ReadTransponders(reader, transponderType, labelConverter).ToList();

            Assert.AreEqual(78, transponders.Count);
            Assert.IsTrue(transponders.All(t => t.Type == transponderType));
            Assert.AreEqual(102061448, transponders[38].Code);
            Assert.AreEqual("CX-98152", transponders[38].Label);
            Assert.AreEqual(102000459, transponders[39].Code);
            Assert.AreEqual("CX-37163", transponders[39].Label);
            Assert.AreEqual("HC-81093", transponders.Last().Label);
        }
        public async Task <ICollection <RaceTransponder> > LoadFromStreamAsync(Guid competitionId, Guid distanceId, Stream stream)
        {
            using (var context = contextFactory())
                using (var transaction = context.BeginTransaction(IsolationLevel.RepeatableRead))
                    try
                    {
                        var transponders = await context.Transponders.ToListAsync();

                        var converter = new MylapsTransponderCodeConverter();

                        var position               = stream.Position;
                        var transpondersReader     = new StreamReader(stream, Encoding);
                        var transponderKeyComparer = new TransponderKeyComparer();
                        var transpondersInFile     = AFile.ReadTransponders(transpondersReader, MylapsTransponderCodeConverter.ProChipType,
                                                                            l =>
                        {
                            long code;
                            return(converter.TryConvertLabel(MylapsTransponderCodeConverter.ProChipType, l, out code) ? code : new long?());
                        });

                        foreach (var transponder in transpondersInFile)
                        {
                            if (!transponders.Contains(transponder, transponderKeyComparer))
                            {
                                context.Transponders.Add(transponder);
                                transponders.Add(transponder);
                            }
                        }

                        var existingTransponders = await(from r in context.RaceTransponders
                                                         where r.Race.DistanceId == distanceId
                                                         select r).ToListAsync();
                        foreach (var transponder in existingTransponders)
                        {
                            context.RaceTransponders.Remove(transponder);
                        }

                        await context.SaveChangesAsync();

                        var distance = await context.Distances.FirstAsync(d => d.CompetitionId == competitionId && d.Id == distanceId);

                        var races = await(from r in context.Races.Include(r => r.Competitor)
                                          where r.Distance.CompetitionId == competitionId && r.DistanceId == distanceId
                                          select r).ToListAsync();

                        await(from tcm in context.TeamCompetitorMembers.Include(m => m.Member)
                              where tcm.Team.Races.Any(r => r.Distance.CompetitionId == competitionId && r.DistanceId == distanceId)
                              select tcm).LoadAsync();

                        stream.Seek(position, SeekOrigin.Begin);
                        var raceTransponders       = new List <RaceTransponder>();
                        var raceTranspondersReader = new StreamReader(stream, Encoding);
                        foreach (var transponder in AFile.ReadRaceTransponders(raceTranspondersReader, races, transponders, ShouldReversePeople(distance)))
                        {
                            context.RaceTransponders.Add(transponder);
                            raceTransponders.Add(transponder);
                        }
                        await context.SaveChangesAsync();

                        transaction.Commit();
                        return(raceTransponders);
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
        }