Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,ClassOfFood")] MainIngredient mainIngredient)
        {
            if (!ModelState.IsValid)
            {
                string msg = (ModelState.FirstOrDefault(x => x.Value.Errors.Any()).Value.Errors.FirstOrDefault().ErrorMessage).Replace("'", "");
                _toastNotification.AddErrorToastMessage(msg);

                return(View(mainIngredient));
            }
            if (ModelState.IsValid)
            {
                var checkExit = _context.MainIngredient.Where(x => x.Name.ToLower() == mainIngredient.Name.ToLower()).Count();

                if (checkExit == 0)
                {
                    _context.Add(mainIngredient);
                    await _context.SaveChangesAsync();

                    _toastNotification.AddSuccessToastMessage(ResponseMessageUtilities.CREATED_SUCESSFUL);
                    return(RedirectToAction(nameof(Index)));
                }
                _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.ITEM_EXIST);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(mainIngredient));
        }
        public async Task <IActionResult> PutMainIngredient([FromRoute] int id, [FromBody] MainIngredient mainIngredient)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != mainIngredient.Id)
            {
                return(BadRequest());
            }

            _context.Entry(mainIngredient).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MainIngredientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,ClassOfFood")] MainIngredient mainIngredient)
        {
            if (!ModelState.IsValid)
            {
                string msg = (ModelState.FirstOrDefault(x => x.Value.Errors.Any()).Value.Errors.FirstOrDefault().ErrorMessage).Replace("'", "");
                _toastNotification.AddErrorToastMessage(msg);

                return(View(mainIngredient));
            }

            var CheckExist = _context.MainIngredient.Where(x => x.Id != mainIngredient.Id && x.Name.ToLower() == mainIngredient.Name.ToLower()).Count();

            if (CheckExist == 0)
            {
                var model = _context.MainIngredient.FirstOrDefault(x => x.Id == mainIngredient.Id);
                model.Name        = mainIngredient.Name;
                model.ClassOfFood = mainIngredient.ClassOfFood;
                await _context.SaveChangesAsync();

                _toastNotification.AddSuccessToastMessage(ResponseMessageUtilities.UPDATE_SUCESSFUL);

                return(RedirectToAction(nameof(Index)));
            }
            _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.ITEM_EXIST);
            return(View(mainIngredient));
        }
Beispiel #4
0
        public async Task <ActionResult> AddMainIngredient([FromBody] StandartDto data)
        {
            var id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var loginedUser = await _auth.VerifyUser(id);

            MainIngredient mainIngredient = new MainIngredient()
            {
                Name    = data.Name,
                Company = loginedUser.Company,
                Status  = true
            };

            try
            {
                await _context.MainIngredients.AddAsync(mainIngredient);

                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            return(StatusCode(201));
        }
        public async Task <IActionResult> PostMainIngredient([FromBody] MainIngredient mainIngredient)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.MainIngredients.Add(mainIngredient);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetMainIngredient", new { id = mainIngredient.Id }, mainIngredient));
        }
Beispiel #6
0
        public void Post_Creates_New_MainIngredient()
        {
            var newMainIngredient  = new MainIngredient(1, "New MainIngredient", "new img");
            var mainingredientList = new List <MainIngredient>();

            mainingredientRepo.When(t => t.Create(newMainIngredient))
            .Do(t => mainingredientList.Add(newMainIngredient));

            mainingredientRepo.GetAll().Returns(mainingredientList);

            var result = underTest.Post(newMainIngredient);

            Assert.Contains(newMainIngredient, result);
        }
Beispiel #7
0
        public void Put_Updates_An_MainIngredient()
        {
            var originalMainIngredient = new MainIngredient(1, "MainIngredient", "img");
            var expectedMainIngredient = new List <MainIngredient>()
            {
                originalMainIngredient
            };

            var updatedMainIngredient = new MainIngredient(1, "Updated MainIngredient", "Updated img");

            mainingredientRepo.When(t => mainingredientRepo.Update(updatedMainIngredient))
            .Do(Callback.First(t => expectedMainIngredient.Remove(originalMainIngredient))
                .Then(t => expectedMainIngredient.Add(updatedMainIngredient)));
            mainingredientRepo.GetAll().Returns(expectedMainIngredient);

            var result = underTest.Put(updatedMainIngredient);

            Assert.All(result, item => Assert.Contains("Updated MainIngredient", item.Image));
        }
Beispiel #8
0
        public void Delete_Removes_An_MainIngredient()
        {
            var mainingredientId      = 1;
            var deletedMainIngredient = new MainIngredient(mainingredientId, "MainIngredient1", "Img");
            var mainingredientList    = new List <MainIngredient>()
            {
                deletedMainIngredient,
                new MainIngredient(2, "MainIngredient2", "img2")
            };

            mainingredientRepo.GetById(mainingredientId).Returns(deletedMainIngredient);
            mainingredientRepo.When(d => d.Delete(deletedMainIngredient))
            .Do(d => mainingredientList.Remove(deletedMainIngredient));
            mainingredientRepo.GetAll().Returns(mainingredientList);

            var result = underTest.Delete(mainingredientId);

            Assert.All(result, item => Assert.Contains("MainIngredient2", item.Image));
        }
 public IEnumerable <MainIngredient> Put([FromBody] MainIngredient mainingredient)
 {
     mainingredientRepo.Update(mainingredient);
     return(mainingredientRepo.GetAll());
 }