Exemplo n.º 1
0
        public async Task <long> CreateAsync(ProductModifyRequest request)
        {
            var modifiedDate = DateTimeOffset.UtcNow;

            request.CreatedDate = modifiedDate;
            request.UpdatedDate = modifiedDate;
            var id = await _productRepository.CreateAsync(request);

            if (id <= 0)
            {
                return(-1);
            }

            if (request.Pictures.Any())
            {
                await _productPictureRepository.CreateAsync(new ProductPicturesModifyRequest
                {
                    CreatedById = request.CreatedById,
                    CreatedDate = request.CreatedDate,
                    ProductId   = id,
                    Pictures    = request.Pictures,
                    UpdatedById = request.UpdatedById,
                    UpdatedDate = request.UpdatedDate
                });
            }

            if (request.ProductAttributes == null || !request.ProductAttributes.Any())
            {
                return(id);
            }

            var groupOfAttributes = new List <ProductAttributeRelationRequest>();

            foreach (var attributeRelation in request.ProductAttributes)
            {
                var existAttribute = groupOfAttributes.FirstOrDefault(x => x.ProductAttributeId == attributeRelation.ProductAttributeId);
                if (existAttribute != null)
                {
                    var attributeValues = existAttribute.AttributeRelationValues.ToList();
                    attributeValues.AddRange(attributeRelation.AttributeRelationValues);
                    existAttribute.AttributeRelationValues = attributeValues;
                }
                else
                {
                    groupOfAttributes.Add(attributeRelation);
                }
            }

            foreach (var attributeRelation in groupOfAttributes)
            {
                attributeRelation.ProductId = id;
                await _productAttributeRepository.CreateAttributeRelationAsync(attributeRelation);
            }

            return(id);
        }