public ActionResult <RetWithGuid> Get(Guid guid)
        {
            NotificationADO notify = Dal.Get(guid);

            if (notify != null)
            {
                return(Ok(new RetWithGuid(notify)));
            }
            return(NotFound($"Object {guid.ToString()} not been stored "));
        }
Example #2
0
        public NotificationADO Get(Guid guid)
        {
            NotificationADO value = null;

            if (dictionary.TryGetValue(guid, out value))
            {
                return(value);
            }
            return(null);
        }
Example #3
0
        private bool TryCreateDir(NotificationADO notify, out string saveDir)
        {
            string base0 = Path.Combine(BaseStoreDirectory, notify.Type);

            if (!Directory.Exists(base0))
            {
                Directory.CreateDirectory(base0);
            }
            bool isTwitter = notify.IsTwitter;


            ref int idDir = ref _numDirFacebook;
Example #4
0
        /// <summary>
        /// Retuens error description or empty string;
        /// </summary>
        /// <param name="type"></param>
        /// <param name="body"></param>
        /// <param name="outNote"></param>
        /// <param name=""></param>
        /// <returns></returns>

        public string TryInsert(string type, object body, out NotificationADO outNote)
        {
            string ret = "";

            type    = ("" + type).Trim().ToLower();
            outNote = null;
            if (!type.StartsWith("twi") && !type.StartsWith("face"))
            {
                return($"Type {type} is not suitable");
            }


            Notification    note    = new Notification(type, body);
            NotificationADO newNote = new NotificationADO(note);

            //Get All with creation time bigger than Now - 60 sec
            NotificationADO[] lastValues = dictionary.Values.ToArray();

            for (int i = 0; i < lastValues.Length; i++)
            {
                var    p       = lastValues[i];
                var    dat0    = DateTime.Now;
                double delDSec = (dat0 - p.Created).TotalMilliseconds;
                if (newNote.Type == p.Type &&
                    delDSec < maxTimeToSec * 1000 &&
                    newNote.JMessage == p.JMessage)
                {
                    outNote = p;
                    var    secs = (int)(0.5 + delDSec / 1000.0);
                    string err  = $"Notify Retrieved:{outNote.FileName}: \nafter creation spawns {secs} sec < {maxTimeToSec}  :";
                    Log.LogWarning(err);
                    Log.LogInformation($"{outNote.JMessage}");

                    return(err);
                }
            }

            dictionary.TryAdd(newNote.Uid, newNote);
            outNote = newNote;
            if (StoreNotification(newNote) == null)
            {
                return(String.Empty);
            }

            return("Error to store");
        }
Example #5
0
        private Exception StoreNotification(NotificationADO notify)
        {
            string saveDir = null;

            try
            {
                lock (_lockTryCreateDir)
                {
                    if (!TryCreateDir(notify, out saveDir))
                    {
                        throw new ApplicationException($"TryCreateDir({notify.JMessage} failed");
                    }
                }
                notify.FileName = Path.Combine(saveDir, notify.RandomString + ".notification.json");

                File.WriteAllText(notify.FileName, notify.JMessage);
                Log.LogInformation($"Notify Stored:{notify.FileName} stored :");
                Log.LogInformation($"{notify.JMessage}");

                var notificationSummary = new NotificationSummary(notify);
                var fileName            = notify.FileName.Replace(".json", ".summary.json");
                File.WriteAllText(fileName, notificationSummary.ToString());
            }
            catch (Exception ex)
            {
                Log.LogError($"Error{ex.StackTrace}");
                //Rewind Directory
                if (!string.IsNullOrWhiteSpace(saveDir) && Directory.Exists(saveDir))
                {
                    try
                    {
                        Directory.Delete(saveDir, true);
                    }
                    catch (Exception)
                    {
                        //throw;
                    }
                }
                return(ex);
            }
            return(null);
        }
        public ActionResult <RetWithGuid> Insert(string type, [FromBody] object body)
        {
            //   lock (_lockInsert)
            // {
            NotificationADO notify = null;

            try
            {
                string err = Dal.TryInsert(type, body, out notify);
                if (string.IsNullOrEmpty(err))
                {
                    return(Ok(new RetWithGuid(notify)));
                }
                return(NotFound(err));
                //new RetWithGuid() { guid = Guid.Empty, body = body });
            }
            catch (Exception ex)
            {
                Log.LogError(ex.StackTrace);
                return(this.NotFound($"Error occures {ex.StackTrace}"));
            }
            //   }
        }
 public RetWithGuid(NotificationADO note)
 {
     guid = note.Uid;
     body = note.GetBody();
 }