Beispiel #1
0
        public async Task <IActionResult> BecomeDonor(DonorsCreateInputModel donorsCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                var cities     = this.cityService.AllCities().Result;
                var inputModel = new DonorsCreateInputModel
                {
                    Cities = cities,
                };

                return(this.View(inputModel));
            }

            var user = await this.userManager.GetUserAsync(this.HttpContext.User);

            var model = new DonorServiceModel()
            {
                FullName  = donorsCreateInputModel.FullName,
                Age       = donorsCreateInputModel.Age,
                BloodType = new BloodTypeServiceModel
                {
                    ABOGroupName = donorsCreateInputModel.BloodType.ABOGroupName,
                    RhesusFactor = donorsCreateInputModel.BloodType.RhesusFactor,
                },
                UserId = user.Id,
                CityId = donorsCreateInputModel.CityId,
            };

            await this.donorService.CreateAsync(model);

            return(this.Redirect("/"));
        }
Beispiel #2
0
        public async Task CreateAsync_WithIncorectData_ShouldThrowException(string fullName, int age, string aboGroup, string rhesusFactor, int city)
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var seeder  = new Seeder();
            await seeder.SeedUsersAsync(context);

            await seeder.SeedBloodTypesAsync(context);

            await seeder.SeedCities(context);

            var userManager  = this.GetUserManagerMock(context);
            var donorService = new DonorService(context, userManager.Object);

            var donorServiceModel = new DonorServiceModel
            {
                FullName  = fullName,
                Age       = age,
                BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel
                {
                    ABOGroupName = aboGroup,
                    RhesusFactor = rhesusFactor,
                },
                UserId = context.Users.First().Id,
                CityId = city,
            };

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await donorService.CreateAsync(donorServiceModel);
            });
        }
        public async Task <bool> CreateAsync(DonorServiceModel donorServiceModel)
        {
            if (string.IsNullOrWhiteSpace(donorServiceModel.FullName) ||
                string.IsNullOrWhiteSpace(donorServiceModel.UserId) ||
                donorServiceModel.BloodType == null)
            {
                throw new ArgumentNullException();
            }

            var user = await this.context.Users.SingleOrDefaultAsync(x => x.Id == donorServiceModel.UserId);

            var aboGroup     = Enum.Parse <ABOGroup>(donorServiceModel.BloodType.ABOGroupName);
            var rhesusFactor = Enum.Parse <RhesusFactor>(donorServiceModel.BloodType.RhesusFactor);

            var bloodType = await this.context.BloodTypes.SingleOrDefaultAsync(x => x.ABOGroupName == aboGroup && x.RhesusFactor == rhesusFactor);

            var donor = new Donor()
            {
                FullName    = donorServiceModel.FullName,
                Age         = donorServiceModel.Age,
                BloodType   = bloodType,
                BloodTypeId = bloodType.Id,
                User        = user,
                UserId      = user.Id,
                CityId      = donorServiceModel.CityId,
            };

            await this.userManager.AddToRoleAsync(user, GlobalConstants.DonorRoleName);

            await this.context.Donors.AddAsync(donor);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <DonorServiceModel> GetByIdAsync(string donorId)
        {
            var donor = await this.context.Donors.Include(x => x.User).SingleOrDefaultAsync(x => x.Id == donorId);

            var model = new DonorServiceModel
            {
                Id          = donor.Id,
                FullName    = donor.FullName,
                Age         = donor.Age,
                BloodTypeId = donor.BloodTypeId,
                CityId      = donor.CityId,
                UserId      = donor.UserId,
                User        = new UserServiceModel
                {
                    Email = donor.User.Email,
                },
            };

            return(model);
        }
Beispiel #5
0
        public async Task CreateAsync_WithCorectData_ShouldReturnCorectResult(string fullName, int age, string aboGroup, string rhesusFactor, int city)
        {
            var errorMessage = "DonorService CreateAsync method does not return properly ";

            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var seeder  = new Seeder();
            await seeder.SeedUsersAsync(context);

            await seeder.SeedBloodTypesAsync(context);

            await seeder.SeedCities(context);

            var userManager  = this.GetUserManagerMock(context);
            var donorService = new DonorService(context, userManager.Object);

            var donorServiceModel = new DonorServiceModel
            {
                FullName  = fullName,
                Age       = age,
                BloodType = new BloodDonationSystem.Services.Models.BloodTypeServiceModel
                {
                    ABOGroupName = aboGroup,
                    RhesusFactor = rhesusFactor,
                },
                UserId = context.Users.First().Id,
                CityId = city,
            };

            var result = await donorService.CreateAsync(donorServiceModel);

            var actualResult   = context.Donors.FirstOrDefault();
            var expectedResult = donorServiceModel;

            Assert.True(result);
            Assert.True(actualResult.FullName == expectedResult.FullName, errorMessage + "FullName");
            Assert.True(actualResult.Age == expectedResult.Age, errorMessage + "Age");
            Assert.True(actualResult.BloodType.ABOGroupName.ToString() == expectedResult.BloodType.ABOGroupName.ToString(), errorMessage + "ABOGroup");
            Assert.True(actualResult.BloodType.RhesusFactor.ToString() == expectedResult.BloodType.RhesusFactor.ToString(), errorMessage + "RhesusFactor");
            Assert.True(actualResult.UserId == expectedResult.UserId, errorMessage + "UserId");
            Assert.True(actualResult.CityId == expectedResult.CityId, errorMessage + "CityId");
        }
        public async Task <DonorServiceModel> GetByUserIdAsync(string userId)
        {
            var donor = await this.context.Donors.SingleOrDefaultAsync(x => x.UserId == userId);

            if (donor == null)
            {
                throw new InvalidOperationException();
            }

            var model = new DonorServiceModel
            {
                Id          = donor.Id,
                FullName    = donor.FullName,
                Age         = donor.Age,
                BloodTypeId = donor.BloodTypeId,
                CityId      = donor.CityId,
                UserId      = donor.UserId,
            };

            return(model);
        }