Last active
August 29, 2015 14:16
-
-
Save httpJunkie/738c4604addb428193aa to your computer and use it in GitHub Desktop.
Update Topic in Web API with Repository
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
// /ng-js/ng-modules/home-index.js | |
angular | |
.module("homeIndex", ["ngRoute"]) | |
.config(config) | |
.controller("topicsController", topicsController) | |
.controller("newTopicController", newTopicController) | |
.controller("editTopicController", editTopicController); | |
function config($routeProvider) { | |
$routeProvider | |
//Other routes left out for brevity | |
.when("/edittopic/:topicId", { | |
templateUrl: "/ng-js/ng-templates/editTopicView.html", | |
controller: "editTopicController", | |
controllerAs: "vm" | |
}) | |
.otherwise({ redirectTo: "/" }); | |
}; | |
//Other controllers left out for brevity | |
function editTopicController($http, $window, $routeParams) { | |
var vm = this; | |
var url = "/api/topics/" + $routeParams.topicId; | |
$http.get(url) | |
.success(function (result) { | |
vm.topic = result[0]; | |
}) | |
.error(function () { | |
alert('error/failed'); | |
}) | |
.then(function () { | |
//Nothing | |
}); | |
vm.update = function (id) { | |
var updateUrl = "/api/topics/" + id; | |
$http.put(updateUrl, vm.editTopic) | |
.success(function (result) { | |
var editTopic = result.data; | |
//TODO: merge with existing topics | |
//alert("Thanks for your post"); | |
}) | |
.error(function () { | |
alert("Update is broken, go fix it!"); | |
}) | |
.then(function () { | |
$window.location = "#/"; | |
}); | |
}; | |
}; |
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
bool EditTopic(int id, Search newSearch); |
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
public bool EditTopic(int id, Search editTopic) | |
{ | |
//var item = _ctx.Topics.FirstOrDefault(t => t.TopicId == id); | |
//item(editTopic).State = EntityState.Modified; | |
//return true; | |
try | |
{ | |
//...Not sure what to do here you can see I was playing around with | |
//...Isolating the topic and then updating it above, but that code was not | |
//...Working out. I was getting an error that said (Method, Delegate or Event is required | |
} | |
catch | |
{ | |
//... | |
} | |
} |
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
public HttpResponseMessage Put(int id,[FromBody]Search editTopic) | |
{ | |
if (_repo.EditTopic(id, editTopic) && _repo.Save()) | |
{ | |
return Request.CreateResponse(HttpStatusCode.Created, editTopic); | |
} | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment