Example #1
0
        public static async Task AddAsync(ReagentViewModel reagentVM)
        {
            Reagent reagent = SimplifyCreate(reagentVM);
            await _context.Reagents.AddAsync(reagent);

            await _context.SaveChangesAsync();
        }
Example #2
0
        public ActionResult Save(Reagent reagent)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new ReagentViewModel(reagent)
                {
                    Units            = _uow.GetRepository <Unit>().GetAll(),
                    StorageLocations = _uow.GetRepository <StorageLocation>().GetAll()
                };
                return(View("ReagentForm", viewModel));
            }

            bool reagentExist = _uow.GetRepository <Reagent>().CheckIfExist(r => r.Name == reagent.Name && r.StorageLocationId == reagent.StorageLocationId);

            Reagent reagentWithTheSameNameAndLocation = new Reagent();

            if (reagentExist)
            {
                reagentWithTheSameNameAndLocation = _uow.GetRepository <Reagent>().Get(r => r.Name == reagent.Name && r.StorageLocationId == reagent.StorageLocationId);
            }

            if (reagent.Id == 0)
            {
                if (!reagentExist)
                {
                    _uow.GetRepository <Reagent>().Add(reagent);
                }
                else
                {
                    return(RedirectToAction("New", "Reagents").WithError("Odczynnik o podanej nazwie istnieje już w danej lokalizacji!"));
                }
            }
            else
            {
                var reagentInDb = _uow.GetRepository <Reagent>().Get(r => r.Id == reagent.Id);

                if (!reagentExist || (reagentExist && reagentWithTheSameNameAndLocation.Id == reagent.Id))
                {
                    reagentInDb.Name              = reagent.Name;
                    reagentInDb.InitialAmount     = reagent.InitialAmount;
                    reagentInDb.ConsumedAmount    = reagent.ConsumedAmount;
                    reagentInDb.FinalAmount       = reagent.FinalAmount;
                    reagentInDb.UnitId            = reagent.UnitId;
                    reagentInDb.StorageLocationId = reagent.StorageLocationId;
                    reagentInDb.Comment           = reagent.Comment;
                }
                else
                {
                    return(RedirectToAction("New", "Reagents").WithError("Odczynnik o podanej nazwie istnieje już w danej lokalizacji!"));
                }
            }
            _uow.SaveChanges();

            return(RedirectToAction("Index", "Reagents"));
        }
Example #3
0
 private static Reagent SimplifyCreate(ReagentViewModel reagentVM)
 {
     return(new Reagent()
     {
         ID = reagentVM.ID,
         ReagentCode = reagentVM.ReagentCode,
         Name = reagentVM.Name,
         InStock = reagentVM.InStock,
         MinimumAmountInStock = reagentVM.MinimumAmountInStock
     });
 }
Example #4
0
        public ViewResult New()
        {
            var units     = _uow.GetRepository <Unit>().GetAll();
            var locations = _uow.GetRepository <StorageLocation>().GetAll();
            var viewModel = new ReagentViewModel
            {
                Units            = units,
                StorageLocations = locations
            };

            return(View("ReagentForm", viewModel));
        }
Example #5
0
        public static async Task UpdateAsync(int id, ReagentViewModel reagentVM)
        {
            Reagent reagentToUpdate = await _context.Reagents.FindAsync(id);

            reagentToUpdate.ReagentCode          = reagentVM.ReagentCode;
            reagentToUpdate.Name                 = reagentVM.Name;
            reagentToUpdate.InStock              = reagentVM.InStock;
            reagentToUpdate.MinimumAmountInStock = reagentVM.MinimumAmountInStock;

            _context.Reagents.Update(reagentToUpdate);
            await _context.SaveChangesAsync();
        }
Example #6
0
        // GET: Reagents/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ReagentViewModel reagent = await ReagentHelper.GetByIdAsync((int)id);

            if (reagent == null)
            {
                return(NotFound());
            }
            return(View(reagent));
        }
Example #7
0
        public async Task <IActionResult> Delete(int id, bool?saveChangesError = false)
        {
            ReagentViewModel reagent = await ReagentHelper.GetByIdAsync((int)id);

            if (reagent == null)
            {
                return(NotFound());
            }

            if (saveChangesError.GetValueOrDefault())
            {
                ViewData["ErrorMessage"] = "Delete failed. Try again, and if the problem persists " +
                                           "see your system administrator.";
            }
            return(View(reagent));
        }
Example #8
0
        public ActionResult Edit(int id)
        {
            var reagent = _uow.GetRepository <Reagent>().Get(r => r.Id == id);

            if (reagent == null)
            {
                return(HttpNotFound());
            }
            var viewModel = new ReagentViewModel(reagent)
            {
                Units            = _uow.GetRepository <Unit>().GetAll(),
                StorageLocations = _uow.GetRepository <StorageLocation>().GetAll()
            };

            return(View("ReagentForm", viewModel));
        }
Example #9
0
        public async Task <IActionResult> Create(ReagentViewModel reagentVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    await ReagentHelper.AddAsync(reagentVM);

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException)
            {
                //Log the error
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }
            return(View(reagentVM));
        }
Example #10
0
        public async Task <IActionResult> Edit(int id, ReagentViewModel reagentVM)
        {
            if (id != reagentVM.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await ReagentHelper.UpdateAsync(id, reagentVM);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateException)
                {
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
            }
            return(View(reagentVM));
        }