Exemple #1
0
        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));
        }
Exemple #2
0
        public async Task <Unit> Handle(DeleteGUIDCommand request, CancellationToken cancellationToken)
        {
            //soft delete from database
            var entity = await _context.GUIDs.SingleAsync(c => c.Id == request.Id, cancellationToken);

            entity.IsDeleted = true;
            _context.Entry(entity).Property("IsDeleted").IsModified = true;
            await _context.SaveChangesAsync(cancellationToken);

            //delete cache
            var cacheKey = request.Id;
            await _distributedCache.RemoveAsync(cacheKey, cancellationToken);

            //return void
            return(Unit.Value);
        }
Exemple #3
0
        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));
        }