Exemplo n.º 1
0
        public async Task CreateDonation(
            string requestOrHospitalId,
            string userDonorId,
            double neededQuantity,
            double quantity,
            BloodGroup bloodGroup,
            RhesusFactor rhesusFactor)
        {
            var donorData = this.donorDataUserRepository.All()
                            .FirstOrDefault(ddu => ddu.ApplicationUserId == userDonorId);

            if (donorData == null)
            {
                throw new ArgumentException(GlobalConstants.NoDonorDataErrorMessage);
            }

            var request = this.requestRepository.All()
                          .FirstOrDefault(r => r.Id == requestOrHospitalId);

            var hospitalData = this.hospitalDataUserRepository.All()
                               .FirstOrDefault(hd =>
                                               request != null ?
                                               hd.Id == request.HospitalId : hd.Id == requestOrHospitalId);

            if (hospitalData == null)
            {
                throw new ArgumentException(GlobalConstants.NoHospitalDataErrorMessage);
            }

            var bloodBank = this.bloodBankRepository.All()
                            .FirstOrDefault(bbk => bbk.HospitalDataId == hospitalData.Id);

            var bag = this.bloodBagRepository.All()
                      .FirstOrDefault(bg => bg.BloodBankId == bloodBank.Id);

            var donationEvent = new DonationEvent
            {
                DateOfDonation = DateTime.UtcNow,
                RequestId      = request != null ? requestOrHospitalId : null,
                UserDonorId    = userDonorId,
            };

            await this.donationEventRepository.AddAsync(donationEvent);

            await this.donationEventRepository.SaveChangesAsync();

            bag = new BloodBag
            {
                Quantity       = quantity != 0 ? quantity : neededQuantity,
                CollectionDate = DateTime.UtcNow,
                DonorDataId    = donorData.Id,
                BloodGroup     = bloodGroup,
                RhesusFactor   = rhesusFactor,
                BloodBankId    = bloodBank.Id,
            };

            await this.bloodBagRepository.AddAsync(bag);

            await this.bloodBagRepository.SaveChangesAsync();
        }
        private static async Task SeedDataAsync(ApplicationDbContext dbContext)
        {
            // Seeding donationEvent
            var donationEvent = new DonationEvent
            {
                DateOfDonation = DateTime.UtcNow,
                RequestId      = "123",
                UserDonorId    = "456",
            };

            await dbContext.DonationEvents.AddAsync(donationEvent);

            // Seeding blood bag
            var bag = new BloodBag
            {
                Quantity       = 500,
                CollectionDate = DateTime.UtcNow,
                BloodGroup     = BloodGroup.A,
                RhesusFactor   = RhesusFactor.Negative,
            };


            await dbContext.BloodBags.AddAsync(bag);

            await dbContext.SaveChangesAsync();

            // Seeding user for donor
            dbContext.Users.Add(new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**",
            });

            // Seeding user for hospital
            dbContext.Users.Add(new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**",
            });

            await dbContext.SaveChangesAsync();
        }