public WasteStockDto SaveWasteStock(WasteStockSaveInput input)
        {
            ValidateWasteStock(input);

            WASTE_STOCK dbData = null;

            if (input.WasteStockDto.WASTE_STOCK_ID > 0)
            {
                //update
                dbData = _repository.GetByID(input.WasteStockDto.WASTE_STOCK_ID);
                if (dbData == null)
                {
                    throw new BLLException(ExceptionCodes.BLLExceptions.DataNotFound);
                }

                //set changes history
                var origin = Mapper.Map <WasteStockDto>(dbData);

                SetChangesHistory(origin, input.WasteStockDto, input.UserId);

                Mapper.Map <WasteStockDto, WASTE_STOCK>(input.WasteStockDto, dbData);

                dbData.MODIFIED_DATE = DateTime.Now;
                dbData.MODIFIED_BY   = input.UserId;
            }
            else
            {
                input.WasteStockDto.CREATED_DATE = DateTime.Now;
                input.WasteStockDto.CREATED_BY   = input.UserId;
                dbData = new WASTE_STOCK();
                Mapper.Map <WasteStockDto, WASTE_STOCK>(input.WasteStockDto, dbData);
                _repository.Insert(dbData);
            }

            try
            {
                _uow.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }


            return(Mapper.Map <WasteStockDto>(dbData));
        }
Example #2
0
        //[ValidateAntiForgeryToken]
        //[HttpPost]
        //public ActionResult Edit(WasteStockFormViewModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
        //            SaveWasteStockToDatabase(model);
        //            AddMessageInfo("Success update Waste Stock", Enums.MessageInfoType.Success);
        //            return RedirectToAction("Index");

        //        }
        //        catch (Exception ex)
        //        {
        //            AddMessageInfo("Update Failed : " + ex.Message, Enums.MessageInfoType.Error);
        //        }
        //    }

        //    model = SetListModel(model);
        //    return View("Edit", model);
        //}

        private WasteStockDto SaveWasteStockToDatabase(WasteStockFormViewModel model)
        {
            var dataToSave = Mapper.Map <WasteStockDto>(model);

            var input = new WasteStockSaveInput()
            {
                WasteStockDto = dataToSave,
                UserId        = CurrentUser.USER_ID,
                UserRole      = CurrentUser.UserRole,
            };

            return(_wasteStockBll.SaveWasteStock(input));
        }
        private void ValidateWasteStock(WasteStockSaveInput input)
        {
            bool isNeedCheck = false;

            if (input.WasteStockDto.WASTE_STOCK_ID == 0)
            {
                isNeedCheck = true;
            }
            else
            {
                var dbData =
                    _repository.Get(c => c.WASTE_STOCK_ID == input.WasteStockDto.WASTE_STOCK_ID).FirstOrDefault();

                if (dbData != null)
                {
                    if (dbData.WERKS != input.WasteStockDto.WERKS &&
                        dbData.MATERIAL_NUMBER != input.WasteStockDto.MATERIAL_NUMBER)
                    {
                        //check is the data already exist in database
                        isNeedCheck = true;
                    }
                }
            }

            if (isNeedCheck)
            {
                var dbData =
                    _repository.Get(
                        c =>
                        c.WERKS == input.WasteStockDto.WERKS &&
                        c.MATERIAL_NUMBER == input.WasteStockDto.MATERIAL_NUMBER).FirstOrDefault();

                if (dbData != null)
                {
                    throw new Exception(string.Format("Plant : {0}, and Material : {1} already exist.",
                                                      input.WasteStockDto.WERKS, input.WasteStockDto.MATERIAL_NUMBER));
                }
            }
        }