示例#1
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 void ShouldMapHabitVariationToHabitCompletionVariationDto()
        {
            var entity = new HabitVariation();

            var result = _mapper.Map <HabitCompletionVariationDto>(entity);

            result.ShouldNotBeNull();
            result.ShouldBeOfType <HabitCompletionVariationDto>();
        }