< Summary

Class:MUNity.Database.Extensions.CountryExtensions
Assembly:MUNity.Database
File(s):C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\Extensions\CountryExtensions.cs
Covered lines:16
Uncovered lines:20
Coverable lines:36
Total lines:67
Line coverage:44.4% (16 of 36)
Covered branches:3
Total branches:12
Branch coverage:25% (3 of 12)
Covered methods:2
Total methods:2
Method coverage:100% (2 of 2)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AddTranslation(...)100%1100%
AddBaseCountries(...)25%1237.5%

File(s)

C:\Users\aeuke\source\repos\PeerConradi\munity\src\MUNityDatabase\Extensions\CountryExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Threading.Tasks;
 6using Microsoft.EntityFrameworkCore;
 7using MUNity.Base;
 8using MUNity.Database.Context;
 9using MUNity.Database.General;
 10
 11namespace MUNity.Database.Extensions;
 12
 13public static class CountryExtensions
 14{
 15    public static Country AddTranslation(this Country country, string languageCode, string translation, string longName 
 41316    {
 41317        country.Translations.Add(new CountryNameTranslation(country, languageCode, translation, longName));
 41318        return country;
 41319    }
 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)
 129    {
 41530        foreach (var country in countries)
 20631        {
 20632            var matchingCountry = context.Countries.Include(n => n.Translations).FirstOrDefault(n =>
 20633                n.Name == country.Name);
 20634            if (matchingCountry != null)
 035            {
 36                // Update the country
 037                matchingCountry.FullName = country.FullName;
 038                matchingCountry.Iso = country.Iso;
 039                if (country.Continent != EContinent.NotSet)
 040                    matchingCountry.Continent = country.Continent;
 41
 042                if (country.Translations.Count > 0)
 043                {
 044                    foreach (var translation in country.Translations)
 045                    {
 046                        var foundTranslation =
 047                            matchingCountry.Translations.FirstOrDefault(n =>
 048                                n.LanguageCode == translation.LanguageCode);
 049                        if (foundTranslation != null)
 050                        {
 051                            foundTranslation.TranslatedName = translation.TranslatedName;
 052                            foundTranslation.TranslatedFullName = translation.TranslatedFullName;
 053                        }
 054                    }
 055                }
 056            }
 57            else
 20658            {
 20659                context.Countries.Add(country);
 20660            }
 20661        }
 62
 163        return context.SaveChanges();
 164    }
 65
 66
 67}