public async Task <IActionResult> OnPostEditAsync(int?id)
        {
            //Upload files to the bucket
            if (PdfRules != null)
            {
                await FileHandler.UploadFileToS3(PdfRules, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestRules/");
            }

            if (PdfTickets.Count > 0)
            {
                foreach (var ticket in PdfTickets)
                {
                    await FileHandler.UploadFileToS3(ticket, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestTickets/");
                }
            }
            if (CheaterList != null)
            {
                await FileHandler.UploadFileToS3(CheaterList, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/CheaterList/");
            }



            //Get Time now
            var time = DateTime.Now;

            Contest.CREATEDDT  = time;
            Contest.MODIFIEDDT = time;

            //Enable Contest
            Contest.ISENABLED = true;

            //Set Published state
            Contest.ISPUBLISHED = false;



            //Set user created
            Contest.MODIFIEDUSER = Contest.CREATEDUSER;

            //Get company
            Company = await _context.Company.FirstOrDefaultAsync(x => x.Id == id);

            //Set Assinged to company
            Contest.ASSIGNEDTOCOMPANY = Company.CompanyName;
            Contest.COMPANYID         = Company.Id;


            _context.Contest.Add(Contest);
            await _context.SaveChangesAsync();

            if (InviteList != null)
            {
                await FileHandler.UploadFileToS3(InviteList, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/InviteList/");
                await UploadList();
            }

            //Set created date on all contexts
            Settings.CREATEDDT  = Contest.CREATEDDT;
            Mails.CREATEDDT     = Contest.CREATEDDT;
            Questions.CREATEDDT = Contest.CREATEDDT;

            //Set modified on all contexts
            Settings.MODIFIEDDT  = Contest.MODIFIEDDT;
            Mails.MODIFIEDDT     = Contest.MODIFIEDDT;
            Questions.MODIFIEDDT = Contest.MODIFIEDDT;

            //set language
            if (Settings.ENGLISHBOOL == true)
            {
                Settings.LANGUAGE = "ENGLISH";
            }
            if (Settings.DUTCHBOOL == true)
            {
                Settings.LANGUAGE = "DUTCH";
            }
            if (Settings.FRENCHBOOL == true)
            {
                Settings.LANGUAGE = "FRENCH";
            }
            if (Settings.GERMANBOOL == true)
            {
                Settings.LANGUAGE = "GERMAN";
            }
            Mails.LANGUAGE     = Settings.LANGUAGE;
            Questions.LANGUAGE = Settings.LANGUAGE;

            //Set contest privacy
            if (Settings.TYPEPRIVATE == true)
            {
                Settings.CONTESTPRIVACY = 2;
            }
            if (Settings.TYPEPRIVATE == false)
            {
                Settings.CONTESTPRIVACY = 1;
            }



            //Set context actions
            Settings.CREATEDUSER  = Contest.CREATEDUSER;
            Mails.CREATEDUSER     = Contest.CREATEDUSER;
            Questions.CREATEDUSER = Contest.CREATEDUSER;



            Settings.CONTESTID  = Contest.ID;
            Mails.CONTESTID     = Contest.ID;
            Questions.CONTESTID = Contest.ID;



            _context.Settings.Add(Settings);
            _context.Mails.Add(Mails);
            _context.Questions.Add(Questions);

            await _context.SaveChangesAsync();

            return(Redirect(Url.Content("~/Contests/Edit/" + Contest.ID + "/" + Contest.COMPANYID)));
        }
        public async Task <IActionResult> OnPostPreviewAsync()
        {
            //Delete old files and upload new ones
            if (PdfRules != null)
            {
                await FindAndDeleteFiles(_context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestRules/");

                await FileHandler.UploadFileToS3(PdfRules, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestRules/");
            }

            if (PdfTickets.Count > 0)
            {
                await FindAndDeleteFiles(_context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestTickets/");

                foreach (var ticket in PdfTickets)
                {
                    await FileHandler.UploadFileToS3(ticket, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/ContestTickets/");
                }
            }

            if (CheaterList != null)
            {
                await FindAndDeleteFiles(_context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/CheaterList/");

                await FileHandler.UploadFileToS3(CheaterList, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/CheaterList/");
            }

            if (InviteList != null)
            {
                await FindAndDeleteFiles(_context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/InviteList/");

                await FileHandler.UploadFileToS3(InviteList, _context.Company.FirstOrDefaultAsync(m => m.Id == _userManager.GetUserAsync(User).Result.CompanyId).Result.CompanyName + "/" + Contest.NAME + "/InviteList/");
                await DeleteFile();
                await UploadList();
            }


            Mails.CONTESTID     = Contest.ID;
            Questions.CONTESTID = Contest.ID;
            Settings.CONTESTID  = Contest.ID;


            var time = DateTime.Now;

            Contest.MODIFIEDDT   = time;
            Settings.MODIFIEDDT  = time;
            Mails.MODIFIEDDT     = time;
            Questions.MODIFIEDDT = time;


            //Set context createduser
            Settings.CREATEDUSER  = Contest.CREATEDUSER;
            Mails.CREATEDUSER     = Contest.CREATEDUSER;
            Questions.CREATEDUSER = Contest.CREATEDUSER;

            //Set context createddt
            Settings.CREATEDDT  = Contest.CREATEDDT;
            Mails.CREATEDDT     = Contest.CREATEDDT;
            Questions.CREATEDDT = Contest.CREATEDDT;


            //Set context modifieduser
            Settings.MODIFIEDUSER  = Contest.MODIFIEDUSER;
            Mails.MODIFIEDUSER     = Contest.MODIFIEDUSER;
            Questions.MODIFIEDUSER = Contest.MODIFIEDUSER;

            //set language
            if (Settings.ENGLISHBOOL == true)
            {
                Settings.LANGUAGE = "ENGLISH";
            }
            if (Settings.DUTCHBOOL == true)
            {
                Settings.LANGUAGE = "DUTCH";
            }
            if (Settings.FRENCHBOOL == true)
            {
                Settings.LANGUAGE = "FRENCH";
            }
            if (Settings.GERMANBOOL == true)
            {
                Settings.LANGUAGE = "GERMAN";
            }
            Mails.LANGUAGE     = Settings.LANGUAGE;
            Questions.LANGUAGE = Settings.LANGUAGE;



            _context.Attach(Contest).State   = EntityState.Modified;
            _context.Attach(Settings).State  = EntityState.Modified;
            _context.Attach(Questions).State = EntityState.Modified;
            _context.Attach(Mails).State     = EntityState.Modified;


            await _context.SaveChangesAsync();


            return(Redirect(Url.Content("~/Contests/Details/") + Contest.ID + "/" + _userManager.GetUserAsync(User).Result.CompanyId));
        }