Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight modification to WebHost class #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,20 @@ 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("/")]
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using SulsApp.ViewModels.Home;
using SulsApp.ViewModels.Problems;
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -7,5 +9,9 @@ namespace SulsApp.Services
public interface IProblemsService
{
void CreateProblem(string name, int points);

IEnumerable<IndexProblemViewModel> GetProblems();

DetailsViewModel GetDetailsProblems(string id);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SulsApp.ViewModels.Submissions;
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -9,5 +10,7 @@ public interface ISubmissionsService
void Create(string userId, string problemId, string code);

void Delete(string id);

CreateFormViewModel GetProblem(string id);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<IndexProblemViewModel> GetProblems()
{
var problems = this.db.Problems.Select(x => new IndexProblemViewModel
{
Id = x.Id,
Name = x.Name,
Count = x.Submissions.Count
}).ToList();

return problems;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SulsApp.Models;
using SulsApp.ViewModels.Submissions;
using System;
using System.Linq;

Expand Down Expand Up @@ -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;
}
}
}