-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dce0eba
Created view models for each of the models and also corrected a typo …
95203ba
Created the exercise creation api
tdshipley 3518c4e
Updated global json to target correct framework so intellisense in vs…
tdshipley b5d7988
Clean up of usings in controllers
tdshipley b65bd20
Added controller for exercise templates
tdshipley 6a8b9c8
Updated the model of the database to have a many to many relationship…
tdshipley fd6680e
part way through creating api for exercise categories
tdshipley f89b856
Fixed create exercise to have categories - need to fix update
55f5c47
Fixed up the exercise category controller
tdshipley f627f04
fix the exercise category delete route to remove the category from an…
tdshipley 0824714
Added a api to create a test for an exercise
tdshipley 222ee0c
Fixes to code from code review
3f7cc21
fix project on linux and mac os
tdshipley 1e70793
Merge branch 'create_initial_api' of https://github.com/tdshipley/Cod…
tdshipley d66737c
fix weridness in select statement in list action on exercise controller
tdshipley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# ignore sqlite dbs | ||
*.db | ||
|
||
# ignore wwwroot folders | ||
wwwroot/dist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
src/CodingMonkey/Controllers/ExerciseCategoryController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.