예제 #1
0
        public async Task <KeyValuePair <bool, string> > GainAchievement(int userId, GainedAchievementForCreationDto gainedAchievementForCreationDto)
        {
            var achievement = await _unitOfWork.Achievements.GetAchievementByValue(gainedAchievementForCreationDto.Value);

            if (achievement == null)
            {
                return(new KeyValuePair <bool, string>(false, "Not valid achievement!"));
            }

            var gainedAchievement = await _unitOfWork.Achievements.GetGainedAchievement(userId, gainedAchievementForCreationDto.Value);

            if (gainedAchievement != null)
            {
                return(new KeyValuePair <bool, string>(false, "Already have gained that achievement!"));
            }

            gainedAchievement = new GainedAchievement
            {
                UserId        = userId,
                User          = await _unitOfWork.Users.GetById(userId),
                DateAchieved  = DateTime.Now,
                Achievement   = achievement,
                AchievementId = achievement.Id
            };

            _unitOfWork.Users.GainAchievement(gainedAchievement);
            if (await _unitOfWork.Complete())
            {
                return(new KeyValuePair <bool, string>(true, "Achievement successfully gained!"));
            }

            return(new KeyValuePair <bool, string>(false, "Problem gaining achievement!"));
        }
예제 #2
0
        public async Task GetAllAchievements_ShouldReturnAllAchievementsForUser()
        {
            // Arrange
            var achievementId     = 1;
            var userId            = 1;
            var dateAchieved      = DateTime.Now;
            var gainedAchievement = new GainedAchievement()
            {
                Id            = 1,
                AchievementId = achievementId,
                DateAchieved  = dateAchieved,
                UserId        = userId
            };

            var gainedAchievementList = new List <GainedAchievement>();

            gainedAchievementList.Add(gainedAchievement);

            var gainedAchievementDto = new GainedAchievementToReturnDto()
            {
                DateAchieved = dateAchieved,
                Icon         = "test",
                Name         = "Test",
                Value        = 5
            };

            ICollection <GainedAchievementToReturnDto> gainedAchievementToReturnDtos =
                new List <GainedAchievementToReturnDto>();

            gainedAchievementToReturnDtos.Add(gainedAchievementDto);

            _unitOfWorkMock.Setup(x => x.Users.GetAllAchievementsForUser(It.IsAny <int>()))
            .ReturnsAsync(gainedAchievementList);

            _mapperMock.Setup(x =>
                              x.Map <ICollection <GainedAchievementToReturnDto> >
                                  (gainedAchievementList))
            .Returns(gainedAchievementToReturnDtos);

            // Act
            var response = await _sut.GetAllAchievementsForUser(userId);

            // Assert
            Assert.Equal(1, response.Count);
            Assert.Equal(dateAchieved, response.FirstOrDefault().DateAchieved);
            Assert.Equal(5, response.FirstOrDefault().Value);
        }
예제 #3
0
 public async void GainAchievement(GainedAchievement gainedAchievement)
 {
     await DataContext.GainedAchievements
     .AddAsync(gainedAchievement);
 }