Exemplo n.º 1
0
        public virtual async Task <bool> Delete(int id)
        {
            using SchedulingContext _context = _contextFactory.CreateDbContext();
            _context.Set <T>().Remove(await _context.Set <T>().FindAsync(id));
            await _context.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 2
0
        public virtual async Task <T> Update(T item)
        {
            using SchedulingContext _context = _contextFactory.CreateDbContext();
            _context.Set <T>().Update(item);
            await _context.SaveChangesAsync();

            return(item);
        }
Exemplo n.º 3
0
        protected async Task <T> MakePersistent <T>(T entity) where T : Entity
        {
            var entry = Context.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                T foundEntity = Context.Set <T>().Find(entity.Id);
                if (foundEntity == null)
                {
                    entry.State = EntityState.Added;
                }
                else
                {
                    entry = Context.Entry(foundEntity);
                    entry.CurrentValues.SetValues(entity);
                    entry.State = EntityState.Modified;
                }
            }

            try
            {
                await Context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Exception occured! {0}", ex.Message);
                Logger.LogError(ex.StackTrace);
                var innerEx = ex.InnerException;
                while (innerEx != null)
                {
                    Logger.LogError("----------------------------------------");
                    Logger.LogError(innerEx, "Exception occured! {0}", innerEx.Message);
                    Logger.LogError(innerEx.StackTrace);
                    innerEx = innerEx.InnerException;
                }
                throw;
            }
            return(entry.Entity);
        }
Exemplo n.º 4
0
        public async Task <bool> AddOrUpdate(JobDto jobDto, CancellationToken ct = default)
        {
            var existingJobEntity = await context.Jobs
                                    .FirstOrDefaultAsync(j => j.SubscriptionName == jobDto.SubscriptionName && j.JobIdentifier == jobDto.JobIdentifier, ct);

            // Return false if there are no updates to be made
            if (existingJobEntity != null &&
                existingJobEntity.DomainName == jobDto.DomainName &&
                existingJobEntity.IsActive == jobDto.IsActive &&
                existingJobEntity.RepeatEndStrategyId == jobDto.RepeatEndStrategyId &&
                existingJobEntity.RepeatIntervalId == jobDto.RepeatIntervalId &&
                existingJobEntity.StartAt.IsEqualToTheMinute(jobDto.StartAt) &&
                existingJobEntity.EndAt.IsEqualToTheMinute(jobDto.EndAt) &&
                existingJobEntity.CronExpressionOverride == jobDto.CronExpressionOverride &&
                existingJobEntity.RepeatOccurrenceNumber == jobDto.RepeatOccurrenceNumber)
            {
                return(false);
            }

            var updatedJobEntity = Mapping.Mapper.Map <JobDto, Job>(jobDto);

            if (existingJobEntity != null)
            {
                updatedJobEntity.JobId = existingJobEntity.JobId;
                context.Entry(existingJobEntity).State = EntityState.Detached;
                context.Jobs.Update(updatedJobEntity);
            }
            else
            {
                await context.Jobs.AddAsync(updatedJobEntity, ct);
            }

            await context.SaveChangesAsync(ct);

            return(true);
        }
Exemplo n.º 5
0
        public async Task CreateRequestForCommandAsync <T>(Guid id)
        {
            var exists = await ExistAsync(id);

            var request = exists ?
                          throw new SchedulingDomainException($"Request with {id} already exists") :
                                new ClientRequest()
                                {
                                    Id   = id,
                                    Name = typeof(T).Name,
                                    Time = DateTime.UtcNow
                                };

            _context.Add(request);

            await _context.SaveChangesAsync();
        }
 public async Task Save()
 {
     await _schedulingContext.SaveChangesAsync();
 }