Пример #1
0
        public async Task <long> CreateAsync(AttributeTypePostDTO attributeTypePostDTO)
        {
            if (attributeTypePostDTO.DefaultCustomValue == null &&
                (!attributeTypePostDTO.UsesDefinedValues || !attributeTypePostDTO.Values.Any()))
            {
                throw new ValidationException("У атрибута должно быть значение по умолчанию");
            }

            if (attributeTypePostDTO.UsesDefinedValues && !attributeTypePostDTO.Values.Any())
            {
                throw new ValidationException("У атрибута должно быть значение по умолчанию");
            }

            if (attributeTypePostDTO.UsesDefinedUnits && !attributeTypePostDTO.Units.Any())
            {
                throw new ValidationException("У атрибута должна быть единица измерения по умолчанию");
            }

            if (attributeTypePostDTO.Units.Any() && (0 > attributeTypePostDTO.DefaultUnitIndex ||
                                                     attributeTypePostDTO.DefaultUnitIndex >=
                                                     attributeTypePostDTO.Units.Count))
            {
                throw new ValidationException("Индекс не соотвествует массиву единиц измерений");
            }

            if (attributeTypePostDTO.Values.Any() && (0 > attributeTypePostDTO.DefaultValueIndex ||
                                                      attributeTypePostDTO.DefaultValueIndex >=
                                                      attributeTypePostDTO.Values.Count))
            {
                throw new ValidationException("Индекс не соотвествует массиву значений");
            }

            var attributeType = new AttributeType()
            {
                Name               = attributeTypePostDTO.Name,
                DataType           = (DAL.App.DTO.Enums.AttributeDataType)attributeTypePostDTO.DataType,
                DefaultCustomValue = attributeTypePostDTO.DefaultCustomValue,
                UsesDefinedUnits   = attributeTypePostDTO.UsesDefinedUnits,
                UsesDefinedValues  = attributeTypePostDTO.UsesDefinedValues,
                TypeUnits          = attributeTypePostDTO.Units.Select(s =>
                                                                       new AttributeTypeUnit {
                    Value = s
                }).ToList(),
                TypeValues = attributeTypePostDTO.Values.Select(s =>
                                                                new AttributeTypeValue {
                    Value = s
                }).ToList(),
            };

            var idCallBack = await UnitOfWork.AttributeTypes.AddAsync(attributeType);

            await UnitOfWork.SaveChangesAsync();

            var cleanAttributeType = await UnitOfWork.AttributeTypes.FirstOrDefaultNoTrackAsync(idCallBack());

            var hasValues = attributeTypePostDTO.UsesDefinedValues && attributeTypePostDTO.Values.Any();
            var hasUnits  = attributeTypePostDTO.UsesDefinedUnits && attributeTypePostDTO.Units.Any();

            AttributeType?typeWithValuesAndUnits = null;

            if (hasValues || hasUnits)
            {
                typeWithValuesAndUnits =
                    await UnitOfWork.AttributeTypes.GetWithValuesAndUnits(cleanAttributeType.Id);
            }

            if (hasValues && typeWithValuesAndUnits != null)
            {
                cleanAttributeType.DefaultValueId =
                    typeWithValuesAndUnits.TypeValues !.ToArray()[attributeTypePostDTO.DefaultValueIndex].Id;
            }

            if (hasUnits && typeWithValuesAndUnits != null)
            {
                cleanAttributeType.DefaultUnitId =
                    typeWithValuesAndUnits.TypeUnits !.ToArray()[attributeTypePostDTO.DefaultUnitIndex].Id;
            }

            if (hasValues || hasUnits)
            {
                await UnitOfWork.AttributeTypes.UpdateAsync(cleanAttributeType);

                await UnitOfWork.SaveChangesAsync();
            }

            return(cleanAttributeType.Id);
        }
Пример #2
0
        public async Task <ActionResult> Create(AttributeTypePostDTO attributeTypePostDTO)
        {
            var typeId = await _bll.AttributeTypes.CreateAsync(attributeTypePostDTO);

            return(CreatedAtAction(nameof(GetById), await GetById(typeId, 0, 0)));
        }