Пример #1
0
        /** DELETE ASYNC */
        public async Task <bool> DeleteAsync(
            ProductTierTransferReport productTierTransferReport)
        {
            var productTier1 = await _context.ProductTiers
                               .SingleOrDefaultAsync(x => x.Id == productTierTransferReport.FromTierId);

            var productTier2 = await _context.ProductTiers
                               .SingleOrDefaultAsync(x => x.Id == productTierTransferReport.ToTierId);

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    /** Reverse transfer quantity back */
                    productTier1.Quantity = productTier1.Quantity + productTierTransferReport.Quantity;
                    productTier2.Quantity = productTier2.Quantity - productTierTransferReport.Quantity;
                    _context.ProductTiers.UpdateRange(
                        new List <ProductTier>()
                    {
                        productTier1,
                        productTier2
                    }
                        );
                    var productTierUpdated = await _context.SaveChangesAsync();

                    if (!(productTierUpdated > 0))
                    {
                        return(false);
                    }

                    /** Delete product tier transfer report */
                    _context.ProductTierTransferReports.Remove(productTierTransferReport);
                    var deleted = await _context.SaveChangesAsync();

                    if (!(deleted > 0))
                    {
                        return(false);
                    }

                    await transaction.CommitAsync();
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        private async Task PrepareProductTierTransferReportInfo(
            ProductTierTransferReport productTierTransferReport,
            int requestedUserId
            )
        {
            var storageManager = await _context.StorageManagers
                                 .SingleOrDefaultAsync(sm => sm.UserId == requestedUserId);

            productTierTransferReport.CreatedAt = DateTime.UtcNow;
            productTierTransferReport.CreatedBy = storageManager.Id;
            productTierTransferReport.IsDeleted = false;
            // rounded transfer quantity to 2 decimal place to avoid
            // awkward quantity number
            productTierTransferReport.Quantity = Math.Round(
                productTierTransferReport.Quantity,
                2,
                MidpointRounding.AwayFromZero);
        }
Пример #3
0
        /** CREATE ASYNC */
        public async Task <CreateResult <ProductTierTransferReport> > CreateAsync(
            ProductTierTransferReport productTierTransferReport,
            int requestedUserId)
        {
            try
            {
                await PrepareProductTierTransferReportInfo(
                    productTierTransferReport,
                    requestedUserId);

                /** Get both source (fromTierId) and destination (toTierId)
                 * from db to validate */
                var productTier1 = await _context.ProductTiers
                                   .Where(pt => pt.Id == productTierTransferReport.FromTierId)
                                   .Include(pt => pt.Tier)
                                   .FirstOrDefaultAsync();

                var productTier2 = await _context.ProductTiers
                                   .Where(pt => pt.Id == productTierTransferReport.ToTierId)
                                   .Include(pt => pt.Tier)
                                   .FirstOrDefaultAsync();

                /** Validate both source and destination product tier */
                AreSourceAndDestinationProductTierValid(
                    productTier1,
                    productTier2,
                    productTierTransferReport.Quantity);

                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        /** Update product tier quantity */
                        bool isUpdateProductTiersQuantitySuccess = await UpdateSourceAndDestinationProductTiersQuantity(
                            productTier1,
                            productTier2,
                            productTierTransferReport.Quantity
                            );

                        if (isUpdateProductTiersQuantitySuccess == false)
                        {
                            transaction.Dispose();
                            throw new Exception("Chuyển số lượng sản từ loại 1 sang loại 2 có lỗi, xin thử lại!");
                        }

                        /** Create product tier transfer report */
                        await _context.ProductTierTransferReports.AddAsync(productTierTransferReport);

                        var created = await _context.SaveChangesAsync();

                        if (!(created > 0))
                        {
                            transaction.Dispose();
                            throw new Exception("Chuyển số lượng sản từ loại 1 sang loại 2 có lỗi, xin thử lại!");
                        }

                        await transaction.CommitAsync();
                    }
                    catch (Exception e)
                    {
                        return(new CreateResult <ProductTierTransferReport>
                        {
                            IsSuccess = false,
                            Errors = new List <string>()
                            {
                                e.Message.ToString()
                            }
                        });
                    }
                }
            }
            catch (Exception e)
            {
                return(new CreateResult <ProductTierTransferReport>
                {
                    IsSuccess = false,
                    Errors = new List <string>()
                    {
                        e.Message.ToString()
                    }
                });
            }

            return(new CreateResult <ProductTierTransferReport>
            {
                IsSuccess = true,
                EntityReturn = productTierTransferReport
            });
        }