Skip to content

Instantly share code, notes, and snippets.

@httpJunkie
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save httpJunkie/738c4604addb428193aa to your computer and use it in GitHub Desktop.
Save httpJunkie/738c4604addb428193aa to your computer and use it in GitHub Desktop.
Update Topic in Web API with Repository
// /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 = "#/";
});
};
};
bool EditTopic(int id, Search newSearch);
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
{
//...
}
}
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