예제 #1
0
        public Result <AgreementModel> PostAgreementModel(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, "Post") == false)
                {
                    Console.WriteLine("Error service failed");
                }
                return(result);
            }

            try {
                if (_agreementRepository.AgreementExists(agreementModel.CNPCUI))
                {
                    Result <AgreementModel> result = new NotFoundResult <AgreementModel>("Agreement already exist!");
                    if (_errorService.AddError(agreementModel.CNPCUI, result.ResultType, result.Errors, "Post") == false)
                    {
                        Console.WriteLine("Error service failed");
                    }
                    return(result);
                }

                bool success = _agreementRepository.CreateAgreement(agreementModel);

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