private void ValidateWasteRole(WasteRoleSaveInput input) { var listData = _repository.Get(c => c.USER_ID == input.WasteRoleDto.USER_ID).ToList(); if (listData.Count > 0) { //if (input.WasteRoleDto.WASTE_ROLE_ID > 0) //{ // var originalData = _repository.GetByID(input.WasteRoleDto.WASTE_ROLE_ID); // if (originalData == null) // throw new BLLException(ExceptionCodes.BLLExceptions.DataNotFound); // if (input.WasteRoleDto.WERKS != originalData.WERKS) // { // //find is user have another plant // var data = // _repository.Get( // c => c.USER_ID == input.WasteRoleDto.USER_ID && c.WERKS != input.WasteRoleDto.WERKS).ToList(); // if (data.Count > 0) // throw new Exception(string.Format("User already have Plant : {0} .", data[0].WERKS)); // } //} //else //{ // foreach (var wasteRole in listData) // { // if (wasteRole.WERKS != input.WasteRoleDto.WERKS) // throw new Exception(string.Format("User already have Plant : {0} .", wasteRole.WERKS)); // //throw new BLLException(ExceptionCodes.BLLExceptions.UserHavePlantExist); // } //} if (input.WasteRoleDto.WASTE_ROLE_ID == 0) { foreach (var wasteRole in listData) { if (wasteRole.WERKS != input.WasteRoleDto.WERKS) { throw new Exception(string.Format("User already have Plant : {0} .", wasteRole.WERKS)); } } } var dataUser = _repository.Get( c => c.USER_ID == input.WasteRoleDto.USER_ID && c.WERKS == input.WasteRoleDto.WERKS && c.GROUP_ROLE == input.WasteRoleDto.GROUP_ROLE).FirstOrDefault(); if (dataUser != null) { throw new Exception(string.Format("User And Plant already have Role : {0} .", EnumHelper.GetDescription(input.WasteRoleDto.GROUP_ROLE))); } } }
private WasteRoleDto SaveWasteRoleToDatabase(WasteRoleFormViewModel model) { var dataToSave = Mapper.Map <WasteRoleDto>(model); var input = new WasteRoleSaveInput() { WasteRoleDto = dataToSave, UserId = CurrentUser.USER_ID, UserRole = CurrentUser.UserRole, }; return(_wasteRoleBll.SaveWasteRole(input)); }
public WasteRoleDto SaveWasteRole(WasteRoleSaveInput input) { ValidateWasteRole(input); foreach (var wasteRoleDetailsDto in input.WasteRoleDto.Details) { WASTE_ROLE dbData = null; if (wasteRoleDetailsDto.WASTE_ROLE_ID > 0) { int idForHistory = (from wasteRoleDetailsDto2 in input.WasteRoleDto.Details where wasteRoleDetailsDto2.IsChecked select wasteRoleDetailsDto2.WASTE_ROLE_ID).FirstOrDefault(); if (wasteRoleDetailsDto.IsChecked) { //update dbData = _repository.GetByID(wasteRoleDetailsDto.WASTE_ROLE_ID); if (dbData == null) { throw new BLLException(ExceptionCodes.BLLExceptions.DataNotFound); } //set changes history var origin = Mapper.Map <WasteRoleDto>(dbData); input.WasteRoleDto.GROUP_ROLE = wasteRoleDetailsDto.WasteGroup; input.WasteRoleDto.WASTE_ROLE_ID = wasteRoleDetailsDto.WASTE_ROLE_ID; SetChangesHistory(origin, input.WasteRoleDto, input.UserId); //Mapper.Map<WasteRoleDto, WASTE_ROLE>(input.WasteRoleDto, dbData); dbData.USER_ID = input.WasteRoleDto.USER_ID; dbData.WERKS = input.WasteRoleDto.WERKS; dbData.GROUP_ROLE = wasteRoleDetailsDto.WasteGroup; dbData.MODIFIED_DATE = DateTime.Now; dbData.MODIFIED_BY = input.UserId; } else { //find id from others row that have same user id and plant //delete //update history change to the existing one _changesHistoryBll.UpdateHistoryFormIdByFormType(wasteRoleDetailsDto.WASTE_ROLE_ID.ToString(), idForHistory.ToString(), Enums.MenuList.WasteRole); SetChangeHistory(EnumHelper.GetDescription(wasteRoleDetailsDto.WasteGroup), "", "WASTE_GROUP", input.UserId, idForHistory.ToString()); DeleteById(wasteRoleDetailsDto.WASTE_ROLE_ID); } } else { if (wasteRoleDetailsDto.IsChecked) { //input.WasteRoleDto.CREATED_DATE = DateTime.Now; //input.WasteRoleDto.CREATED_BY = input.UserId; //dbData = new WASTE_ROLE(); //Mapper.Map<WasteRoleDto, WASTE_ROLE>(input.WasteRoleDto, dbData); dbData = new WASTE_ROLE(); dbData.USER_ID = input.WasteRoleDto.USER_ID; dbData.WERKS = input.WasteRoleDto.WERKS; dbData.GROUP_ROLE = wasteRoleDetailsDto.WasteGroup; dbData.CREATED_BY = input.UserId; dbData.CREATED_DATE = DateTime.Now; if (input.WasteRoleDto.WASTE_ROLE_ID > 0) { SetChangeHistory("", EnumHelper.GetDescription(wasteRoleDetailsDto.WasteGroup), "WASTE_GROUP", input.UserId, input.WasteRoleDto.WASTE_ROLE_ID.ToString()); } _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<WasteRoleDto>(dbData); return(input.WasteRoleDto); }