Exemplo n.º 1
0
 public async Task <DataResult <UnitUpdateResponse> > Update([FromBody] UnitCreateOrUpdateDTO model, int unitId)
 {
     model.Id = unitId;
     return(await _unitEditService.ProcessUnitUpdate(model));
 }
Exemplo n.º 2
0
 public async Task <DataResult <UnitAddResponse> > Create([FromBody] UnitCreateOrUpdateDTO model)
 {
     return(await _unitEditService.ProcessUnitCreate(model));
 }
Exemplo n.º 3
0
 public async Task <DataResult <UnitUpdateResponse> > ProcessUnitUpdate(UnitCreateOrUpdateDTO model)
 {
     return(await ProcessUnitUpdateTransaction(model));
 }
Exemplo n.º 4
0
        private async Task <DataResult <UnitUpdateResponse> > ProcessUnitUpdateTransaction(UnitCreateOrUpdateDTO model)
        {
            return(await _transactionManager.ExecuteInImplicitTransactionAsync(async() =>
            {
                DAL.Entities.Tables.UnitTree unitEntity = await _unitOfWork.Units.GetByIdAsync(model.Id);
                if (unitEntity == null)
                {
                    return new DataResult <UnitUpdateResponse>()
                    {
                        ResponseStatusType = ResponseStatusType.Error,
                        Message = ResponseMessageType.NotFound
                    };
                }
                if (string.IsNullOrWhiteSpace(model.Name))
                {
                    return new DataResult <UnitUpdateResponse>()
                    {
                        ResponseStatusType = ResponseStatusType.Error,
                        Message = ResponseMessageType.IncorrectParameter
                    };
                }
                unitEntity.Name = model.Name;
                await _unitOfWork.Units.UpdateAsync(unitEntity);
                await _unitOfWork.SaveAsync();

                return new DataResult <UnitUpdateResponse>()
                {
                    ResponseStatusType = ResponseStatusType.Succeed,
                    Data = new UnitUpdateResponse()
                    {
                        UnitId = unitEntity.Id,
                        Name = unitEntity.Name
                    }
                };
            }));
        }
Exemplo n.º 5
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
                    }
                };
            }));
        }