public async Task <PersonLicense> AddNewLicenseAsync(string issuerId, string discipline, string key, Person person, int?season, DateTime?validFrom, DateTime?validTo, PersonLicenseFlags flags, string category = null, int?number = null, string sponsor = null, Club club = null, string venueCode = null, string transponder1 = null, string transponder2 = null) { var expert = calculatorManager.Find(discipline); if (expert == null) { throw new InvalidDisciplineException(); } season = season ?? expert.CurrentSeason; validFrom = validFrom ?? expert.SeasonStarts(season.Value); validTo = validTo ?? expert.SeasonEnds(season.Value); if (category == null) { var age = expert.SeasonAge(season.Value, person.BirthDate); var personCategory = await GetDefaultCategoryAsync(issuerId, discipline, person.Gender, age); category = personCategory?.Code; } if (venueCode == null && club != null) { await context.LoadAsync(club, c => c.Venues, c => c.VenueDiscipline == discipline); venueCode = club.Venues?.FirstOrDefault(v => v.VenueDiscipline == discipline)?.VenueCode; } var license = new PersonLicense { IssuerId = issuerId, Discipline = discipline, Key = key, Season = season.Value, ValidFrom = validFrom.Value, ValidTo = validTo.Value, Sponsor = sponsor, Club = club, Person = person, Category = category, Number = number, Flags = flags, VenueCode = venueCode, Transponder1 = transponder1, Transponder2 = transponder2 }; context.PersonLicenses.Add(license); await context.SaveChangesAsync(); recorder.RecordEvent(new PersonLicenseChangedEvent(license)); return(license); }
private async Task <string> GenerateLicenseKeyAsync(string issuerId, PersonLicenseFlags flags, string keyPrefix, int keyFrom) { var previousKey = await(from l in context.PersonLicenses where l.IssuerId == issuerId && (l.Flags & flags) == flags orderby l.Key descending select l.Key).FirstOrDefaultAsync(); var prefix = keyPrefix ?? ""; int previousKeyNumber; var nextKeyNumber = previousKey != null && previousKey.StartsWith(prefix) && int.TryParse(previousKey.Substring(prefix.Length), out previousKeyNumber) ? Math.Max(keyFrom, previousKeyNumber + 1) : keyFrom; return($"{prefix}{nextKeyNumber}"); }
public async Task <PersonLicense> AddNewDisposableLicenseAsync(string issuerId, string discipline, Person person, DateTime valid, PersonLicenseFlags flags = PersonLicenseFlags.None, Club club = null, string venueCode = null) { var expert = calculatorManager.Find(discipline); if (expert == null) { throw new InvalidDisciplineException(); } flags |= PersonLicenseFlags.DisposableLicense; var key = await GenerateDisposableLicenseKeyAsync(issuerId); var season = expert.Season(valid); return(await AddNewLicenseAsync(issuerId, discipline, key, person, season, valid.Date, valid.Date.AddDays(1), flags, club : club, venueCode : venueCode)); }
public async Task <PersonLicensePrice> GetPersonLicensePriceAsync(string issuerId, string discipline, Gender gender, int age, PersonLicenseFlags flags = PersonLicenseFlags.None) { flags &= PersonLicenseFlags.PriceFlags; return(await(from p in context.PersonLicensePrices where p.LicenseIssuerId == issuerId && p.Discipline == discipline && (p.Flags & PersonLicenseFlags.PriceFlags) == flags && p.FromAge <= age && p.ToAge >= age select p).FirstOrDefaultAsync()); }
public async Task <PersonLicense> UpdatePersonLicenseAsync(PersonLicense license, PersonLicenseExpertise expertise, int?season = null, DateTime?validFrom = null, DateTime?validTo = null, string sponsor = null, Club club = null, PersonLicenseFlags flags = PersonLicenseFlags.None, string category = null, int?number = null, string venueCode = null, string transponder1 = null, string transponder2 = null) { var expert = calculatorManager.Find(license.Discipline); if (expert == null) { throw new InvalidDisciplineException(); } season = season ?? expert.CurrentSeason; using (var transaction = context.UseOrBeginTransaction(IsolationLevel.RepeatableRead)) try { if (license.Season != season.Value) { license.Flags = flags; license.Season = season.Value; license.ValidFrom = validFrom ?? expert.SeasonStarts(season.Value); license.ValidTo = validTo ?? expert.SeasonEnds(season.Value); license.Sponsor = sponsor; license.Club = club; if (category == null) { var age = expert.SeasonAge(season.Value, license.Person.BirthDate); var personCategory = await GetDefaultCategoryAsync(license.IssuerId, license.Discipline, license.Person.Gender, age); license.Category = personCategory?.Code; } else { license.Category = category; } if (expertise.HasFlag(PersonLicenseExpertise.Number)) { license.Number = number; } if (venueCode != null) { license.VenueCode = venueCode; } license.Transponder1 = transponder1; license.Transponder2 = transponder2; } else { license.Flags = flags; if (expertise.HasFlag(PersonLicenseExpertise.Validity)) { license.ValidFrom = validFrom ?? expert.SeasonStarts(season.Value); license.ValidTo = validTo ?? expert.SeasonEnds(season.Value); } if (expertise.HasFlag(PersonLicenseExpertise.Sponsor)) { license.Sponsor = sponsor; } if (expertise.HasFlag(PersonLicenseExpertise.Club)) { license.Club = club; } if (expertise.HasFlag(PersonLicenseExpertise.Category)) { if (category == null) { var age = expert.SeasonAge(season.Value, license.Person.BirthDate); var personCategory = await GetDefaultCategoryAsync(license.IssuerId, license.Discipline, license.Person.Gender, age); license.Category = personCategory?.Code; } else { license.Category = category; } } if (expertise.HasFlag(PersonLicenseExpertise.Number)) { license.Number = number; } if (expertise.HasFlag(PersonLicenseExpertise.Venue)) { license.VenueCode = venueCode; } if (expertise.HasFlag(PersonLicenseExpertise.Transponders)) { license.Transponder1 = transponder1; license.Transponder2 = transponder2; } } await context.SaveChangesAsync(); transaction.Commit(); recorder.RecordEvent(new PersonLicenseChangedEvent(license)); } catch { transaction.Rollback(); throw; } return(license); }
public async Task <PersonLicense> AddNewTemporaryLicenseAsync(string issuerId, string discipline, Person person, int season, DateTime validFrom, DateTime validTo, PersonLicenseFlags flags = PersonLicenseFlags.None) { var expert = calculatorManager.Find(discipline); if (expert == null) { throw new InvalidDisciplineException(); } flags |= PersonLicenseFlags.TemporaryLicense; var key = await GenerateTemporaryLicenseKeyAsync(issuerId); return(await AddNewLicenseAsync(issuerId, discipline, key, person, season, validFrom, validTo, flags)); }