Exemple #1
0
        public Result <AgreementModel> PutAgreementModel(AgreementModel agreementModel)
        {
            var validateResult = _validatorService.ValidateAgreement(agreementModel);

            if (validateResult.ResultType != ResultType.Ok)
            {
                Result <AgreementModel> result = new BadRequestResult <AgreementModel>(validateResult.Errors);
                if (_errorService.AddError(
                        (agreementModel.CNPCUI != null ? agreementModel.CNPCUI : "invalid"),
                        result.ResultType,
                        result.Errors,
                        "Put") == false)
                {
                    Console.WriteLine("Error service failed");
                }
                return(result);
            }

            try
            {
                if (!_agreementRepository.AgreementExists(agreementModel.CNPCUI))
                {
                    Result <AgreementModel> result = new NotFoundResult <AgreementModel>("Agreement not found!");
                    if (_errorService.AddError(agreementModel.CNPCUI, result.ResultType, result.Errors, "Put") == false)
                    {
                        Console.WriteLine("Error service failed");
                    }
                    return(result);
                }
                bool success = _agreementRepository.UpdateAgreement(agreementModel);

                if (!success)
                {
                    Result <AgreementModel> result = new BadRequestResult <AgreementModel>("Bad request: Agreement update failed!");
                    if (_errorService.AddError(agreementModel.CNPCUI, result.ResultType, result.Errors, "Put") == false)
                    {
                        Console.WriteLine("Error service failed");
                    }
                    return(result);
                }
            }
            catch
            {
                Result <AgreementModel> result = new UnexpectedResult <AgreementModel>();
                if (_errorService.AddError(agreementModel.CNPCUI, result.ResultType, result.Errors, "Put") == false)
                {
                    Console.WriteLine("Error service failed");
                }
                return(result);
            }
            return(new SuccessResult <AgreementModel>(agreementModel));
        }