Skip to content
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# IDE
/.idea
/.vs
/*.user
*.user
*.csproj


# Build results
[Dd]ebug/
Expand All @@ -12,3 +14,5 @@
/GraphLabs.Backend.Api/db
/GraphLabs.Backend.Api/wwwroot/modules
/GraphLabs.Backend.Api/logs
/GraphLabs.Backend.Api/Properties/launchSettings.json

65 changes: 65 additions & 0 deletions GraphLabs.Backend.Api/Controllers/SubjectsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraphLabs.Backend.DAL;
using GraphLabs.Backend.Domain;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace GraphLabs.Backend.Api.Controllers
{
[ODataRoutePrefix("subjects")]
public class SubjectsController : ODataController
{
private readonly GraphLabsContext _db;

public SubjectsController(GraphLabsContext context)
{
_db = context;
}

[EnableQuery]
public IQueryable<Subject> Get()
{
return _db.Subjects;
}

[ODataRoute("({key})")]
[EnableQuery]
public SingleResult<Subject> Get(long key)
{
return SingleResult.Create(_db.Subjects.Where(t => t.Id == key));
}

[HttpPost]
public async Task<IActionResult> Post([FromBody]CreateRequest request)
{
if (request == null ||
string.IsNullOrEmpty(request.Name) ||
string.IsNullOrEmpty(request.Description))
return BadRequest();

var subject = new Subject
{
Name = request.Name,
Description = request.Description
};

_db.Subjects.Add(subject);
await _db.SaveChangesAsync();

return Ok(subject.Id);
}

public class CreateRequest
{
public string Name { get; set; }

public string Description { get; set; }
}
}
}
92 changes: 92 additions & 0 deletions GraphLabs.Backend.Api/Controllers/TestAnswersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraphLabs.Backend.DAL;
using GraphLabs.Backend.Domain;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;

namespace GraphLabs.Backend.Api.Controllers
{
[ODataRoutePrefix("testAnswers")]
public class TestAnswersController : ODataController
{
private readonly GraphLabsContext _db;

public TestAnswersController(GraphLabsContext context)
{
_db = context;
}

[EnableQuery]
public IQueryable<TestAnswer> Get()
{
return _db.TestAnswers;
}

[EnableQuery]
[ODataRoute("({key})")]
public SingleResult<TestAnswer> Get(long key)
{
return SingleResult.Create(_db.TestAnswers.Where(t => t.Id == key));
}

[HttpPost]
[ODataRoute("({key})")]
public async Task<IActionResult> Post([FromBody]CreateRequest request, long key)
{
if (request == null || string.IsNullOrEmpty(request.Answer))
return BadRequest();

var testQuestionVersion = _db.TestQuestionVersions.Single(v => v.Id == key);
if (testQuestionVersion == null)
return BadRequest();

var testAnswer = new TestAnswer
{
Answer = request.Answer,
IsRight = request.IsRight,
TestQuestionVersion = testQuestionVersion
};

if (testQuestionVersion.TestAnswers == null)
{
testQuestionVersion.TestAnswers = new List<TestAnswer> { testAnswer };
}
else
{
testQuestionVersion.TestAnswers.Add(testAnswer);
}

await _db.SaveChangesAsync();
return Ok(testAnswer.Id);
}

[HttpDelete]
[ODataRoute("({key})")]
public async Task<IActionResult> Delete(long key)
{
var answer = _db.TestAnswers.Single(v => v.Id == key);
if (answer != null)
{
_db.TestAnswers.Remove(answer);
await _db.SaveChangesAsync();
}
else
{
return NotFound();
}

return Ok();
}

public class CreateRequest
{
public string Answer { get; set; }

public bool IsRight { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using GraphLabs.Backend.DAL;
using GraphLabs.Backend.Domain;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

namespace GraphLabs.Backend.Api.Controllers
{
[ODataRoutePrefix("testQuestionVersions")]
public class TestQuestionVersionsController : ODataController
{
private readonly GraphLabsContext _db;

public TestQuestionVersionsController(GraphLabsContext context)
{
_db = context;
}

[EnableQuery]
public IQueryable<TestQuestionVersion> Get()
{
return _db.TestQuestionVersions;
}

[EnableQuery]
[ODataRoute("({key})")]
public SingleResult<TestQuestionVersion> Get(long key)
{
return SingleResult.Create(_db.TestQuestionVersions.Where(t => t.Id == key));
}


//ODataRoute("createVariant") not working
[HttpPost]
public async Task<IActionResult> Post([FromBody]CreateRequest[] request)
{
if (request == null)
return BadRequest();

var variant = new Collection<TestQuestionVersion>();

foreach (var r in request)
{
if (r.SubjectId == 0 || r.Quantity == 0)
return BadRequest();

var subjectId = r.SubjectId;
var quantity = r.Quantity;

var question = _db.TestQuestions.Where(w => w.Subject.Id == subjectId);
var questionLength = question.Count();

if (quantity > questionLength)
quantity = questionLength;
if (questionLength == 0)
return new NotFoundResult();

var questionArray = question.Select(s => s.Id).ToArray();
for (var i = 0; i<quantity; i++)
{
var questionArrayRandomId = questionArray[new Random().Next(0, questionArray.Length)];
questionArray = questionArray.Where(w => w != questionArrayRandomId).ToArray();
var questionVersion = _db.TestQuestionVersions.Last(p => p.TestQuestion.Id == questionArrayRandomId);
variant.Add(questionVersion);
}
}

return Ok(variant);
}

public class CreateRequest
{
public long SubjectId { get; set; }

public int Quantity { get; set; }
}

}
}
119 changes: 119 additions & 0 deletions GraphLabs.Backend.Api/Controllers/TestQuestionsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using GraphLabs.Backend.DAL;
using GraphLabs.Backend.Domain;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

namespace GraphLabs.Backend.Api.Controllers
{
[ODataRoutePrefix("testQuestions")]
public class TestQuestionsController : ODataController
{
private readonly GraphLabsContext _db;

public TestQuestionsController(GraphLabsContext context)
{
_db = context;
}

[EnableQuery]
public IQueryable<TestQuestion> Get()
{
return _db.TestQuestions;
}

[EnableQuery]
[ODataRoute("({key})")]
public SingleResult<TestQuestion> Get(long key)
{
return SingleResult.Create(_db.TestQuestions.Where(t => t.Id == key));
}

[HttpPost]
[ODataRoute("({key})")]
public async Task<IActionResult> Post([FromBody]CreateRequest request, long key)
{
if (request == null ||
request.MaxScore == 0 ||
string.IsNullOrEmpty(request.PlainText) ||
string.IsNullOrEmpty(request.Difficulty))
return BadRequest();

TestQuestion testQuestion;
var testQuestionVersion = new TestQuestionVersion
{
PlainText = request.PlainText,
Difficulty = GetDifficulty(request.Difficulty),
MaxScore = request.MaxScore
};

if (key == 0)
{
//Create TestQuestion and TestQuestion.TestQuestionVersion
var subject = _db.Subjects.Single(s => s.Id == request.SubjectId);
if (subject == null)
return BadRequest();

testQuestion = new TestQuestion
{
TestQuestionVersions = new List<TestQuestionVersion> { testQuestionVersion },
Subject = subject
};

if (subject.TestQuestions == null)
{
subject.TestQuestions = new List<TestQuestion> { testQuestion };
}
else
{
subject.TestQuestions.Add(testQuestion);
}
_db.TestQuestions.Add(testQuestion);
}
else
{
//Update TestQuestion.TestQuestionVersion
testQuestion = _db.TestQuestions.Include(q => q.TestQuestionVersions).Single(q => q.Id == key);
testQuestion.TestQuestionVersions.Add(testQuestionVersion);
}

testQuestionVersion.TestQuestion = testQuestion;
_db.TestQuestionVersions.Add(testQuestionVersion);

await _db.SaveChangesAsync();
return Ok(testQuestionVersion.Id);
}

public class CreateRequest
{
public string PlainText { get; set; }

public string Difficulty { get; set; }

public int MaxScore { get; set; }

public long? SubjectId { get; set; }
}

private Difficulty GetDifficulty(string diff)
{
switch (diff)
{
case "Three":
return Difficulty.Three;
case "Four":
return Difficulty.Four;
case "Five":
return Difficulty.Five;
}
return Difficulty.Three;
}
}
}
Loading