Last active
August 29, 2015 14:16
-
-
Save httpJunkie/6baced0c2fa2aabc9e7a to your computer and use it in GitHub Desktop.
CRUD for Favorites
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
<div class="small-12 column"><h3>Edit Search</h3></div> | |
<div class="small-12 column"> | |
<form name="editFavoriteForm" novalidate ng-submit="vm.update(vm.search.searchId)"> | |
<input name="userId" type="hidden" ng-model="vm.search.userId" /> | |
<label for="name">Name</label> | |
<input name="name" type="text" ng-model="vm.search.name" autofocus /> | |
<label for="description">Description</label> | |
<textarea name="description" rows="5" cols="30" ng-model="vm.search.description"></textarea> | |
<label for="searchString">Search String</label> | |
<input name="searchString" type="text" ng-model="vm.search.searchString" /> | |
<input type="submit" class="tiny button radius" value="Save" /> | <a href="#/" class="tiny button radius">Cancel</a> | |
</form> | |
</div> |
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
namespace MyProject.Controllers.api | |
{ | |
public class FavoritesController : ApiController | |
{ | |
private IFavoritesRepository _favRepo; | |
public FavoritesController(IFavoritesRepository favRepo) | |
{ | |
_favRepo = favRepo; | |
} | |
public IEnumerable<Search> Get() | |
{ | |
var id = User.Identity.GetUserId(); | |
IQueryable<Search> results; | |
results = _favRepo.GetFavoritesByUserId(id); | |
var favorites = results.OrderByDescending(s => s.UserId == id); | |
return favorites; | |
} | |
public IEnumerable<Search> Get(int id) | |
{ | |
IQueryable<Search> results; | |
results = _favRepo.GetFavoriteBySearchId(id); | |
return results; | |
} | |
public HttpResponseMessage Post([FromBody]Search newFavorite) | |
{ | |
if (newFavorite.Created == null) | |
{ | |
newFavorite.Created = DateTime.UtcNow; | |
} | |
if (_favRepo.AddFavorite(newFavorite) && _favRepo.Save()) | |
{ | |
return Request.CreateResponse(HttpStatusCode.Created, newFavorite); | |
} | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
public HttpResponseMessage Put(int id,[FromBody]Search editFavorite) | |
{ | |
if (_favRepo.EditFavorite(id, editFavorite) && _favRepo.Save()) | |
{ | |
return Request.CreateResponse(HttpStatusCode.Created, editFavorite); | |
} | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
public HttpResponseMessage Delete(int id) | |
{ | |
if (_favRepo.DelFavorite(id) && _favRepo.Save()) | |
{ | |
return Request.CreateResponse(HttpStatusCode.Created, id); | |
} | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
} | |
} |
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
namespace MyProject.DataLayer | |
{ | |
public class FavoritesRepository : IFavoritesRepository | |
{ | |
MyProjectContext _ctx; | |
public FavoritesRepository(MyProjectContext ctx) | |
{ | |
_ctx = ctx; | |
} | |
public IQueryable<Search> GetFavoritesByUserId(string id) | |
{ | |
return _ctx.Search.Where(s => s.UserId == id); | |
} | |
public IQueryable<Search> GetFavoriteBySearchId(int id) | |
{ | |
return _ctx.Search.Where(s => s.SearchId == id); | |
} | |
public bool Save() | |
{ | |
try | |
{ | |
return _ctx.SaveChanges() > 0; | |
} | |
catch | |
{ | |
// TODO log this error | |
return false; | |
} | |
} | |
public bool AddFavorite(Search newFavorite) | |
{ | |
_ctx.Search.Add(newFavorite); | |
return true; | |
} | |
public bool EditFavorite(int id, Search editFavorite) | |
{ | |
try | |
{ | |
var search = _ctx.Search.FirstOrDefault(s => s.SearchId == id); | |
search(editFavorite).State = EntityState.Modified; | |
return true; | |
} | |
catch | |
{ | |
//Nothing done here yet | |
} | |
} | |
public bool DelFavorite(int id) | |
{ | |
var search = _ctx.Search; | |
search.Remove(search.SingleOrDefault(s => s.SearchId == id)); | |
return true; | |
} | |
} | |
} |
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
namespace MyProject.DataLayer | |
{ | |
public interface IFavoritesRepository | |
{ | |
IQueryable<Search> GetFavoritesByUserId(string id); | |
IQueryable<Search> GetFavoriteBySearchId(int id); | |
bool Save(); | |
bool AddFavorite(Search newSearch); | |
bool EditFavorite(int id, Search newSearch); | |
bool DelFavorite(int id); | |
//* | |
// We could also add update and deletes here as well | |
// bool Update(param); | |
// bool Delete(param); | |
} | |
} |
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/render-index.js | |
angular | |
.module("renderIndex", ["ngRoute","ngCookies"]) | |
.config(config) | |
.controller("favoritesController", favoritesController) | |
.controller("newFavoriteController", newFavoriteController) | |
.controller("editFavoriteController", editFavoriteController) | |
function config($routeProvider) { | |
$routeProvider | |
.when("/", { | |
templateUrl: "/ng-js/ng-templates/favoritesView.html", | |
controller: "favoritesController", | |
controllerAs: "vm" | |
}) | |
.when("/newfavorite", { | |
templateUrl: "/ng-js/ng-templates/newFavoriteView.html", | |
controller: "newFavoriteController", | |
controllerAs: "vm" | |
}) | |
.when("/editfavorite/:searchId", { | |
templateUrl: "/ng-js/ng-templates/editFavoriteView.html", | |
controller: "editFavoriteController", | |
controllerAs: "vm" | |
}) | |
.otherwise({ redirectTo: "/" }); | |
}; | |
//Controllers for Favorite Searches | |
function favoritesController($http, $window) { | |
var vm = this; | |
vm.searches = []; | |
$http.get("/api/favorites") | |
.success(function (result) { | |
vm.searches = result; | |
}) | |
.error(function () { | |
alert('error/failed'); | |
}) | |
.then(function () { | |
//Nothing | |
}); | |
vm.delete = function (searchId) { | |
var url = "/api/favorites/" + searchId; | |
$http.delete(url) | |
.success(function (result) { | |
var newFavorite = result.data; | |
//TODO: merge with existing topics | |
//alert("Delete Successfull"); | |
removeSearch(vm.searches, searchId); | |
}) | |
.error(function () { | |
alert("Your broken, go fix yourself!"); | |
}) | |
.then(function () { | |
$window.location = "#/"; | |
}); | |
}; | |
}; | |
function removeSearch(items, id) { | |
var index; | |
for (var i = 0; i < items.length; i++) { | |
if (items[i].searchId == id) { | |
index = i; | |
break; | |
} | |
} | |
if (index) { | |
items.splice(index, 1); | |
} | |
} | |
function newFavoriteController($http, $window, $cookies) { | |
var vm = this; | |
vm.newFavorite = {}; | |
vm.newFavorite.searchString = $cookies.currentSearch; | |
vm.newFavorite.userId = $cookies.uId; | |
vm.save = function () { | |
$http.post("/api/favorites", vm.newFavorite) | |
.success(function (result) { | |
var newFavorite = result.data; | |
//TODO: merge with existing topics | |
//alert("Thanks for your post"); | |
}) | |
.error(function () { | |
alert("Your broken, go fix yourself!"); | |
}) | |
.then(function () { | |
$window.location = "#/"; | |
}); | |
}; | |
}; | |
function editFavoriteController($http, $window, $routeParams) { | |
var vm = this; | |
var url = "/api/favorites/" + $routeParams.searchId; | |
$http.get(url) | |
.success(function (result) { | |
vm.search = result; | |
}) | |
.error(function () { | |
alert('error/failed'); | |
}) | |
.then(function () { | |
//Nothing | |
}); | |
vm.update = function (id) { | |
var updateUrl = "/api/favorites/" + id; | |
$http.put(updateUrl, vm.editFavorite) | |
.success(function (result) { | |
var editFavorite = result.data; | |
//TODO: merge with existing favorites | |
//alert("Thanks for your post"); | |
}) | |
.error(function () { | |
alert("Your broken, go fix yourself!"); | |
}) | |
.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
namespace RenderLib.Models | |
{ | |
public class Search | |
{ | |
public int SearchId { get; set; } | |
[MaxLength(128), Column(TypeName = "nvarchar")] | |
public string UserId { get; set; } | |
[MaxLength(64), Column(TypeName = "nvarchar")] | |
public string Name { get; set; } | |
[MaxLength(256), Column(TypeName = "nvarchar")] | |
public string Description { get; set; } | |
public DateTime? Created { get; set; } | |
[MaxLength(2080), Column(TypeName = "nvarchar")] | |
public string SearchString { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment