private void DoWork(object state) { _logger.LogInformation("Timed Background Service is working."); using (var scope = Services.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <WordHintDbContext>(); try { var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random"); var board = model.ToCrossBoard(); // add in database var newTemplate = new CrossWord.Scraper.MySQLDbService.Models.CrosswordTemplate() { Rows = model.Size.Rows, Cols = model.Size.Cols, Grid = model.Grid }; db.CrosswordTemplates.Add(newTemplate); db.SaveChangesAsync(); } catch (Exception ex) { _logger.LogError(ex, "An error occurred writing to the " + $"database. Error: {ex.Message}"); } } }
public IActionResult GenerateTemplates() { Queue.QueueBackgroundWorkItem(async token => { var guid = Guid.NewGuid().ToString(); logger.LogInformation( $"Queued Background Task {guid} added to the queue."); using (var scope = serviceScopeFactory.CreateScope()) { var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService <WordHintDbContext>(); try { var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random"); var board = model.ToCrossBoard(); // add in database var newTemplate = new CrosswordTemplate() { Rows = model.Size.Rows, Cols = model.Size.Cols, Grid = model.Grid }; db.CrosswordTemplates.Add(newTemplate); await db.SaveChangesAsync(); } catch (Exception ex) { logger.LogError(ex, "An error occurred writing to the " + $"database. Error: {ex.Message}"); } } logger.LogInformation( $"Queued Background Task {guid} is complete."); }); return(Ok("Generate crosssword template added to the queue")); }
private CrossBoard GetCrossboard() { ICrossBoard board = null; // var template = GetRandomCrosswordTemplateFromDb(); CrosswordTemplate template = null; if (template != null) { board = new CrossBoard(); int cols = (int)template.Cols; int rows = (int)template.Rows; board.SetBoardSize(cols, rows); int n = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { var val = template.Grid[n]; if (val == ".") { board.AddStartWord(col, row); } n += 1; } } // debug the generated template // using (StreamWriter writer = new StreamWriter("template.txt")) // { // board.WriteTemplateTo(writer); // } } else { var model = CrossBoardCreator.GetCrossWordModelFromUrl("http-random"); board = model.ToCrossBoard(); // add in database var newTemplate = new CrosswordTemplate() { Rows = model.Size.Rows, Cols = model.Size.Cols, Grid = model.Grid }; db.CrosswordTemplates.Add(newTemplate); db.SaveChanges(); } var gen = new CrossGenerator(dictionary, board); board.Preprocess(dictionary); var generated = gen.Generate().FirstOrDefault() as CrossBoard; return(generated); }