diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SIS.MvcFramework/WebHost.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SIS.MvcFramework/WebHost.cs index ac434325..ed8c0b5d 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SIS.MvcFramework/WebHost.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SIS.MvcFramework/WebHost.cs @@ -88,7 +88,16 @@ private static HttpResponse InvokeAction(HttpRequest request, IServiceCollection foreach (var property in parameter.ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var propertyValue = GetValueFromRequest(request, property.Name); - property.SetValue(parameterValue, Convert.ChangeType(propertyValue, property.PropertyType)); + + if (property.PropertyType.IsGenericType && + property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + property.SetValue(parameterValue, null); + } + else + { + property.SetValue(parameterValue, Convert.ChangeType(propertyValue, property.PropertyType)); + } } actionParameterValues.Add(parameterValue); diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/HomeController.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/HomeController.cs index 19fbb403..870f342e 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/HomeController.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/HomeController.cs @@ -12,12 +12,12 @@ namespace SulsApp.Controllers public class HomeController : Controller { private readonly ILogger logger; - private readonly ApplicationDbContext db; + private readonly IProblemsService problemsService; - public HomeController(ILogger logger, ApplicationDbContext db) + public HomeController(ILogger logger, IProblemsService problemsService) { this.logger = logger; - this.db = db; + this.problemsService = problemsService; } [HttpGet("/")] @@ -25,12 +25,7 @@ public HttpResponse Index() { if (this.IsUserLoggedIn()) { - var problems = db.Problems.Select(x => new IndexProblemViewModel - { - Id = x.Id, - Name = x.Name, - Count = x.Submissions.Count(), - }).ToList(); + var problems = this.problemsService.GetProblems(); var loggedInViewModel = new LoggedInViewModel() { Problems = problems, diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/ProblemsController.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/ProblemsController.cs index 28f51fb6..33ec8ef3 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/ProblemsController.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/ProblemsController.cs @@ -9,12 +9,10 @@ namespace SulsApp.Controllers public class ProblemsController : Controller { private readonly IProblemsService problemsService; - private readonly ApplicationDbContext db; - public ProblemsController(IProblemsService problemsService, ApplicationDbContext db) + public ProblemsController(IProblemsService problemsService) { this.problemsService = problemsService; - this.db = db; } public HttpResponse Create() @@ -56,20 +54,7 @@ public HttpResponse Details(string id) return this.Redirect("/Users/Login"); } - var viewModel = this.db.Problems.Where(x => x.Id == id).Select( - x => new DetailsViewModel - { - Name = x.Name, - Problems = x.Submissions.Select(s => - new ProblemDetailsSubmissionViewModel - { - CreatedOn = s.CreatedOn, - AchievedResult = s.AchievedResult, - SubmissionId = s.Id, - MaxPoints = x.Points, - Username = s.User.Username, - }) - }).FirstOrDefault(); + var viewModel = this.problemsService.GetDetailsProblems(id); return this.View(viewModel); } diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/SubmissionsController.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/SubmissionsController.cs index 4497f13e..aa547156 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/SubmissionsController.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Controllers/SubmissionsController.cs @@ -12,12 +12,10 @@ namespace SulsApp.Controllers { public class SubmissionsController : Controller { - private readonly ApplicationDbContext db; private readonly ISubmissionsService submissionsService; - public SubmissionsController(ApplicationDbContext db, ISubmissionsService submissionsService) + public SubmissionsController(ISubmissionsService submissionsService) { - this.db = db; this.submissionsService = submissionsService; } @@ -28,13 +26,7 @@ public HttpResponse Create(string id) return this.Redirect("/Users/Login"); } - var problem = this.db.Problems - .Where(x => x.Id == id) - .Select(x => new CreateFormViewModel - { - Name = x.Name, - ProblemId = x.Id, - }).FirstOrDefault(); + var problem = this.submissionsService.GetProblem(id); if (problem == null) { return this.Error("Problem not found!"); diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/IProblemsService.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/IProblemsService.cs index da3ecb8b..7d1e380f 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/IProblemsService.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/IProblemsService.cs @@ -1,4 +1,6 @@ -using System; +using SulsApp.ViewModels.Home; +using SulsApp.ViewModels.Problems; +using System; using System.Collections.Generic; using System.Text; @@ -7,5 +9,9 @@ namespace SulsApp.Services public interface IProblemsService { void CreateProblem(string name, int points); + + IEnumerable GetProblems(); + + DetailsViewModel GetDetailsProblems(string id); } } diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ISubmissionsService.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ISubmissionsService.cs index a4c39711..3522bd29 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ISubmissionsService.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ISubmissionsService.cs @@ -1,4 +1,5 @@ -using System; +using SulsApp.ViewModels.Submissions; +using System; using System.Collections.Generic; using System.Text; @@ -9,5 +10,7 @@ public interface ISubmissionsService void Create(string userId, string problemId, string code); void Delete(string id); + + CreateFormViewModel GetProblem(string id); } } diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ProblemsService.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ProblemsService.cs index 55048e5a..8a4c3e3d 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ProblemsService.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/ProblemsService.cs @@ -1,6 +1,9 @@ using SulsApp.Models; +using SulsApp.ViewModels.Home; +using SulsApp.ViewModels.Problems; using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace SulsApp.Services @@ -24,5 +27,39 @@ public void CreateProblem(string name, int points) this.db.Problems.Add(problem); this.db.SaveChanges(); } + + public DetailsViewModel GetDetailsProblems(string id) + { + var viewModel = this.db.Problems + .Where(x => x.Id == id) + .Select(x => new DetailsViewModel + { + Name = x.Name, + Problems = x.Submissions.Select(s => new ProblemDetailsSubmissionViewModel + { + Username = s.User.Username, + AchievedResult = s.AchievedResult, + MaxPoints = x.Points, + CreatedOn = s.CreatedOn, + SubmissionId = s.Id + }) + .ToList() + }) + .FirstOrDefault(); + + return viewModel; + } + + public IEnumerable GetProblems() + { + var problems = this.db.Problems.Select(x => new IndexProblemViewModel + { + Id = x.Id, + Name = x.Name, + Count = x.Submissions.Count + }).ToList(); + + return problems; + } } } diff --git a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/SubmissionsService.cs b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/SubmissionsService.cs index 160e350e..642ed2dc 100644 --- a/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/SubmissionsService.cs +++ b/2020-Jan-Season/SoftUni-Information-Services/SIS/SulsApp/Services/SubmissionsService.cs @@ -1,4 +1,5 @@ using SulsApp.Models; +using SulsApp.ViewModels.Submissions; using System; using System.Linq; @@ -37,5 +38,18 @@ public void Delete(string id) this.db.Remove(submission); this.db.SaveChanges(); } + + public CreateFormViewModel GetProblem(string id) + { + var problem = this.db.Problems + .Where(x => x.Id == id) + .Select(x => new CreateFormViewModel + { + Name = x.Name, + ProblemId = x.Id + }).FirstOrDefault(); + + return problem; + } } }