public static HelixEvent ParseHelixEvent(HelixEventViewModel requestItem)
        {
            HelixEvent eventItem = new HelixEvent();

            eventItem.HelixEventId = requestItem.Id;
            eventItem.Timestamp    = requestItem.Timestamp;

            return(eventItem);
        }
Exemple #2
0
        public async Task <IActionResult> PutProductsToEvent([FromBody] HelixEventViewModel eventRequest)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Update product against DB
                    await _eventService.Update(eventRequest);
                }
                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;

                    /*
                     * var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                     * {
                     *  Content = new StringContent(string.Format("Error: {0} {1} ", ex.Message, ex.StackTrace.ToString()) ),
                     *  ReasonPhrase = ex.Message + ex.StackTrace.ToString()
                     * };
                     * return resp;*/
                }

                // Do something else here in reality concurrently without waiting for products updates completed
                //await updateTask;

                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
        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);
        }