public async Task <int> Handle(UpsertHabitDifficultyCommand request, CancellationToken cancellationToken)
            {
                var habitVariation = await _context.HabitVariations.FindAsync(request.HabitVariationId);

                if (habitVariation == null)
                {
                    throw new NotFoundException(nameof(HabitVariation), request.HabitVariationId);
                }

                HabitDifficulty entity;

                if (request.Id != 0)
                {
                    entity = await _context.HabitDifficulties.FindAsync(request.Id);

                    await _mediator.Publish(new HabitDifficultyUpdated { HabitDifficultyId = entity.HabitDifficultyId }, cancellationToken);
                }
                else
                {
                    entity = new HabitDifficulty();
                    _context.HabitDifficulties.Add(entity);
                    await _mediator.Publish(new HabitDifficultyCreated { HabitDifficultyId = entity.HabitDifficultyId }, cancellationToken);
                }

                entity.HabitVariationId = request.HabitVariationId;
                entity.Description      = request.Description;
                entity.DifficultyLevel  = request.DifficultyLevel;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.HabitDifficultyId);
            }
            public async Task <int> Handle(UpsertHabitCommand request, CancellationToken cancellationToken)
            {
                Habit entity;

                if (request.Id != 0)
                {
                    entity = await _context.Habits.FirstOrDefaultAsync(h => h.HabitId == request.Id, cancellationToken);

                    await _mediator.Publish(new HabitUpdated { HabitId = entity.HabitId }, cancellationToken);
                }
                else
                {
                    entity = new Habit();
                    _context.Habits.Add(entity);
                    await _mediator.Publish(new HabitCreated { HabitId = entity.HabitId }, cancellationToken);
                }

                entity.Name        = request.Name;
                entity.Description = request.Description;
                entity.HabitType   = request.HabitType;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.HabitId);
            }
Exemplo n.º 3
0
            public async Task <int> Handle(UpsertHabitVariationCommand request, CancellationToken cancellationToken)
            {
                var habit = await _context.Habits.FindAsync(request.HabitId);

                if (habit == null)
                {
                    throw new NotFoundException(nameof(Habit), request.HabitId);
                }

                HabitVariation entity;

                if (request.Id != 0)
                {
                    entity = await _context.HabitVariations.FindAsync(request.Id);
                }
                else
                {
                    entity = new HabitVariation();

                    _context.HabitVariations.Add(entity);
                    await _mediator.Publish(new HabitVariationCreated { HabitVariationId = entity.HabitVariationId }, cancellationToken);
                }

                entity.HabitId          = request.HabitId;
                entity.HabitVariantName = request.Name;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.HabitVariationId);
            }
            public async Task <Unit> Handle(DeleteHabitVariationCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.HabitVariations.FirstOrDefaultAsync(hv => hv.HabitVariationId == request.Id, cancellationToken);

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

                _context.HabitVariations.Remove(entity);

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <int> Handle(UpsertHabitCompletionCommand request, CancellationToken cancellationToken)
            {
                var habitCompleted = await _context.Habits.FindAsync(request.HabitId);

                if (habitCompleted == null)
                {
                    throw new NotFoundException(nameof(Habit), request.HabitId);
                }

                var habitVariationCompleted = _context.HabitVariations.FindAsync(request.HabitVariationId);

                if (habitVariationCompleted == null)
                {
                    throw new NotFoundException(nameof(HabitVariation), request.HabitVariationId);
                }

                var habitDifficultyCompleted = _context.HabitDifficulties.FindAsync(request.HabitDifficultyId);

                if (habitDifficultyCompleted == null)
                {
                    throw new NotFoundException(nameof(HabitDifficulty), request.HabitDifficultyId);
                }

                HabitCompletion entity;

                if (request.Id > 0)
                {
                    entity = await _context.HabitCompletions.FindAsync(request.Id);

                    await _mediator.Publish(new HabitCompletionUpdated { HabitCompletionId = entity.HabitCompletionId }, cancellationToken);
                }
                else
                {
                    entity = new HabitCompletion();
                    _context.HabitCompletions.Add(entity);
                    await _mediator.Publish(new HabitCompletionCreated { HabitCompletionId = entity.HabitCompletionId }, cancellationToken);
                }

                entity.Date              = request.Date;
                entity.Habit             = habitCompleted;
                entity.HabitVariationId  = request.HabitVariationId;
                entity.HabitDifficultyId = request.HabitDifficultyId;

                await _context.SaveChangesAsync(cancellationToken);

                return(entity.HabitCompletionId);
            }