public async Task <DataResult <ClassificationAddResponse> > ProcessClassificationCreate(ClassificationCreateDTO model)
        {
            DAL.Entities.Tables.UnitTree unitTree = await _unitOfWork.Units.GetByIdExpandedAsync(model.ParentId);

            if (unitTree == null)
            {
                return(new DataResult <ClassificationAddResponse>()
                {
                    ResponseStatusType = ResponseStatusType.Error,
                    Message = ResponseMessageType.ParentIdMissing
                });
            }
            DAL.Entities.Tables.UnitType unitType = await _unitOfWork.UnitTypes.GetByIdAsync(model.UnitTypeId);

            if (unitType == null)
            {
                return(new DataResult <ClassificationAddResponse>()
                {
                    ResponseStatusType = ResponseStatusType.Error,
                    Message = ResponseMessageType.NotFound
                });
            }

            List <DAL.Entities.Tables.UnitType> allowedTypes = _unitTypeCache.GetFromCache(unitTree.UnitClassification.UnitType.Name);

            if (allowedTypes.All(x => x.Id != model.UnitTypeId))
            {
                return(new DataResult <ClassificationAddResponse>()
                {
                    Message = ResponseMessageType.OperationNotAllowedForUnitType,
                    ResponseStatusType = ResponseStatusType.Error
                });
            }
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                return(new DataResult <ClassificationAddResponse>()
                {
                    ResponseStatusType = ResponseStatusType.Error,
                    Message = ResponseMessageType.InvalidModel
                });
            }
            UnitClassification classification = _mapper.Map <UnitClassification>(model);
            await _unitOfWork.UnitClassifications.AddAsync(classification);

            await _unitOfWork.SaveAsync();

            return(new DataResult <ClassificationAddResponse>()
            {
                ResponseStatusType = ResponseStatusType.Succeed,
                Data = new ClassificationAddResponse()
                {
                    ClassificationId = classification.Id
                }
            });
        }
Exemple #2
0
        private async Task <DataResult <UnitAddResponse> > ProcessUnitCreateTransaction(UnitCreateOrUpdateDTO model)
        {
            return(await _transactionManager.ExecuteInImplicitTransactionAsync(async() =>
            {
                UnitClassification classification = await _unitOfWork.UnitClassifications.GetByIdAsync(model.UnitClassificationId);
                if (classification == null)
                {
                    return new DataResult <UnitAddResponse>()
                    {
                        Message = ResponseMessageType.ClassificationMissing,
                        ResponseStatusType = ResponseStatusType.Error
                    };
                }

                DAL.Entities.Tables.UnitTree parentUnit = await _unitOfWork.Units.GetByIdExpandedAsync(model.ParentId);

                if (parentUnit == null)
                {
                    return new DataResult <UnitAddResponse>()
                    {
                        Message = ResponseMessageType.ParentIdMissing,
                        ResponseStatusType = ResponseStatusType.Error
                    };
                }

                var allowedTypes = _unitTypeCache.GetFromCache(parentUnit.UnitClassification.UnitType.Name);
                if (!allowedTypes.Any(x => x.Id == classification.UnitType.Id && x.Name == classification.UnitType.Name))
                {
                    return new DataResult <UnitAddResponse>()
                    {
                        Message = ResponseMessageType.OperationNotAllowedForUnitType,
                        ResponseStatusType = ResponseStatusType.Error
                    };
                }
                DAL.Entities.Tables.UnitTree unitEntity = _mapper.Map <DAL.Entities.Tables.UnitTree>(model);

                await _unitOfWork.Units.AddAsync(unitEntity);
                await _unitOfWork.SaveAsync();

                await _unitOfWork.Units.UpdateBowers(unitEntity.Id, model.ParentId);
                return new DataResult <UnitAddResponse>()
                {
                    ResponseStatusType = ResponseStatusType.Succeed,
                    Data = new UnitAddResponse()
                    {
                        UnitId = unitEntity.Id,
                        ParentId = unitEntity.ParentId,
                        UnitClassificationId = unitEntity.UnitClassificationId
                    }
                };
            }));
        }