public async Task <GuidDTO> Handle(CreateGUIDCommand request, CancellationToken cancellationToken) { //create rich domain object var entity = new GuidMetadata(request.Id, request.Expire, request.User); //add to database and save try { _context.Add(entity); await _context.SaveChangesAsync(cancellationToken); } catch (System.Exception ex) { throw ex; } //create cache try { var cacheKey = entity.Id; var cacheMetadata = entity.Expire + "|" + entity.User; await _cache.SetStringAsync(cacheKey, cacheMetadata, cancellationToken); } catch (System.Exception ex) { throw ex; } //map to dto and return return(GuidDTO.Create(entity)); }
public void GuidWithHyphensIsValid() { string myGuid = "12345678-90ab-cdef-1234-567890abcdef"; long myExpire = 123; string myName = "John Doe"; GuidMetadata metadata = new GuidMetadata(myGuid, myExpire, myName); Assert.AreEqual(metadata.Id.ToString(), new Guid(metadata.Id).ToString("N").ToUpper()); }
public void LowerCaseGuidIsValid() { string myGuid = "1234567890abcdef1234567890abcdef"; long myExpire = 123; string myName = "John Doe"; GuidMetadata metadata = new GuidMetadata(myGuid, myExpire, myName); Assert.AreEqual(metadata.Id.ToString(), metadata.Id.ToString().ToUpper()); }
public void GuidIsNullCreatesRandomGuid() { string myGuid = null; long myExpire = 123; string myName = "John Doe"; GuidMetadata metadata = new GuidMetadata(myGuid, myExpire, myName); Assert.IsNotNull(Guid.Parse(metadata.Id.ToString())); }
public void ExpirationTimeProvidedIsApplied() { string myGuid = null; long? myExpire = 1547078400; string myName = "John Doe"; DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0); DateTime myDateTime = new DateTime(2019, 1, 1, 0, 0, 0); GuidMetadata metadata = new GuidMetadata(myGuid, myExpire, myName); Assert.AreEqual(unixStartTime.AddSeconds((double)metadata.Expire), new DateTime(2019, 1, 10, 0, 0, 0)); }
public void Simple() { if (Directory.Exists(Path)) { Directory.Delete(Path, true); } var first = new GuidKeyTestEntity { Data = "First" }; var second = new GuidKeyTestEntity { Data = "Second" }; var db = new GuidKeyTestDb(); using (db.BeginLock()) { db.GuidKeyTestDataset.Save(first); db.GuidKeyTestDataset.Save(second); } db = new GuidKeyTestDb(); // renew db var ids = db.GuidKeyTestDataset.Select(x => x.Id).ToArray(); Assert.AreEqual(2, ids.Length); Assert.AreNotEqual(first, db.GuidKeyTestDataset.First(x => x.Data == "First")); Assert.AreEqual(first.Id, db.GuidKeyTestDataset.First(x => x.Data == "First").Id); Assert.IsTrue(!String.IsNullOrWhiteSpace(first.Id) && first.Id != GuidMetadata.GetDefaultId() && first.Id != second.Id); Trace.TraceInformation("Default key: {0}", GuidMetadata.GetDefaultId()); Trace.TraceInformation("Generated keys:"); Trace.Indent(); foreach (var id in ids) { Trace.TraceInformation(id); } Trace.Unindent(); }
public async Task <GuidDTO> Handle(UpdateGUIDCommand request, CancellationToken cancellationToken) { //create rich domain object, bypass null or empty name validation var entity = new GuidMetadata(request.Id, request.Expire, request.User ?? "N/A", request.IsDeleted); //modify database and save changes if (request.Expire != null) { _context.Entry(entity).Property("Expire").IsModified = true; } if (request.User != null) { _context.Entry(entity).Property("User").IsModified = true; } if (request.IsDeleted != null) { _context.Entry(entity).Property("IsDeleted").IsModified = true; } await _context.SaveChangesAsync(cancellationToken); //get existing cache var cacheKey = request.Id; var cache = _distributedCache.GetString(cacheKey); //update existing cache string cacheMetadata; if (cache != null) { var data = cache.Split("|"); var expire = data[0].ToString(); var user = data[1].ToString(); cacheMetadata = string.Concat(entity.Expire != null ? entity.Expire.ToString() : expire, "|" , entity.User != null ? entity.User : user); await _distributedCache.SetStringAsync(cacheKey, cacheMetadata, cancellationToken); } //map to dto and return return(GuidDTO.Create(entity)); }
public async Task <GuidDTO> Handle(GetGUIDQuery request, CancellationToken cancellationToken) { GuidMetadata entity; var cacheKey = request.Id; var cacheMetadata = _distributedCache.GetString(cacheKey); if (!string.IsNullOrEmpty(cacheMetadata)) { //parse metadata and create rich domain object string[] metadata = cacheMetadata.Split('|'); entity = new GuidMetadata(request.Id, Convert.ToInt64(metadata[0]), metadata[1]); } else { //get entity from database try { entity = await _context.GUIDs.SingleAsync(c => c.Id == request.Id, cancellationToken); } catch (Exception ex) { throw ex; } //update cache cacheMetadata = entity.Expire + "|" + entity.User; _distributedCache.SetString(cacheKey, cacheMetadata); } //check expired DateTime dateTime = DateTime.UtcNow; DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime.ToLocalTime()); if (dateTimeOffset.ToUnixTimeSeconds() > entity.Expire) { throw new ExpiredException(); } return(GuidDTO.Create(entity)); }
public static GuidDTO Create(GuidMetadata metadata) { return(Projection.Compile().Invoke(metadata)); }