コード例 #1
0
        public async Task <bool> Update(HelixEventViewModel requestItem)
        {
            HelixEvent                      helixEvent = null;
            ICollection <Product>           products   = null;
            ICollection <HelixEventProduct> eventProducts;

            try
            {
                helixEvent    = ModelConverter.ParseHelixEvent(requestItem);
                products      = ModelConverter.ParseProducts(requestItem.Products);
                eventProducts = ModelConverter.ParseEventProducts(requestItem.Id, requestItem.Products);
            }
            catch (Exception ex)
            {
                /** In real application,
                 * Encapsulate the details in the layer-specific log files for further troubleshooting
                 * and wrap it in user-friendly manner
                 **/
                throw ex;
            }

            try
            {
                /// Update Event
                bool isEventUpdateOk = await _helixEventDbService.Update(helixEvent);

                if (isEventUpdateOk)
                {
                    helixEvent = await _helixEventDbService.GetById(helixEvent.HelixEventId);

                    /// Update Products (if any new product pops up)
                    bool isProductsUpdateOk = await _productDbService.UpdateBatch(products);

                    if (isProductsUpdateOk)
                    {
                        /// Update the event-specific related product info
                        bool isLinkageUpdateOk = await _eventProductDbService.UpdateBatch(helixEvent.HelixEventId, eventProducts);

                        if (isLinkageUpdateOk)
                        {
                            helixEvent.ProductsLink = _eventProductDbService.GetAllByEventId(helixEvent.HelixEventId);

                            await _helixEventDbService.Update(helixEvent);

                            /// Only commit the changes if everything is updated successfully
                            await _dbContext.SaveChangesAsync();
                        }
                        else
                        {
                            throw new Exception("Updating Linkage failed!");
                        }
                    }
                    else
                    {
                        throw new Exception("Updating Products failed!");
                    }
                }
                else
                {
                    throw new Exception("Updating Events failed!");
                }
            }
            catch (Exception ex)
            {
                /** In real application,
                 * Encapsulate the details in the layer-specific log files for further troubleshooting
                 * and wrap it in user-friendly manner
                 **/
                throw ex;
            }

            return(true);
        }