Пример #1
0
        public ActionResult UpdateAssetSubscription([FromBody] UpdateAssetSubscriptionEvent assetSubscription)
        {
            try
            {
                if (!string.IsNullOrEmpty(assetSubscription.Source) &&
                    !Enum.TryParse(assetSubscription.Source, true, out SubscriptionSource _))
                {
                    logger.LogWarning("Invalid Source Value");
                    return(BadRequest("Invalid Source Value"));
                }

                assetSubscription.StartDate   = assetSubscription.StartDate.ToMySqlDateTimeOverflowCorrection();
                assetSubscription.EndDate     = assetSubscription.EndDate.ToMySqlDateTimeOverflowCorrection();
                assetSubscription.ReceivedUTC = DateTime.UtcNow;

                if (subscriptionService.UpdateAssetSubscription(assetSubscription))
                {
                    return(Ok());
                }

                logger.LogWarning("Update Asset Subscription Request Failed");
                return(BadRequest("Update Asset Subscription Request Failed"));
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Asset Subscription not exists!"))
                {
                    logger.LogWarning("Asset Subscription not exists!");
                    return(BadRequest("Asset Subscription not exists!"));
                }

                if (ex.Message.Contains("Invalid Asset Subscription Type"))
                {
                    logger.LogWarning("Invalid Asset Subscription Type");
                    return(BadRequest("Invalid Asset Subscription Type"));
                }

                logger.LogError(ex.Message + ex.StackTrace);
                return(StatusCode(500));
            }
        }
Пример #2
0
        public bool UpdateAssetSubscription(UpdateAssetSubscriptionEvent updateSubscription)
        {
            var dbAsset = GetExistingAssetSubscription(updateSubscription.SubscriptionUID.Value);

            if (dbAsset == null)
            {
                throw new Exception("Asset Subscription not exists!");
            }

            if (!_assetSubscriptionTypeCache.ContainsKey(updateSubscription.SubscriptionType))
            {
                throw new Exception("Invalid Asset Subscription Type for the SubscriptionUID:- " +
                                    updateSubscription.SubscriptionUID);
            }

            if (!FieldHelper.IsValidValuesFilled(updateSubscription, dbAsset, logger))
            {
                logger.LogError("Second Parameter expects typeOf IDbTable");
            }

            dbAsset.fk_ServiceTypeID = _assetSubscriptionTypeCache[updateSubscription.SubscriptionType];
            dbAsset.UpdateUTC        = DateTime.UtcNow;

            var kafkaMessage = new KafkaMessage()
            {
                Key     = updateSubscription.SubscriptionUID.ToString(),
                Message = new { UpdateAssetSubscriptionEvent = updateSubscription }
            };

            var actions = new List <Action>();

            actions.Add(() => transaction.Upsert <DbAssetSubscription>(dbAsset));
            actions.Add(() => topics.ToList().ForEach(topic =>
            {
                kafkaMessage.Topic = topic;
                transaction.Publish(kafkaMessage);
            }));

            return(transaction.Execute(actions));
        }