public async Task <ViewResult <ContractNumberModel> > AddOrUpdate(ContractNumberModel model, string userName)
        {
            var result = new ViewResult <ContractNumberModel>();

            try
            {
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    ContractNumber contractNumber;
                    var            contractNumberTempByName =
                        await _contractNumberRepository.GetContractByName(model.Name, model.Id);

                    if (contractNumberTempByName != null)
                    {
                        result.Status  = -2;
                        result.Message = "Contract number is already taken";
                        return(result);
                    }
                    if (model.Id == 0)
                    {
                        contractNumber            = model.ToContractNumberModel();
                        contractNumber.CreateTime = DateTime.UtcNow;
                        contractNumber.CreateBy   = userName;
                    }
                    else
                    {
                        var contractNumberTemp = await _contractNumberRepository.GetById(model.Id);

                        if (contractNumberTemp == null)
                        {
                            result.Status  = -3;
                            result.Message = "This contract does not exist";
                            return(result);
                        }
                        contractNumber            = model.ToContractNumberModel();
                        contractNumber.CreateTime = contractNumberTemp.CreateTime;
                        contractNumber.CreateBy   = contractNumberTemp.CreateBy;
                        contractNumber.EditTime   = DateTime.UtcNow;
                        contractNumber.EditBy     = userName;
                    }
                    await _contractNumberRepository.AddOrUpdate(contractNumber);

                    result.Data = contractNumber.ToContractNumberModel();

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                result.Status  = -1;
                result.Message = ex.Message;
                Logger.WriteErrorLog("ContractNumberService", "AddOrUpdate", ex);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ViewResult <ContractNumberModel> > AddOrUpdate(ContractNumberModel model)
        {
            var userName = User.Identity.Name;

            return(await _contractNumberService.AddOrUpdate(model, userName));
        }