public async Task <ActionResult <GUIDData> > GetById(string guid) { if (string.IsNullOrWhiteSpace(guid)) { return(NotFound()); } GUIDData guidObj = null; // check if the guid is available in cache var cacheData = CacheHelper.Get(guid, typeof(GUIDData)); if (cacheData != null) { guidObj = (GUIDData)cacheData; } else { // find guid metada from the db guidObj = guidDataContext.GUIDs.Find(guid); if (guidObj != null) { // if found, add the metadata to cache CacheHelper.Set(guidObj.guid, guidObj); } } // if the expire time in the found guid objet is already in the past, the data is deleted from the db and the cache. null value is retuned. if (guidObj != null && guidObj.expire < new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) { await this.DeleteGUIDData(guidObj.guid); guidObj = null; } return(guidObj ?? (ActionResult <GUIDData>)NotFound()); }
public async void TestGet() { var validExpiry = new DateTimeOffset(DateTime.Now.AddDays(100)).ToUnixTimeSeconds(); // create a new guid string guid = Guid.NewGuid().ToString(); // save the new guid to the api's store await TryPost(guid, new { guid = guid, expire = validExpiry, user = "******" }); //HttpContent contentPost = new StringContent(paramString, Encoding.UTF8, "application/json"); guid = guid ?? ""; GUIDData guidObj = new GUIDData(guid); guidObj.expire = validExpiry; guidObj.user = "******"; // attempt to Get the same guid var response = await _client.GetAsync($"http://localhost:5002/api/guid/{guid}"); // test the GET API's response Assert.Equal(HttpStatusCode.OK, response.StatusCode); var jsonResponse = await response.Content.ReadAsStringAsync(); var responseObj = JsonConvert.DeserializeObject <GUID.Models.GUIDData>(jsonResponse); // test for guid values Assert.Equal(guid, responseObj.guid); }
public void AddGUID(Int32 id, Guid data) { // wait for our mutex to clear before we continue. while (!storagemutex) { Thread.Sleep(1); } // Claim our mutex. storagemutex = false; var item = new GUIDData() { ID = id, GUID = data, TimeStamp = DateTime.UtcNow }; storage.Add(item); // Release our mutex. storagemutex = true; }
public async Task <ActionResult <GUIDData> > PostGUIDData(string guid, [FromBody] GUIDData guidObj) { // create a new guid if its not already passed as input guid = guid ?? (guidObj != null && !string.IsNullOrWhiteSpace(guidObj.guid) ? guidObj.guid : Guid.NewGuid().ToString()); Guid hxGuid; // error response for an invalid guid if (string.IsNullOrWhiteSpace(guid) || !Guid.TryParse(guid, out hxGuid)) { return(StatusCode((int)HttpStatusCode.BadRequest, "Not a valid Guid.")); } // error response for a guid mismatch between path input vs post data if (guidObj != null && !string.IsNullOrWhiteSpace(guidObj.guid) && guid != guidObj.guid) { return(StatusCode((int)HttpStatusCode.Conflict, "Conflicting Input.")); } guidObj = guidObj ?? new GUIDData(guid); guidObj.guid = guid; // look for existing data with the guid passed as input GUIDData existingGuidObj = (GUIDData)CacheHelper.Get(guid, typeof(GUIDData)); if (existingGuidObj == null) { existingGuidObj = guidDataContext.GUIDs.Find(guid); } // if there id no existing guid, this is treated as a Create request if (existingGuidObj == null) { // If no expire times is passed in input, set it to 30 days from now if (guidObj.expire == 0) { guidObj.expire = new DateTimeOffset(DateTime.Now.AddDays(30)).ToUnixTimeSeconds(); } // if the input expire time is already in the past, no data is created and a null will be returned if (guidObj.expire > new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) { guidDataContext.GUIDs.Add(guidObj); existingGuidObj = guidObj; } } // if the guid already exists, this is treated as an Update reuest else { // assign the new expire time (if present) from input to the existing metadata object if (guidObj.expire != 0) { existingGuidObj.expire = guidObj.expire; } // if the expire time set in the updated guid object is already in the past, the object is deleted and a null will be returned if (existingGuidObj.expire <= new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) { await this.DeleteGUIDData(existingGuidObj.guid); existingGuidObj = null; } else { // assign the new user name from input to the existing metadata object if (!string.IsNullOrWhiteSpace(guidObj.user)) { existingGuidObj.user = guidObj.user; } guidDataContext.GUIDs.Update(existingGuidObj); } } // for both Create and Update requests, 'existingGuidObj' will be null if the request was invalid or the expiry time is already in the past if (existingGuidObj != null) { //var SaveTask = guidDataContext.SaveChangesAsync(); guidDataContext.SaveChanges(); // Asycn call commented - was causing concurrency issues with postgres // update the cache while the data is being persisted to db CacheHelper.Set(existingGuidObj.guid, existingGuidObj); //await SaveTask; } return(existingGuidObj); }