/// <summary> /// Creates a new sandwich /// </summary> /// <param name="sandwich">Sandwich to create</param> /// <returns>Id of the new created sandwich</returns> public async virtual Task <int> SandwichCreate(Sandwich sandwich) { //Map the DTO to entities SANDWICH sandwichBase = _mapper.Map <SANDWICH>(sandwich); using (IUnitOfWork uow = new UnitOfWork((DbContext)Activator.CreateInstance(_contextType))) { //Get a new sequence's value sandwichBase.Id = uow.GetNextValInSequence <int>("SANDWICH_SEQUENCE"); //Save the data uow.SandwichRepository.Create(sandwichBase); int sandwichId = sandwichBase.Id; foreach (Ingredient ingredient in sandwich.Ingredients) { SANDWICH_INGREDIENT data = new SANDWICH_INGREDIENT() { Id = uow.GetNextValInSequence <int>("SANDWICH_INGREDIENT_SEQUENCE"), IngredientId = ingredient.Id, SandwichId = sandwichId }; uow.SandwichIngredientRepository.Create(data); } //Commit the changes await uow.CommitAsync(); } return(sandwichBase.Id); }
/// <summary> /// Updates a sandwich /// </summary> /// <param name="sandwich">Sandwich to update</param> /// <returns>Task for async operations</returns> public async virtual Task SandwichUpdate(Sandwich sandwich) { //Map the DTO to entities SANDWICH receivedData = _mapper.Map <SANDWICH>(sandwich); using (IUnitOfWork uow = new UnitOfWork((DbContext)Activator.CreateInstance(_contextType))) { //Update the data uow.SandwichRepository.Update(receivedData); //Commit the changes await uow.CommitAsync(); } }