public void ShouldApplyUpdateVaccine()
        {
            var person    = new Person();
            var yesterday = DateTime.Now.Date.AddDays(-1);
            var today     = DateTime.Now.Date;

            person.Vaccines.Add(new Vaccine {
                Name = "Vaccine 1", Id = "1", AppliedAt = yesterday, CreatedAt = yesterday
            });
            person.Vaccines.Add(new Vaccine {
                Name = "Vaccine 2", Id = "2", AppliedAt = yesterday, CreatedAt = yesterday
            });
            person.Vaccines.Add(new Vaccine {
                Name = "Vaccine 3", Id = "3", AppliedAt = yesterday, CreatedAt = yesterday
            });

            var command = new UpdateVaccineCommand
            {
                Id        = "2",
                Name      = "Vaccine 2 edited",
                AppliedAt = today
            };

            person.Apply(command);

            var vaccine = person.Vaccines.ToArray()[1];

            vaccine.Name.Should().Be(command.Name);
            vaccine.AppliedAt.Should().Be(command.AppliedAt);
            vaccine.CreatedAt.ToShortDateString().Should().Be(yesterday.ToShortDateString());
            vaccine.UpdatedAt.Value.ToShortDateString().Should().Be(today.ToShortDateString());
        }
Exemple #2
0
        public async Task <IActionResult> Put(string id, [FromBody] UpdateVaccineCommand command)
        {
            Person person = await peopleRepository.GetAsync(id);

            if (person == null)
            {
                return(new NotFoundResult());
            }

            person.Apply(command);

            await peopleRepository.UpdateAsync(person);

            return(new OkObjectResult(person));
        }
        public void Apply(UpdateVaccineCommand command)
        {
            EnsureNotExistVaccine(command.Name, command.Id);

            var vaccine = Vaccines.FirstOrDefault(v => v.Id == command.Id);

            if (vaccine == null)
            {
                throw new VaccineNotFoundException("Vaccine not found!");
            }

            vaccine.Name      = command.Name;
            vaccine.AppliedAt = command.AppliedAt;
            vaccine.UpdatedAt = DateTime.Now.Date;
        }
        public void ShouldThrowExceptionWhenApplyUpdateAndExistsAnotherVaccineWithNameEquals()
        {
            var person = new Person();

            person.Vaccines.Add(new Vaccine {
                Name = "Vaccine 2 edited", Id = "1", AppliedAt = DateTime.Now.Date.AddDays(-1)
            });

            var command = new UpdateVaccineCommand
            {
                Id        = "2",
                Name      = "Vaccine 2 edited",
                AppliedAt = DateTime.Now.Date
            };

            Action apply = () => person.Apply(command);

            apply.Should().ThrowExactly <BusinessException>()
            .Which.Message.Equals("Already exists vaccine Vaccine 2 edited");
        }
        public void ShouldThrowExceptionWhenApplyUpdateVaccineIsNotFound()
        {
            var person = new Person();

            person.Vaccines.Add(new Vaccine {
                Name = "Vaccine 1", Id = "1", AppliedAt = DateTime.Now.Date.AddDays(-1)
            });

            var command = new UpdateVaccineCommand
            {
                Id        = "2",
                Name      = "Vaccine 2 edited",
                AppliedAt = DateTime.Now.Date
            };

            Action apply = () => person.Apply(command);

            apply.Should().ThrowExactly <BusinessException>()
            .Which.Message.Equals("Vaccine not found!");
        }