Exemplo n.º 1
0
 public async Task <DataResult <ClassificationAddResponse> > Create([FromBody] ClassificationCreateDTO model)
 {
     return(await _classificationService.ProcessClassificationCreate(model));
 }
Exemplo n.º 2
0
        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
                }
            });
        }