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

Create initial api for CRUD actions on DB #3

Merged
merged 15 commits into from
Apr 5, 2016
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# ignore sqlite dbs
*.db

# ignore wwwroot folders
wwwroot/dist
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-rc1-final"
"version": "1.0.0-rc1-update1"
}
}
13 changes: 0 additions & 13 deletions src/CodingMonkey/Controllers/ApiController.cs

This file was deleted.

19 changes: 8 additions & 11 deletions src/CodingMonkey/Controllers/CodeExecutionController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodingMonkey.ViewModels;
using Microsoft.AspNet.Mvc;
using CodingMonkey.CodeExecutor;
using Microsoft.CodeAnalysis;

namespace CodingMonkey.Controllers
namespace CodingMonkey.Controllers
{
public class CodeExecutionController : ApiController
using System.Collections.Generic;
using CodingMonkey.ViewModels;
using Microsoft.AspNet.Mvc;
using CodingMonkey.CodeExecutor;

[Route("api/[controller]/[action]")]
public class CodeExecutionController : Controller
{
[HttpPost]
public JsonResult Compile([FromBody] CodeEditorViewModel model)
Expand Down
206 changes: 206 additions & 0 deletions src/CodingMonkey/Controllers/ExerciseCategoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
namespace CodingMonkey.Controllers
{
using CodingMonkey.ViewModels;
using CodingMonkey.Models;

using Microsoft.AspNet.Mvc;

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Data.Entity;

[Route("api/[controller]/[action]")]
public class ExerciseCategoryController : Controller
{
[FromServices]
public CodingMonkeyContext CodingMonkeyContext { get; set; }

[HttpGet]
public JsonResult List()
{
var exerciseCategories = CodingMonkeyContext.ExerciseCategories.Include(e => e.ExerciseExerciseCategories);

List<ExerciseCategoryViewModel> vm = new List<ExerciseCategoryViewModel>();

foreach (var exerciseCategory in exerciseCategories)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update code to use GetExercisesInCategory private method.

{
List<int> exerciseIdsInCategory = GetExercisesInCategory(exerciseCategory.ExerciseCategoryId);

vm.Add(
new ExerciseCategoryViewModel()
{
Id = exerciseCategory.ExerciseCategoryId,
Name = exerciseCategory.Name,
Description = exerciseCategory.Description,
ExerciseIds = exerciseIdsInCategory
});
}

return Json(vm);
}

[HttpGet]
[Route("{id}")]
public JsonResult Details(int id)
{
var exerciseCategory =
CodingMonkeyContext.ExerciseCategories.SingleOrDefault(e => e.ExerciseCategoryId == id);

if (exerciseCategory == null)
{
return Json(string.Empty);
}

List<int> exercisesInCategory = GetExercisesInCategory(exerciseCategory.ExerciseCategoryId);

var vm = new ExerciseCategoryViewModel()
{
Id = exerciseCategory.ExerciseCategoryId,
Name = exerciseCategory.Name,
Description = exerciseCategory.Description,
ExerciseIds = exercisesInCategory
};

return Json(vm);
}

[HttpPost]
public JsonResult Create([FromBody] ExerciseCategoryViewModel vm)
{
var exceptionResult = new Dictionary<string, dynamic>();

if (vm == null)
{
return Json(string.Empty);
}

ExerciseCategory exerciseCategoryToCreate = new ExerciseCategory()
{
Name = vm.Name,
Description = vm.Description
};

try
{
CodingMonkeyContext.ExerciseCategories.Add(exerciseCategoryToCreate);

if (ModelState.IsValid)
{
CodingMonkeyContext.SaveChanges();
}
}
catch (Exception)
{
exceptionResult["updated"] = false;
exceptionResult["reason"] = "exception thrown";

return Json(exceptionResult);
}

List<int> exercisesInCategory = GetExercisesInCategory(exerciseCategoryToCreate.ExerciseCategoryId);

vm.Id = exerciseCategoryToCreate.ExerciseCategoryId;
vm.Name = exerciseCategoryToCreate.Name;
vm.Description = exerciseCategoryToCreate.Description;
vm.ExerciseIds = exercisesInCategory;

return Json(vm);
}

[HttpPost]
[Route("{id}")]
public JsonResult Update(int id, [FromBody] ExerciseCategoryViewModel vm)
{
if (vm == null)
{
return Json(string.Empty);
}

var exceptionResult = new Dictionary<string, dynamic>();
var exerciseCategoryToUpdate =
CodingMonkeyContext.ExerciseCategories.SingleOrDefault(e => e.ExerciseCategoryId == id);

if (exerciseCategoryToUpdate == null)
{
exceptionResult["updated"] = false;
exceptionResult["reason"] = "record not found";

return Json(exceptionResult);
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check for model == null

try
{
exerciseCategoryToUpdate.Name = vm.Name;
exerciseCategoryToUpdate.Description = vm.Description;

if (ModelState.IsValid)
{
CodingMonkeyContext.SaveChanges();
}
}
catch (Exception)
{
exceptionResult["updated"] = false;
exceptionResult["reason"] = "exception thrown";

return Json(exceptionResult);
}

List<int> exercisesInCategory = GetExercisesInCategory(exerciseCategoryToUpdate.ExerciseCategoryId);

vm.Id = exerciseCategoryToUpdate.ExerciseCategoryId;
vm.Name = exerciseCategoryToUpdate.Name;
vm.Description = exerciseCategoryToUpdate.Description;
vm.ExerciseIds = exercisesInCategory;

return Json(vm);
}

[HttpDelete]
[Route("{id}")]
public JsonResult Delete(int id)
{
var result = new Dictionary<string, dynamic>();
var exerciseCategoryToDelete =
CodingMonkeyContext.ExerciseCategories.Include(ec => ec.ExerciseExerciseCategories)
.SingleOrDefault(e => e.ExerciseCategoryId == id);

if (exerciseCategoryToDelete == null)
{
result["deleted"] = false;
result["reason"] = "record not found";
}
else
{
try
{
//Remove category assignments
exerciseCategoryToDelete.ExerciseExerciseCategories.RemoveAll(
x => x.ExerciseCategoryId == exerciseCategoryToDelete.ExerciseCategoryId);

CodingMonkeyContext.ExerciseCategories.Remove(exerciseCategoryToDelete);
CodingMonkeyContext.SaveChanges();
result["deleted"] = true;
}
catch (Exception)
{
result["deleted"] = false;
result["reason"] = "exception thrown";
}
}

return Json(result);
}

private List<int> GetExercisesInCategory(int exerciseCategoryId)
{
return
CodingMonkeyContext.Exercises.Include(e => e.ExerciseExerciseCategories)
.Where(e => e.ExerciseExerciseCategories.Any(c => c.ExerciseCategoryId == exerciseCategoryId))
.Select(e => e.ExerciseId)
.ToList();
}
}
}
Loading