public void CaseTemplatesSites_Create_DoesCreate()
        {
            //Arrange

            Random           rnd = new Random();
            CaseTemplateSite caseTemplateSite = new CaseTemplateSite();

            caseTemplateSite.CaseTemplateId = rnd.Next(1, 255);
            caseTemplateSite.SdkSiteId      = rnd.Next(1, 255);
            caseTemplateSite.SdkCaseId      = rnd.Next(1, 255);

            //Act

            caseTemplateSite.Create(DbContext);

            List <CaseTemplateSite>        dbCaseTemplatesSite         = DbContext.CaseTemplateSites.AsNoTracking().ToList();
            List <CaseTemplateSiteVersion> dbCaseTemplatesSiteVersions = DbContext.CaseTemplateSiteVersions.AsNoTracking().ToList();

            //Assert
            Assert.NotNull(dbCaseTemplatesSite);
            Assert.NotNull(dbCaseTemplatesSiteVersions);

            Assert.AreEqual(1, dbCaseTemplatesSite.Count);
            Assert.AreEqual(1, dbCaseTemplatesSiteVersions.Count);

            Assert.AreEqual(caseTemplateSite.Id, dbCaseTemplatesSite[0].Id);
            Assert.AreEqual(caseTemplateSite.Version, dbCaseTemplatesSite[0].Version);
            Assert.AreEqual(caseTemplateSite.CreatedAt.ToString(), dbCaseTemplatesSite[0].CreatedAt.ToString());
            Assert.AreEqual(caseTemplateSite.UpdatedAt.ToString(), dbCaseTemplatesSite[0].UpdatedAt.ToString());
            Assert.AreEqual(caseTemplateSite.CreatedByUserId, dbCaseTemplatesSite[0].CreatedByUserId);
            Assert.AreEqual(caseTemplateSite.UpdatedByUserId, dbCaseTemplatesSite[0].UpdatedByUserId);
            Assert.AreEqual(Constants.WorkflowStates.Created, dbCaseTemplatesSite[0].WorkflowState);
            Assert.AreEqual(caseTemplateSite.CaseTemplateId, dbCaseTemplatesSite[0].CaseTemplateId);
            Assert.AreEqual(caseTemplateSite.SdkSiteId, dbCaseTemplatesSite[0].SdkSiteId);
            Assert.AreEqual(caseTemplateSite.SdkCaseId, dbCaseTemplatesSite[0].SdkCaseId);

            //Versions
            Assert.AreEqual(caseTemplateSite.Id, dbCaseTemplatesSiteVersions[0].CaseTemplateSiteId);
            Assert.AreEqual(caseTemplateSite.Version, dbCaseTemplatesSiteVersions[0].Version);
            Assert.AreEqual(caseTemplateSite.CreatedAt.ToString(), dbCaseTemplatesSiteVersions[0].CreatedAt.ToString());
            Assert.AreEqual(caseTemplateSite.UpdatedAt.ToString(), dbCaseTemplatesSiteVersions[0].UpdatedAt.ToString());
            Assert.AreEqual(caseTemplateSite.CreatedByUserId, dbCaseTemplatesSiteVersions[0].CreatedByUserId);
            Assert.AreEqual(caseTemplateSite.UpdatedByUserId, dbCaseTemplatesSiteVersions[0].UpdatedByUserId);
            Assert.AreEqual(Constants.WorkflowStates.Created, dbCaseTemplatesSiteVersions[0].WorkflowState);
            Assert.AreEqual(caseTemplateSite.CaseTemplateId, dbCaseTemplatesSiteVersions[0].CaseTemplateId);
            Assert.AreEqual(caseTemplateSite.SdkSiteId, dbCaseTemplatesSiteVersions[0].SdkSiteId);
            Assert.AreEqual(caseTemplateSite.SdkCaseId, dbCaseTemplatesSiteVersions[0].SdkCaseId);
        }
        public async Task <OperationResult> CreateCaseTemplate(int id)
        {
            try
            {
                Core _core = new Core();
                _core.StartSqlOnly(
                    "host= localhost;Database=759_SDK;user = root;port=3306;Convert Zero Datetime = true;SslMode=none;");

                Dictionary <int, string> laRoutes = new Dictionary <int, string>()
                {
                    { 24, "Århus H - Aalborg" },
                    { 25, "Aalborg - Lindholm" }
                };

                string checkSum;

                DateTime dt            = DateTime.Now;
                var      dayofWeek     = dt.AddDays(1).ToString("dddd", new System.Globalization.CultureInfo("da-DK"));
                var      dayOfWeek2    = dt.AddDays(2).ToString("dddd", new System.Globalization.CultureInfo("da-DK"));
                var      date          = dt.AddDays(1).ToString("dd");
                var      date2         = dt.AddDays(2).ToString("dd");
                var      monthOfYear   = dt.ToString("MMMM", new System.Globalization.CultureInfo("da-DK"));
                var      year          = dt.ToString("yyyy");
                string   dateFormatted = dt.ToString("-yyyy-MM-dd-yyyy-MM-dd");
                string   laNumber;
                string   laTitle;

                foreach (KeyValuePair <int, string> entry in laRoutes)
                {
                    laNumber = entry.Key.ToString();
                    laTitle  = "La-" + laNumber + dateFormatted;

                    CaseTemplate existingCaseTemplate = await _dbContext.CaseTemplates.SingleOrDefaultAsync(x =>
                                                                                                            x.Title == entry.Value && x.StartAt.ToString("dd-MM-yyyy") == dt.AddDays(1).ToString("dd-MM-yyyy") &&
                                                                                                            x.EndAt.ToString("dd-MM-yyyy") == dt.AddDays(2).ToString("dd-MM-yyyy"));

                    if (existingCaseTemplate == null)
                    {
                        string fileCheckSum = GetLa(laNumber, _core);
                        checkSum = fileCheckSum;

                        MainElement mainElement = _core.TemplateRead(9);
                        mainElement.Repeated                   = 1;
                        mainElement.Label                      = $"Banedanmark LA: {entry.Value}";
                        mainElement.ElementList[0].Label       = mainElement.Label;
                        mainElement.ElementList[0].Description = new CDataValue()

                        {
                            InderValue =
                                $"Fra: {dayofWeek} den {date}. {monthOfYear} {year}<br>Til: {dayOfWeek2} den {date2}. {monthOfYear} {year}"
                        };

                        DataElement dataElement = (DataElement)mainElement.ElementList[0];
                        dataElement.DataItemList[0].Label = entry.Value;
                        ShowPdf showPdf = (ShowPdf)dataElement.DataItemList[1];
                        showPdf.Value = fileCheckSum;

                        CaseTemplate newCaseTemplate = new CaseTemplate();
                        newCaseTemplate.Title    = entry.Value;
                        newCaseTemplate.StartAt  = dt.AddDays(1);
                        newCaseTemplate.EndAt    = dt.AddDays(2);
                        newCaseTemplate.PdfTitle = fileCheckSum;
                        newCaseTemplate.Create(_dbContext);

                        List <SiteName_Dto> sites = _core.Advanced_SiteItemReadAll(false);

                        foreach (var site in sites)
                        {
                            int?sdkCaseId = _core.CaseCreate(mainElement, "", site.SiteUId);

                            CaseTemplateSite caseTemplateSite = new CaseTemplateSite()
                            {
                                CaseTemplateId = newCaseTemplate.Id,
                                SdkSiteId      = site.SiteUId,
                                SdkCaseId      = (int)sdkCaseId
                            };

                            caseTemplateSite.Create(_dbContext);
                        }
                    }

                    UploadedData existingUploadedData = await _dbContext.UploadedDatas.SingleOrDefaultAsync(x =>
                                                                                                            x.FileName == laTitle);

                    if (existingUploadedData == null)
                    {
                        UploadedData uploadedData = new UploadedData()
                        {
                            FileName       = laTitle,
                            ExpirationDate = existingCaseTemplate.EndAt
                        };

                        uploadedData.Create(_dbContext);
                    }
                }

                return(new OperationResult(true, "Created successfully"));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationDataResult <CaseTemplatesModel>(false,
                                                                    _baneDanmarkLaLocalizationService.GetString("Failure creating LA") + $" {e.Message}"));
            }
        }