|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using SecureFromScratch.Quiz.Model; |
| 4 | +using SecureFromScratch.Quiz.Services; |
| 5 | +using SecureFromScratch.Quiz.Safety; |
| 6 | +//using SecureFromScratch.Quiz.Safety.Spoilers; |
| 7 | +using SecureFromScratch.Quiz.Config; |
| 8 | + |
| 9 | +namespace SecureFromScratch.Quiz.Controllers |
| 10 | +{ |
| 11 | + [ApiController] |
| 12 | + [Route("/")] |
| 13 | + public class QuizController : ControllerBase |
| 14 | + { |
| 15 | + private readonly ILogger<QuizController> _logger; |
| 16 | + //private readonly LoggerEx _answersLogger; |
| 17 | + private readonly ILogger _answersLogger; |
| 18 | + private readonly MultipleChoiceQuestion _quizService; |
| 19 | + |
| 20 | + public QuizController( |
| 21 | + ILogger<QuizController> logger, |
| 22 | + ILoggerFactory loggerFactory, |
| 23 | + MultipleChoiceQuestion quizService) |
| 24 | + { |
| 25 | + _logger = logger; |
| 26 | + //_answersLogger = new LoggerEx(loggerFactory.CreateLogger("Answers")); |
| 27 | + _answersLogger = logger; |
| 28 | + _quizService = quizService; |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + private static string GenerateAnswerOption(string username, string optionText, bool isCorrect) |
| 35 | + { |
| 36 | + return $"<a href='/answer?username={username}&isCorrect={isCorrect}'>{optionText}</a><br>\n"; |
| 37 | + } |
| 38 | + |
| 39 | + public record QuestionDetails(string Question, string[] Answers); |
| 40 | + |
| 41 | + [HttpGet("question")] |
| 42 | + public ContentResult GetQuestionDetails([FromQuery(Name = "username")] string rawUsername) |
| 43 | + { |
| 44 | + _logger.LogInformation("/question with name={Username}", rawUsername); |
| 45 | + |
| 46 | + var question = _quizService.GetQuestion(); |
| 47 | + _logger.LogInformation("/question returning question: {Question}", question); |
| 48 | + |
| 49 | + var username = new FullName(rawUsername); |
| 50 | + var shuffledAnswers = _quizService.GenerateShuffledAnswers(); |
| 51 | + var options = shuffledAnswers.Options; |
| 52 | + var correctAnswerIdx = shuffledAnswers.CorrectAnswerIdx; |
| 53 | + |
| 54 | + var html = "<html>" + |
| 55 | + "<head><title>Answer The Daily Question</title></head>" + |
| 56 | + "<body>" + |
| 57 | + $"<h2>{question}</h2><br>\n" + |
| 58 | + "Here are the possible answers, choose the appropriate link:<br>\n" + |
| 59 | + GenerateAnswerOption(username.Username, options[0], correctAnswerIdx == 0) + |
| 60 | + GenerateAnswerOption(username.Username, options[1], correctAnswerIdx == 1) + |
| 61 | + GenerateAnswerOption(username.Username, options[2], correctAnswerIdx == 2) + |
| 62 | + GenerateAnswerOption(username.Username, options[3], correctAnswerIdx == 3) + |
| 63 | + "</body></html>"; |
| 64 | + |
| 65 | + return Content(html, "text/html"); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + private string GenerateAnswerOption(object value, string v1, bool v2) |
| 70 | + { |
| 71 | + throw new NotImplementedException(); |
| 72 | + } |
| 73 | + |
| 74 | + [HttpGet("answer")] |
| 75 | + public ContentResult SubmitAnswer([FromQuery(Name = "username")] string rawUsername, [FromQuery] bool isCorrect) |
| 76 | + { |
| 77 | + _logger.LogInformation("/answer with name={Username} and answer={Answer}", rawUsername, isCorrect); |
| 78 | + |
| 79 | + var username = new FullName(rawUsername); |
| 80 | + |
| 81 | + if (isCorrect) |
| 82 | + _answersLogger.LogWarning("Right: {Username}", username.Username); |
| 83 | + else |
| 84 | + _answersLogger.LogInformation("Wrong: {Username}", username.Username); |
| 85 | + |
| 86 | + var html = "<html>" + |
| 87 | + "<head><title>Done!</title></head>" + |
| 88 | + "<body>" + |
| 89 | + $"<h2>Thank you for submitting, {username.Username}</h2>" + |
| 90 | + "<a href='/index.html'>Start Again</a>" + |
| 91 | + "</body></html>"; |
| 92 | + |
| 93 | + return Content(html, "text/html"); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments