Пример #1
0
        public async Task Increment(CourseId id)
        {
            CoursesCounter counter = await repository.Search() ?? InitializeCounter();

            if (!counter.HasIncremented(id))
            {
                counter.Increment(id);

                await repository.Save(counter);
            }
        }
        public void it_should_not_increment_an_already_incremented_course()
        {
            CourseCreatedDomainEvent domainEvent = CourseCreatedDomainEventMother.Random();

            CourseId       courseId        = CourseIdMother.Create(domainEvent.AggregateId);
            CoursesCounter existingCounter = CoursesCounterMother.WithOne(courseId);

            ShouldSearch(existingCounter);

            this.Subscriber.On(domainEvent);
        }
Пример #3
0
        public async Task it_should_find_an_existing_courses_counter()
        {
            CoursesCounter          counter = CoursesCounterMother.Random();
            FindCoursesCounterQuery query   = new FindCoursesCounterQuery();

            CoursesCounterResponse response = CoursesCounterResponseMother.Create(counter.Total.Value);

            ShouldSearch(counter);

            Assert.Equal(response, await _handler.Handle(query));
        }
Пример #4
0
        public async Task Save(CoursesCounter counter)
        {
            CoursesCounterEfCoreModel courseCounter = new CoursesCounterEfCoreModel();

            courseCounter.Id              = counter.Id.Value;
            courseCounter.Total           = counter.Total.Value;
            courseCounter.ExistingCourses = counter.ExistingCourses;

            await this.Context.CoursesCounter.AddAsync(courseCounter);

            await this.Context.SaveChangesAsync();
        }
Пример #5
0
        public static CoursesCounter Incrementing(CoursesCounter existingCounter, CourseId courseId)
        {
            List <CourseId> existingCourses = new List <CourseId>(existingCounter.ExistingCourses);

            existingCourses.Add(courseId);

            return(Create(
                       existingCounter.Id,
                       CoursesCounterTotalMother.Create(existingCounter.Total.Value + 1),
                       existingCourses
                       ));
        }
Пример #6
0
        public async Task Save(CoursesCounter counter)
        {
            if (_context.Entry(counter).State == EntityState.Detached)
            {
                await _context.AddAsync(counter);
            }
            else
            {
                _context.Entry(counter).State = EntityState.Modified;
            }

            await _context.SaveChangesAsync();
        }
        public void it_should_increment_an_existing_counter()
        {
            CourseCreatedDomainEvent domainEvent = CourseCreatedDomainEventMother.Random();

            CourseId       courseId           = CourseIdMother.Create(domainEvent.AggregateId);
            CoursesCounter existingCounter    = CoursesCounterMother.Random();
            CoursesCounter incrementedCounter = CoursesCounterMother.Incrementing(existingCounter, courseId);

            ShouldSearch(existingCounter);

            this.Subscriber.On(domainEvent);

            ShouldHaveSaved(incrementedCounter);
        }
        public void it_should_initialize_a_new_counter()
        {
            CourseCreatedDomainEvent domainEvent = CourseCreatedDomainEventMother.Random();

            CourseId       courseId   = CourseIdMother.Create(domainEvent.AggregateId);
            CoursesCounter newCounter = CoursesCounterMother.WithOne(courseId);

            ShouldSearch();
            ShouldGenerateUuid(newCounter.Id.Value);

            this.Subscriber.On(domainEvent);

            this.ShouldHaveSaved(newCounter);
        }
 protected void ShouldSearch(CoursesCounter counter)
 {
     Repository.Setup(x => x.Search()).ReturnsAsync(counter);
 }
 protected void ShouldHaveSaved(CoursesCounter course)
 {
     Repository.Verify(x => x.Save(course), Times.AtLeastOnce());
 }
 private CoursesCounter InitializeCounter()
 {
     return(CoursesCounter.Initialize(_uuidGenerator.Generate()));
 }
        public async Task <CoursesCounterResponse> Find()
        {
            CoursesCounter coursesCounter = await this._repository.Search() ?? throw new CoursesCounterNotInitialized();

            return(new CoursesCounterResponse(coursesCounter.Total.Value));
        }
 private CoursesCounter InitializeCounter()
 {
     return(CoursesCounter.Initialize(new CoursesCounterId(this.UuidGenerator.Generate())));
 }
 public static CoursesCounterIncrementedDomainEvent FromCounter(CoursesCounter counter)
 {
     return(Create(counter.Id, counter.Total));
 }