public async Task <ICrudResult <EmptyClass> > DeleteAsync(ICrudCriteria <int> criteria) { // Initialize a result. var result = new SimpleCrudResult <EmptyClass>(); // Look for the recipe verifying that it belongs to the user then try to delete. var recipe = await this.Context.Recipes.FirstOrDefaultAsync(a => a.Id == criteria.Value && a.AccountId == criteria.AccountId); if (recipe == null) { result.ErrorMessage = "Unable to find or access the specified Recipe."; } else { this.Context.Recipes.Remove(recipe); var count = await this.Context.SaveChangesAsync(); if (count > 0) { result.Success = true; } else { result.ErrorMessage = "Delete failed to save."; } } return(result); }
/// <summary> /// Move the account into deleted status. /// </summary> /// <param name="criteria"></param> /// <returns></returns> public async Task <ICrudResult <EmptyClass> > DeleteAsync(ICrudCriteria <int> criteria) { // Initialize a result. var result = new SimpleCrudResult <EmptyClass>(); // Find the account and attempt to delete it. var account = await this.Context.Accounts.FirstOrDefaultAsync(a => a.Id == criteria.AccountId); if (account == null) { result.ErrorMessage = "Unable to find or access the specified Account."; } else { account.Status = AccountStatus.Deleted.ToString(); var count = await this.Context.SaveChangesAsync(); if (count > 0) { result.Success = true; } else { result.ErrorMessage = "Delete failed to save."; } } return(result); }
public async Task <ICrudResult <Recipe> > CreateAsync(Recipe recipe) { // Instantiate a default result. var result = new SimpleCrudResult <Recipe>(); // Add the Recipe to context and save. if (recipe != null) { recipe = this.Context.Recipes.Add(recipe); // Recipe-to-Tag is many-to-many so tags must be attached individually. recipe.Tags.ToList().ForEach(t => this.Context.Tags.Attach(t)); var count = await this.Context.SaveChangesAsync(); if (count > 0) { result.Model = recipe; result.Success = true; } else { result.ErrorMessage = "The Recipe failed to save."; } } else { result.ErrorMessage = "Recipe is null."; } return(result); }
public async Task <ICrudResult <Tag> > CreateAsync(Tag tag) { // Instantiate a default result. var result = new SimpleCrudResult <Tag>(); // Add the Tag to context and save. if (tag != null) { this.Context.Tags.Add(tag); var count = await this.Context.SaveChangesAsync(); if (count > 0) { result.Model = tag; result.Success = true; } else { result.ErrorMessage = "The Tag failed to save."; } } else { result.ErrorMessage = "Tag is null."; } return(result); }
public async Task <ICrudResult <Tag> > ReadAsync(ICrudCriteria <int> criteria) { // Initialize a result. var result = new SimpleCrudResult <Tag>(); // Look for the tag verifying that it belongs to the user before returning it. if (criteria != null) { var tag = await this.Context.Tags.FirstOrDefaultAsync(a => a.Id == criteria.Value && a.AccountId == criteria.AccountId); if (tag == null) { result.ErrorMessage = "Unable to find or access the specified Tag."; } else { result.Model = tag; result.Success = true; } } else { result.ErrorMessage = "Cannot locate a tag using a null criteria."; } return(result); }
public async Task <ICrudResult <Tag> > UpdateAsync(ICrudCriteria <Tag> criteria) { // Initialize a result. var result = new SimpleCrudResult <Tag>(); // Look for the tag verifying that it belongs to the user before updating only desired properties. if (criteria != null) { Tag tag = null; if (criteria.Value != null) { tag = await this.Context.Tags .FirstOrDefaultAsync(a => a.Id == criteria.Value.Id && a.AccountId == criteria.AccountId); } if (tag == null) { result.ErrorMessage = "Unable to find or access the specified Tag."; } else { try { // Specify properties to update. tag.Description = criteria.Value.Description; this.Context.SetPropertyModified(tag, t => t.Description); // Save and handle results var saveCount = await this.Context.SaveChangesAsync(); if (saveCount > 0) { result.Model = tag; result.Success = true; } else { result.ErrorMessage = "The Tag failed to save."; } } catch (DbUpdateConcurrencyException) { result.ErrorMessage = "The Tag could not be saved because it was out of date."; } } } else { result.ErrorMessage = "Cannot update a tag using a null criteria."; } return(result); }
public async Task <ICrudResult <Account> > UpdateAsync(ICrudCriteria <Account> criteria) { // Initialize a result. var result = new SimpleCrudResult <Account>(); // Fetch the account. Account account = null; if (criteria.Value != null) { account = await this.Context.Accounts .FirstOrDefaultAsync(a => a.Id == criteria.AccountId); } if (account == null) { result.ErrorMessage = "Unable to find the specified account."; } else { try { // Update only specific properties. account.EmailAddress = criteria.Value.EmailAddress; account.UserName = criteria.Value.UserName; this.Context.SetPropertyModified(account, a => a.UserName); if (account.Password != criteria.Value.Password) { account.Password = CoreUtility.EncryptPassword(criteria.Value.Password, account.Salt); } // Save and handle results var saveCount = await this.Context.SaveChangesAsync(); if (saveCount > 0) { result.Model = account; result.Success = true; } else { result.ErrorMessage = "The Account failed to save."; } } catch (DbUpdateConcurrencyException) { result.ErrorMessage = "The Account could not be saved because it was out of date."; } } return(result); }
private TagsController ArrangeControllerForDelete(bool dataManagerResultSuccess, bool validClaim) { var mockClaimsProvider = GetMockClaimsProvider(validClaim); // Mock the data manager specifiying the input success flag. var mockDataManager = new Mock <IDataManager <Tag> >(); ICrudResult <EmptyClass> crudResult = new SimpleCrudResult <EmptyClass>() { Success = dataManagerResultSuccess }; mockDataManager.Setup(m => m.DeleteAsync(It.IsAny <ICrudCriteria <int> >())).Returns(Task.FromResult(crudResult)); // Instantiate a controller with the mocked dependencies and return. return(ArrangeController(mockClaimsProvider.Object, mockDataManager.Object)); }
public async Task <ICrudResult <Account> > CreateAsync(Account account) { // Instantiate a default result. var result = new SimpleCrudResult <Account>(); // Add the Account to context and save. if (account != null) { // Verify no existing accounts with the same user name or email address. var existingAccount = await this.Context.Accounts .FirstOrDefaultAsync(a => a.UserName == account.UserName || a.EmailAddress == account.EmailAddress); if (existingAccount == null) { // Set status as unconfirmed, encrypt the password, save and handle results. account.Id = Guid.NewGuid().ToString(); account.Status = AccountStatus.Unconfirmed.ToString(); account.Salt = CoreUtility.GenerateSalt(); account.Password = CoreUtility.EncryptPassword(account.Password, account.Salt); this.Context.Accounts.Add(account); if (await this.Context.SaveChangesAsync() > 0) { result.Model = account; result.Success = true; } else { result.ErrorMessage = "The Account failed to save."; } } else if (existingAccount.UserName == account.UserName) { result.ErrorMessage = String.Format("The user name {0} is already in use.", account.UserName); } else { result.ErrorMessage = String.Format("The email address {0} is already in use.", account.EmailAddress); } } else { result.ErrorMessage = "Account is null."; } return(result); }
private TagsController ArrangeControllerForPut(bool dataManagerResultSuccess, bool validClaim) { var mockClaimsProvider = GetMockClaimsProvider(validClaim); // Mock the data manager specifiying the input success flag. var mockDataManager = new Mock <IDataManager <Tag> >(); ICrudResult <Tag> crudResult = new SimpleCrudResult <Tag>() { Success = dataManagerResultSuccess }; if (dataManagerResultSuccess) { crudResult.Model = TestUtility.GetDefaultTag(); } mockDataManager.Setup(m => m.UpdateAsync(It.IsAny <SimpleCriteria <Tag> >())).Returns(Task.FromResult(crudResult)); // Instantiate a controller with the mocked dependencies and return. return(ArrangeController(mockClaimsProvider.Object, mockDataManager.Object)); }
public async Task <ICrudResult <Account> > ReadAsync(ICrudCriteria <int> criteria) { // Initialize a result. var result = new SimpleCrudResult <Account>(); // Look for the account. var account = await this.Context.Accounts.FirstOrDefaultAsync(a => a.Id == criteria.AccountId); if (account == null) { result.ErrorMessage = "Unable to find or access the specified Account."; } else { result.Model = account; result.Success = true; } return(result); }
public async Task <ICrudResult <Recipe> > ReadAsync(ICrudCriteria <int> criteria) { // Initialize a result. var result = new SimpleCrudResult <Recipe>(); // Look for the recipe verifying that it belongs to the user before returning it. var recipe = await this.Context.Recipes .Include(r => r.Ingredients) .Include(r => r.Tags) .FirstOrDefaultAsync(a => a.Id == criteria.Value && a.AccountId == criteria.AccountId); if (recipe == null) { result.ErrorMessage = "Unable to find or access the specified Recipe."; } else { recipe.Tags = recipe.Tags == null ? new Tag[0] : recipe.Tags.OrderBy(t => t.Description).ToArray(); result.Model = recipe; result.Success = true; } return(result); }
public async Task <ICrudResult <Recipe> > UpdateAsync(ICrudCriteria <Recipe> criteria) { // Initialize a result. var result = new SimpleCrudResult <Recipe>() { Model = criteria.Value }; // Look for the recipe verifying that it belongs to the user before updating only desired properties. try { Recipe dbRecipe = null; if (criteria.Value != null) { // Get the Recipe and it's Ingredients, Steps and Tags dbRecipe = await this.Context.Recipes .Include(r => r.Ingredients) .Include(r => r.Tags) .FirstOrDefaultAsync(r => r.Id == criteria.Value.Id && r.AccountId == criteria.AccountId); } if (dbRecipe == null) { result.ErrorMessage = "Unable to find or access the specified Recipe."; return(result); } // Update primitive properties on Recipe dbRecipe.Description = criteria.Value.Description; dbRecipe.Directions = criteria.Value.Directions; dbRecipe.Name = criteria.Value.Name; dbRecipe.Notes = criteria.Value.Notes; dbRecipe.PreparationMinutes = criteria.Value.PreparationMinutes; dbRecipe.Servings = criteria.Value.Servings; // If the image file changed delete any current image file. if (!String.IsNullOrWhiteSpace(dbRecipe.ImageFileName) && !dbRecipe.ImageFileName.Equals(criteria.Value.ImageFileName, StringComparison.OrdinalIgnoreCase)) { await this.BlobStore.DeleteAsync(dbRecipe.ImageFileName); } dbRecipe.ImageFileName = criteria.Value.ImageFileName; // Remove missing children dbRecipe.Ingredients .Where(d => !criteria.Value.Ingredients.Any(i => i.Id == d.Id)) .Select(d => d.Id).ToList() .ToList() .ForEach(i => { var ingredient = dbRecipe.Ingredients.Single(n => n.Id == i); dbRecipe.Ingredients.Remove(ingredient); Context.Entry(ingredient).State = EntityState.Deleted; }); dbRecipe.Tags .Where(d => !criteria.Value.Tags.Any(t => t.Id == d.Id)) .Select(d => d.Id).ToList() .ToList() .ForEach(i => { dbRecipe.Tags.Remove(dbRecipe.Tags.Single(t => t.Id == i)); }); // Add or update Ingredients foreach (var ingredient in criteria.Value.Ingredients) { var dbIngredient = dbRecipe.Ingredients.FirstOrDefault(i => i.Id == ingredient.Id); if (dbIngredient == null) { dbIngredient = new Ingredient() { RecipeId = dbRecipe.Id }; dbRecipe.Ingredients.Add(dbIngredient); } dbIngredient.Description = ingredient.Description; dbIngredient.Quantity = ingredient.Quantity; dbIngredient.Units = ingredient.Units; } // Add new Tags foreach (var tag in criteria.Value.Tags.Where(t => !dbRecipe.Tags.Any(d => d.Id == t.Id))) { this.Context.Tags.Attach(tag); dbRecipe.Tags.Add(tag); } // Save and handle results var saveCount = await this.Context.SaveChangesAsync(); if (saveCount > 0) { result.Model = dbRecipe; result.Success = true; } else { result.ErrorMessage = "The Recipe failed to save."; } } catch (DbUpdateConcurrencyException) { result.ErrorMessage = "The Recipe could not be saved because it was out of date."; } return(result); }