/// <summary> /// add or update manager /// </summary> /// <param name="model"></param> /// <param name="userName"></param> /// <returns></returns> public async Task <ViewResult <ManagerModel> > AddOrUpdate(ManagerModel model, string userName) { var result = new ViewResult <ManagerModel>(); try { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { //check duplicated name var dupCustomer = await _customerRepository.GetCustomerByLoginId(model.LoginName); var dupName = await _managerRepository.GetManagerByName(model.LoginName, model.Id); var dupManagerName = await _managerRepository.GetManagerByManagerName(model.Name, model.Id); if (dupManagerName != null) { result.Status = -2; result.Message = "Name is already taken"; return(result); } if (dupCustomer != null || dupName != null) { result.Status = -2; result.Message = "Login Id is already taken"; return(result); } Manager manager; //new manager model if (model.Id == 0) { manager = model.ToManagerModel(); manager.CreateTime = DateTime.UtcNow; manager.CreateBy = userName; } else { var managerModel = await _managerRepository.GetById(model.Id); if (managerModel == null) { result.Status = -3; result.Message = "Manager does not exist"; return(result); } var customer = await _customerRepository.GetByManagerId(model.Id); if (customer != null && managerModel.Role == "Manager" && model.Role == "Admin") { result.Status = -3; result.Message = $"Manager is used by {customer.CompanyName}"; return(result); } manager = model.ToManagerModel(); manager.CreateTime = managerModel.CreateTime; manager.CreateBy = managerModel.CreateBy; manager.EditTime = DateTime.UtcNow; manager.EditBy = userName; } await _managerRepository.AddOrUpdate(manager); var managerResult = await _managerRepository.GetById(manager.Id); result.Data = managerResult.ToManagerModel(); scope.Complete(); } } catch (Exception ex) { result.Status = -1; result.Message = ex.Message; Logger.WriteErrorLog("ManagerService", "AddOrUpdate", ex); } return(result); }