public void CreateMsPosition(MsPositionInput input) { Logger.Info("CreateMsPosition() - Started."); Logger.DebugFormat("CreateMsPosition() - Start checking existing code and name. Params sent:{0}" + "departmentID = {1}{0}" + "positionCode = {2}{0}" + "positionName = {3}" , Environment.NewLine, input.departmentID, input.positionCode, input.positionName); var checkPositionCode = (from x in _msPositionRepo.GetAll() where x.departmentID == input.departmentID && (x.positionCode == input.positionCode || x.positionName == input.positionName) select x).Any(); Logger.DebugFormat("CreateMsPosition() - End checking existing code and name. Result: {0}", checkPositionCode); if (!checkPositionCode) { var data = new MS_Position { positionName = input.positionName, positionCode = input.positionCode, departmentID = input.departmentID, isActive = input.isActive }; try { Logger.DebugFormat("CreateMsPosition() - Start insert position. Params sent:{0}" + "positionName = {1}{0}" + "positionCode = {2}{0}" + "departmentID = {3}{0}" + "departmentID = {4}" , Environment.NewLine, input.positionName, input.positionCode, input.departmentID, input.isActive); _msPositionRepo.Insert(data); CurrentUnitOfWork.SaveChanges(); Logger.DebugFormat("CreateMsPosition() - End insert position."); } catch (DataException ex) { Logger.ErrorFormat("CreateMsPosition() ERROR DataException. Result = {0}", ex.Message); throw new UserFriendlyException("Db Error: " + ex.Message); } catch (Exception ex) { Logger.ErrorFormat("CreateMsPosition() ERROR Exception. Result = {0}", ex.Message); throw new UserFriendlyException("Error: " + ex.Message); } } else { Logger.ErrorFormat("CreateMsPosition() ERROR. Result = {0}", "Position Code or Position Name Already Exist in This Department!"); throw new UserFriendlyException("Position Code or Position Name Already Exist in This Department!"); } Logger.Info("CreateMsPosition() - Finished."); }
public JObject UpdateMsPosition(MsPositionInput input) { Logger.Info("UpdateMsPosition() Started."); JObject obj = new JObject(); Logger.DebugFormat("UpdateMsPosition() - Start checking exiting code and name. Params sent:{0}" + "departmentID = {1}{0}" + "postionId = {2}{0}" + "positionCode = {3}{0}" + "positionName = {4}" , Environment.NewLine, input.departmentID, input.Id, input.positionCode, input.positionName); var checkPositionCode = (from A in _msPositionRepo.GetAll() where A.departmentID == input.departmentID && A.Id != input.Id && (A.positionCode == input.positionCode || A.positionName == input.positionName) select A).Any(); Logger.DebugFormat("UpdateMsPosition() - End checking exiting code and name. Result: {0}", checkPositionCode); Logger.DebugFormat("UpdateMsPosition() - Start checking MS_Officer."); var checkOfficer = (from A in _msOfficerRepo.GetAll() where A.positionID == input.Id select A).Any(); Logger.DebugFormat("UpdateMsPosition() - End checking MS_Officer. Result: {0}", checkOfficer); if (!checkPositionCode) { var getMsPosition = (from a in _msPositionRepo.GetAll() where a.Id == input.Id select a).FirstOrDefault(); var updateMsPosition = getMsPosition.MapTo <MS_Position>(); updateMsPosition.departmentID = input.departmentID; updateMsPosition.isActive = input.isActive; if (!checkOfficer) { updateMsPosition.positionName = input.positionName; updateMsPosition.positionCode = input.positionCode; obj.Add("message", "Edit Successfully"); } else { obj.Add("message", "Edit Successfully, but can't change Position Name & Code"); } try { Logger.DebugFormat("UpdateMsPosition() - Start update position. Params sent:{0}" + "positionName = {1}{0}" + "positionCode = {2}{0}" + "departmentID = {3}{0}" + "isActive = {4}" , Environment.NewLine, input.positionName, input.positionCode, input.departmentID, input.isActive); _msPositionRepo.Update(updateMsPosition); CurrentUnitOfWork.SaveChanges(); Logger.DebugFormat("UpdateMsPosition() - End update position."); } catch (DataException ex) { Logger.ErrorFormat("UpdateMsPosition() ERROR DataException. Result = {0}", ex.Message); throw new UserFriendlyException("Db Error: " + ex.Message); } catch (Exception ex) { Logger.ErrorFormat("UpdateMsPosition() ERROR Exception. Result = {0}", ex.Message); throw new UserFriendlyException("Error: " + ex.Message); } } else { Logger.ErrorFormat("UpdateMsPosition() ERROR. Result = {0}", "Position Code or Position Name Already Exist !"); throw new UserFriendlyException("Position Code or Position Name Already Exist !"); } Logger.Info("UpdateMsPosition() Finished."); return(obj); }