| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Microsoft.EntityFrameworkCore; |
| | 7 | | using MUNity.Base; |
| | 8 | | using MUNity.Database.Context; |
| | 9 | | using MUNity.Database.General; |
| | 10 | |
|
| | 11 | | namespace MUNity.Database.Extensions; |
| | 12 | |
|
| | 13 | | public static class CountryExtensions |
| | 14 | | { |
| | 15 | | public static Country AddTranslation(this Country country, string languageCode, string translation, string longName |
| 413 | 16 | | { |
| 413 | 17 | | country.Translations.Add(new CountryNameTranslation(country, languageCode, translation, longName)); |
| 413 | 18 | | return country; |
| 413 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Adds the countries that are given to the Database. If a country already has the given name it |
| | 23 | | /// will just update the country. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="context"></param> |
| | 26 | | /// <param name="countries"></param> |
| | 27 | | /// <returns></returns> |
| | 28 | | public static int AddBaseCountries(this MunityContext context, IEnumerable<Country> countries) |
| 1 | 29 | | { |
| 415 | 30 | | foreach (var country in countries) |
| 206 | 31 | | { |
| 206 | 32 | | var matchingCountry = context.Countries.Include(n => n.Translations).FirstOrDefault(n => |
| 206 | 33 | | n.Name == country.Name); |
| 206 | 34 | | if (matchingCountry != null) |
| 0 | 35 | | { |
| | 36 | | // Update the country |
| 0 | 37 | | matchingCountry.FullName = country.FullName; |
| 0 | 38 | | matchingCountry.Iso = country.Iso; |
| 0 | 39 | | if (country.Continent != EContinent.NotSet) |
| 0 | 40 | | matchingCountry.Continent = country.Continent; |
| | 41 | |
|
| 0 | 42 | | if (country.Translations.Count > 0) |
| 0 | 43 | | { |
| 0 | 44 | | foreach (var translation in country.Translations) |
| 0 | 45 | | { |
| 0 | 46 | | var foundTranslation = |
| 0 | 47 | | matchingCountry.Translations.FirstOrDefault(n => |
| 0 | 48 | | n.LanguageCode == translation.LanguageCode); |
| 0 | 49 | | if (foundTranslation != null) |
| 0 | 50 | | { |
| 0 | 51 | | foundTranslation.TranslatedName = translation.TranslatedName; |
| 0 | 52 | | foundTranslation.TranslatedFullName = translation.TranslatedFullName; |
| 0 | 53 | | } |
| 0 | 54 | | } |
| 0 | 55 | | } |
| 0 | 56 | | } |
| | 57 | | else |
| 206 | 58 | | { |
| 206 | 59 | | context.Countries.Add(country); |
| 206 | 60 | | } |
| 206 | 61 | | } |
| | 62 | |
|
| 1 | 63 | | return context.SaveChanges(); |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | |
|
| | 67 | | } |