public MenuResult CreateMenu(AuthIdentity identity, Menu menu, params Guid[] recipeIds) { using (var session = GetSession()) { menu.Title = menu.Title.Trim(); var ret = new MenuResult(); using (var transaction = session.BeginTransaction()) { Models.Menus dbMenu; var dupes = session.QueryOver <Models.Menus>() .Where(p => p.UserId == identity.UserId) .Where(p => p.Title == menu.Title) .ToRowCountQuery() .RowCount(); if (dupes > 0) { throw new MenuAlreadyExistsException(); } session.Save(dbMenu = new Models.Menus { UserId = identity.UserId, Title = menu.Title, CreatedDate = DateTime.Now, }); foreach (var rid in recipeIds.NeverNull()) { var fav = new Favorites { UserId = identity.UserId, Recipe = new Models.Recipes() { RecipeId = rid }, Menu = dbMenu }; session.Save(fav); } transaction.Commit(); ret.MenuCreated = true; ret.NewMenuId = dbMenu.MenuId; } return(ret); } }
public MenuResult CreateMenu(AuthIdentity identity, Menu menu, params Guid[] recipeIds) { using (var session = GetSession()) { menu.Title = menu.Title.Trim(); var ret = new MenuResult(); using (var transaction = session.BeginTransaction()) { Models.Menus dbMenu; var dupes = session.QueryOver<Models.Menus>() .Where(p => p.UserId == identity.UserId) .Where(p => p.Title == menu.Title) .ToRowCountQuery() .RowCount(); if (dupes > 0) { throw new MenuAlreadyExistsException(); } session.Save(dbMenu = new Models.Menus { UserId = identity.UserId, Title = menu.Title, CreatedDate = DateTime.Now, }); foreach (var rid in recipeIds.NeverNull()) { var fav = new Favorites { UserId = identity.UserId, Recipe = new Models.Recipes() {RecipeId = rid}, Menu = dbMenu }; session.Save(fav); } transaction.Commit(); ret.MenuCreated = true; ret.NewMenuId = dbMenu.MenuId; } return ret; } }
private static void RenameMenu(string newName, Menus databaseMenu) { if (!string.IsNullOrWhiteSpace(newName) && databaseMenu != null) { databaseMenu.Title = newName.Trim(); } }
private static void CheckForUserAccess(AuthorIdentity identity, Menus databaseMenu) { if (databaseMenu.UserId != identity.UserId) { throw new UserDoesNotOwnMenuException(); } }
private static void AddRecipesToMenu( AuthorIdentity identity, Guid[] recipesAdd, IList<Favorites> databaseRecipes, Menus databaseMenu, ISession session) { var existing = from r in databaseRecipes select r.Recipe.RecipeId; recipesAdd = recipesAdd.Except(existing).ToArray(); // Remove duplicates foreach (var recipeId in recipesAdd) { var fav = new Favorites { UserId = identity.UserId, Recipe = new Recipes { RecipeId = recipeId }, Menu = databaseMenu }; session.Save(fav); } }
public MenuResult UpdateMenu(AuthIdentity identity, Guid?menuId, Guid[] recipesAdd, Guid[] recipesRemove, MenuMove[] recipesMove, bool clear, string newName = null) { var ret = new MenuResult(); ret.MenuUpdated = true; // TODO: Verify actual changes were made before setting MenuUpdated to true using (var session = GetSession()) { using (var transaction = session.BeginTransaction()) { Models.Menus dbMenu = null; IList <Favorites> dbRecipes = null; if (menuId.HasValue) { dbMenu = session.QueryOver <Models.Menus>() .Fetch(prop => prop.Recipes).Eager .Where(p => p.MenuId == menuId) .SingleOrDefault(); if (dbMenu == null) { throw new MenuNotFoundException(); } if (dbMenu.UserId != identity.UserId) // User does not have access to modify this menu { throw new UserDoesNotOwnMenuException(); } if (!String.IsNullOrWhiteSpace(newName) && dbMenu != null) // Rename menu { dbMenu.Title = newName.Trim(); } dbRecipes = dbMenu.Recipes; } else { dbRecipes = session.QueryOver <Favorites>() .Where(p => p.UserId == identity.UserId) .Where(p => p.Menu == null) .List(); } if (recipesAdd.Any()) // Add recipes to menu { var existing = (from r in dbRecipes select r.Recipe.RecipeId); recipesAdd = recipesAdd.Except(existing).ToArray(); //Remove dupes foreach (var rid in recipesAdd) { var fav = new Favorites { UserId = identity.UserId, Recipe = new Models.Recipes() { RecipeId = rid }, Menu = dbMenu }; session.Save(fav); } } if (recipesRemove.Any()) // Remove recipes from menu { var toDelete = (from r in dbRecipes where recipesRemove.Contains(r.Recipe.RecipeId) select r); toDelete.ForEach(session.Delete); } if (clear) // Remove every recipe from menu { dbRecipes.ForEach(session.Delete); } if (recipesMove.Any()) // Move items to another menu { foreach (var moveAction in recipesMove) { Models.Menus dbTarget = null; if (moveAction.TargetMenu.HasValue) { dbTarget = session.QueryOver <Models.Menus>() .Where(p => p.MenuId == moveAction.TargetMenu.Value) .Where(p => p.UserId == identity.UserId) .SingleOrDefault(); if (dbTarget == null) { throw new MenuNotFoundException(moveAction.TargetMenu.Value); } } var rToMove = (moveAction.MoveAll ? dbRecipes : dbRecipes.Where(r => moveAction.RecipesToMove.Contains(r.Recipe.RecipeId))); rToMove.ForEach(a => a.Menu = dbTarget); } } transaction.Commit(); } } return(ret); }