/// <summary> /// Update Famer information /// </summary> /// <param name="entity"></param> /// <returns></returns> public ChangeDataResult UpdateFamerWithPersonal(Famer entity) { if (entity.Personal == null) { return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.DATA_VALIDATE_ERROR })); } var result = Validate(entity); //validate faild if (!result.Succeeded) { return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.DATA_VALIDATE_ERROR })); } //validate success _unitOfWork.BeginTransaction(); try { //update personal information result = _personalService.Update(entity.Personal); if (!result.Succeeded) { return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.SQL_ACTION_ERROR, Description = result.GetError() })); } //update personal success //then update employee entity.Personal = null; _famerRepository.Update(entity); _unitOfWork.Commit(); _unitOfWork.SaveChanges(); return(new ChangeDataResult()); } catch (SqlException ex) { //error, rollback change _unitOfWork.RollBack(); return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.SQL_ACTION_ERROR, Description = ex.Message })); } }
/// <summary> /// Add new famer /// </summary> /// <param name="entity">Famer infomation</param> /// <returns></returns> public override ChangeDataResult Add(Famer entity) { ChangeDataResult result; if (entity.Personal == null) { result = ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.DATA_VALIDATE_ERROR, Description = "Phải điền đầy đủ thông tin về nông dân" }); return(result); } result = Validate(entity); //validate entity faild if (!result.Succeeded) { return(result); } _unitOfWork.BeginTransaction(); try { //add personal result = _personalService.Add(entity.Personal); //add personal fail if (!result.Succeeded) { return(result); } //add personal success //then add employee entity.Id = Guid.NewGuid(); _famerRepository.Add(entity); _unitOfWork.Commit(); _unitOfWork.SaveChanges(); return(result); } catch (Exception ex) { _unitOfWork.RollBack(); return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.SQL_ACTION_ERROR, Description = ex.Message })); } }
/// <summary> /// Update employee /// </summary> /// <param name="entity"></param> /// <returns></returns> public override ChangeDataResult Update(Employee entity) { var error = Validate(entity); if (!error.Succeeded) { return(error); } try { _unitOfWork.BeginTransaction(); //update relationship of entity ChangeDataResult updateResult; if (entity.Personal != null) { updateResult = _personalService.Update(entity.Personal); if (!updateResult.Succeeded) { return(updateResult); } } //update entity entity.Personal = null; _employeeRepository.Update(entity); _unitOfWork.SaveChanges(); _unitOfWork.Commit(); return(new ChangeDataResult()); } catch (SqlException ex) { _unitOfWork.RollBack(); return(ChangeDataResult.Fails(new ChangeDataError() { Code = MessageCode.SQL_ACTION_ERROR, Description = ex.Message })); } }
public override ChangeDataResult Add(Employee entity) { ChangeDataResult result; _unitOfWork.BeginTransaction(); try { if (entity == null) { throw new ArgumentNullException(); } Personal personal = entity.Personal; result = _personalService.Add(personal); if (!result.Succeeded) { return(result); } result = Validate(entity); if (!result.Succeeded) { return(result); } entity.Id = Guid.NewGuid(); _employeeRepository.Add(entity); _unitOfWork.Commit(); _unitOfWork.SaveChanges(); return(result); } catch (Exception ex) { _unitOfWork.RollBack(); return(ChangeDataResult.Fails(new ChangeDataError { Code = MessageCode.SQL_ACTION_ERROR, Description = ex.Message })); } }