Exemplo n.º 1
0
        public async Task <BaseValueSegmentDto> CreateAsync(BaseValueSegmentDto baseValueSegmentDto)
        {
            if (baseValueSegmentDto.Id.HasValue)
            {
                throw new BadRequestException(string.Format("Id {0} is invalid.", baseValueSegmentDto.Id));
            }

            if (baseValueSegmentDto.BaseValueSegmentTransactions.Count == 0)
            {
                throw new BadRequestException("No Base Value Segment Transaction defined for the Base Value Segment");
            }

            foreach (var transaction in baseValueSegmentDto.BaseValueSegmentTransactions)
            {
                if (transaction.BaseValueSegmentOwners == null ||
                    transaction.BaseValueSegmentOwners.Count == 0)
                {
                    throw new BadRequestException("No Base Value Segment Owners defined for the Base Value Segment Transaction");
                }

                if (transaction.BaseValueSegmentValueHeaders == null ||
                    transaction.BaseValueSegmentValueHeaders.Count == 0)
                {
                    throw new BadRequestException("No Base Value Segment Value Headers defined for the Base Value Segment Transaction");
                }
            }

            // We are going to create a brand new Base Value Segment. In order for EF to know,
            // we are going to set all Primary Keys to 0.

            var eventsCreated = await _grmEventDomain.Create(baseValueSegmentDto);

            try
            {
                var entity = baseValueSegmentDto.ToEntity();

                entity.Id = 0;

                var baseValueSegmentOwnerValues = new List <BaseValueSegmentOwnerValue>();

                foreach (var baseValueSegmentTransaction in entity.BaseValueSegmentTransactions)
                {
                    PrepareTransactionForCreate(baseValueSegmentTransaction, false);
                }

                foreach (var assessmentRevisionBaseValueSegmentDto in entity.BaseValueSegmentAssessmentRevisions)
                {
                    assessmentRevisionBaseValueSegmentDto.Id = 0;
                    assessmentRevisionBaseValueSegmentDto.BaseValueSegmentId = 0;
                }

                foreach (var entityBaseValueSegmentTransaction in entity.BaseValueSegmentTransactions)
                {
                    DiscoverOwnerValuesForSavingInTransaction(() =>
                                                              _bvsRepository.GetUserTransactionType(),
                                                              () => _bvsRepository.GetUserDeletedTransactionType(),
                                                              entityBaseValueSegmentTransaction, baseValueSegmentOwnerValues);
                }

                foreach (var entityBaseValueSegmentAssessmentRevision in entity.BaseValueSegmentAssessmentRevisions)
                {
                    var newStatusType = _bvsRepository.GetNewStatusType();
                    entityBaseValueSegmentAssessmentRevision.BaseValueSegmentStatusType   = newStatusType;
                    entityBaseValueSegmentAssessmentRevision.BaseValueSegmentStatusTypeId = newStatusType.Id;
                }

                // Client side code does not necessary set this value when passing over,
                // In the case of when an assessment event does not have any current BVS, then
                // this issue will be seen. We are thus covering this case on the server side.
                foreach (var entityBaseValueSegmentTransaction in entity.BaseValueSegmentTransactions)
                {
                    if (string.IsNullOrEmpty(entityBaseValueSegmentTransaction.EffectiveStatus))
                    {
                        entityBaseValueSegmentTransaction.EffectiveStatus = "A";
                    }
                }

                await _bvsRepository.CreateAsync(entity, baseValueSegmentOwnerValues);

                return(entity.ToDomain());
            }
            catch
            {
                _grmEventDomain.Delete(eventsCreated);
                throw;
            }
        }