Example #1
0
        public async Task <Unit> Handle(UpdateTodoCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Todos.FindAsync(request.Id);

            entity.Content        = request.Content;
            entity.Email          = request.Email;
            entity.Date           = request.Date;
            entity.TimeZone       = request.TimeZone;
            entity.CronExpression = request.CronExpression;
            entity.ScheduleType   = request.ScheduleType;

            if (entity == null)
            {
                throw new NotFoundException(nameof(Todo), request.Id);
            }

            if (request.ScheduleType == ScheduleType.SpecificDate)
            {
                _backgroundWorkerService.Schedule <IEmailQueue>(job =>
                                                                job.Send(request.Email),
                                                                TimeZoneInfo.ConvertTimeFromUtc(request.Date.Value, TimeZoneInfo.FindSystemTimeZoneById(request.TimeZone)).TimeOfDay);
            }
            else
            {
                var cronExpression = request.ScheduleType switch
                {
                    ScheduleType.Daily => "0 12 * * *",
                    ScheduleType.Weekly => "0 12 * * 1",
                    ScheduleType.Monthly => "0 12 1 * *",
                    _ => request.CronExpression
                };

                _backgroundWorkerService.Cron <IEmailQueue>(job =>
                                                            job.Send(request.Email),
                                                            cronExpression,
                                                            TimeZoneInfo.FindSystemTimeZoneById(request.TimeZone));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
    }
Example #2
0
        public async Task <string> Handle(CreateTodoCommand request, CancellationToken cancellationToken)
        {
            var entity = new Todo
            {
                Id             = Guid.NewGuid().ToString(),
                Content        = request.Content,
                Email          = request.Email,
                Date           = request.Date,
                TimeZone       = request.TimeZone,
                CronExpression = request.CronExpression,
                ScheduleType   = request.ScheduleType
            };

            if (request.ScheduleType == ScheduleType.SpecificDate)
            {
                _backgroundWorkerService.Schedule <IEmailQueue>(job =>
                                                                job.Send(request.Email),
                                                                TimeZoneInfo.ConvertTimeFromUtc(request.Date.Value, TimeZoneInfo.FindSystemTimeZoneById(request.TimeZone)).TimeOfDay);
            }
            else
            {
                var cronExpression = request.ScheduleType switch
                {
                    ScheduleType.Daily => "0 12 * * *",
                    ScheduleType.Weekly => "0 12 * * 1",
                    ScheduleType.Monthly => "0 12 1 * *",
                    _ => request.CronExpression
                };

                _backgroundWorkerService.Cron <IEmailQueue>(job =>
                                                            job.Send(request.Email),
                                                            cronExpression,
                                                            TimeZoneInfo.FindSystemTimeZoneById(request.TimeZone));
            }

            _context.Todos.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
    }