public async Task SendMessage(ProductsPayloadDm payload)
        {
            string data = JsonConvert.SerializeObject(payload);
            Message message = new Message(Encoding.UTF8.GetBytes(data));

            await _queueClient.SendAsync(message);
        }
        public async Task <List <string> > Add(ProductsPayloadDm payloadDm)
        {
            bool isVendorPresent = await _context.Vendors.AnyAsync(x => x.Code == payloadDm.Vendor.Code);

            if (!isVendorPresent)
            {
                throw new ArgumentException("Invalid Vendor.");
            }

            var duplicateProdCodes = new List <string>();

            foreach (var product in payloadDm.Products)
            {
                bool isProductPresent = await _context.Products.AnyAsync(x => x.Code == product.Code);

                if (isProductPresent)
                {
                    duplicateProdCodes.Add(product.Code);
                }
                else
                {
                    var prodEm = _mapper.Map <Product>(product);
                    prodEm.CreatedOn = DateTime.UtcNow;
                    _context.Products.Add(prodEm);
                }
            }

            await _context.SaveChangesAsync();

            return(duplicateProdCodes);
        }
Esempio n. 3
0
        /// <summary>
        /// Implementaion with both local object and DB.
        /// </summary>
        /// <param name="payloadDm"></param>
        /// <returns></returns>
        public async Task Process(ProductsPayloadDm payloadDm)
        {
            //Implementation with local object.
            if (IsLocalObjectImplementation)
            {
                foreach (var product in payloadDm.Products)
                {
                    var prodEm = _mapper.Map <Product>(product);
                    prodEm.CreatedOn = DateTime.UtcNow;
                    Products.Add(prodEm);
                }

                return;
            }



            //Implementation with DB
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var _context = scope.ServiceProvider.GetService <DataContext>();

                bool isVendorPresent = await _context.Vendors.AnyAsync(x => x.Code == payloadDm.Vendor.Code);

                if (!isVendorPresent)
                {
                    throw new ArgumentException("Invalid Vendor.");
                }

                var duplicateProdCodes = new List <string>();
                foreach (var product in payloadDm.Products)
                {
                    bool isProductPresent = await _context.Products.AnyAsync(x => x.Code == product.Code);

                    if (isProductPresent)
                    {
                        duplicateProdCodes.Add(product.Code);
                    }
                    else
                    {
                        var prodEm = _mapper.Map <Product>(product);
                        prodEm.CreatedOn = DateTime.UtcNow;
                        _context.Products.Add(prodEm);
                    }
                }

                await _context.SaveChangesAsync();
            }
            //await _productRepository.Add(payloadDm);
        }
        public async Task <List <string> > ValidateProducts(ProductsPayloadDm payloadDm)
        {
            bool isVendorPresent = await _context.Vendors.AnyAsync(x => x.Code == payloadDm.Vendor.Code);

            if (!isVendorPresent)
            {
                throw new ArgumentException("Invalid Vendor.");
            }

            var duplicateProdCodes = new List <string>();

            foreach (var product in payloadDm.Products)
            {
                bool isProductPresent = await _context.Products.AnyAsync(x => x.Code == product.Code);

                if (isProductPresent)
                {
                    duplicateProdCodes.Add(product.Code);
                }
            }

            return(duplicateProdCodes);
        }
Esempio n. 5
0
        public async Task <IActionResult> Create([FromBody][Required] ProductsPayloadDm request)
        {
            await _serviceBusSender.SendMessage(request);

            return(Ok(request));
        }