public async Task <string> UpdateCake(EditAndDeleteViewModel model)
        {
            if (this.repository.All().Any(p => p.Name == model.Name && p.Id != model.Id))
            {
                return("Product with such name already exists.");
            }

            if (this.repository.All().Any(p => p.Image == model.Image && p.Id != model.Id))
            {
                return("Product with such image url already exists.");
            }

            var product = this.mapper.Map <EditAndDeleteViewModel, Product>(model);

            this.repository.Update(product);

            try
            {
                await this.repository.SaveChangesAsync();
            }
            catch (Exception e)
            {
                this.logger.LogDebug(e.Message);

                return("Sorry, an error occurred and your request couldn't be processed.");
            }

            return("true");
        }
Beispiel #2
0
        public async Task <IActionResult> Delete(EditAndDeleteViewModel model)
        {
            try
            {
                //await this.cakeService.DeleteCake(model);
                await this.cakeService.SoftDelete(model.Id);
            }
            catch (Exception e)
            {
                ViewData["Errors"] = e.Message;

                return(this.View("Error"));
            }

            return(this.RedirectToAction("Index"));
        }
Beispiel #3
0
        public async Task DeleteCake_WithInvalidId_ShouldDoNoting()
        {
            //Arange
            var db = this.SetDb();

            await this.SeedProducts(db);

            var repo = new Repository <Product>(db);

            var cakeService = new CakeService(null, repo, this.Mapper);

            EditAndDeleteViewModel cake = null;

            //Act

            //Assert
            await Assert.ThrowsAsync <NullReferenceException>(async() => await cakeService.DeleteCake(cake));
        }
Beispiel #4
0
        public async Task <IActionResult> Edit(EditAndDeleteViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToList();

                var errorModel = this.errorService.GetErrorModel(errors);

                return(View("Error", errorModel));
            }

            var successMessage = await this.cakeService.UpdateCake(model);

            if (successMessage != "true")
            {
                return(this.View("Error", successMessage));
            }

            return(RedirectToAction("Index"));
        }
        public async Task DeleteCake(EditAndDeleteViewModel model)
        {
            if (model == null)
            {
                throw new NullReferenceException("Cake not found.");
            }

            var cake = await this.repository.GetByIdAsync(model.Id);

            cake.IsDeleted = true;

            this.repository.Update(cake);

            try
            {
                await this.repository.SaveChangesAsync();
            }
            catch (Exception e)
            {
                this.logger.LogDebug(e.Message);

                throw new InvalidOperationException("Sorry, an error occurred while trying to delete cake.");
            }
        }