コード例 #1
0
        public async Task <IActionResult> Edit(string id, AscentInputModel ascentInput)
        {
            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var valid  = this.ascentsService
                         .Exists(x => x.Id == id && x.ApplicationUserId == userId);

            if (!valid)
            {
                return(this.Forbid());
            }

            if (!this.ModelState.IsValid)
            {
                var ascent = new AscentEditViewModel()
                {
                    Id          = id,
                    AscentInput = ascentInput,
                };

                this.SetEditListItems(ascent.AscentInput);

                return(this.View(ascent));
            }

            await this.ascentsService.EditAsync(id, ascentInput);

            this.TempData[GlobalConstants.MessageKey] = "Successfully edited ascent!";

            return(this.RedirectToAction("Index", "Ascents", new { area = "Boulders" }));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(string id, AscentInputModel ascentInput)
        {
            var existsAscent = this.ascentsService
                               .Exists(x => x.Id == id);

            if (!existsAscent)
            {
                return(this.NotFound());
            }

            if (!this.ModelState.IsValid)
            {
                var ascent = new AscentEditViewModel()
                {
                    Id          = id,
                    AscentInput = ascentInput,
                };

                this.SetselectListItems(ascent.AscentInput);

                return(this.View(ascent));
            }

            await this.ascentsService.EditAsync(id, ascentInput);

            this.TempData[GlobalConstants.MessageKey] = $"Successfully edited ascent!";

            return(this.RedirectToAction("Index", "Ascents", new { area = "Boulders" }));
        }
コード例 #3
0
        private void SetCreateListItems(AscentInputModel ascentInput)
        {
            ascentInput.GradesSelectListItems = this.gradesService
                                                .GetMany <GradeViewModel>(orderBySelector: x => x.Text)
                                                .Select(x => new SelectListItem(x.Text, x.Id))
                                                .ToList();

            ascentInput.StylesSelectListItems = this.stylesService
                                                .GetMany <StyleViewModel>(orderBySelector: x => x.CreatedOn)
                                                .Select(x => new SelectListItem($"{x.LongText} ({x.ShortText})", x.Id))
                                                .ToList();
        }
コード例 #4
0
        public async Task AddAsync(AscentInputModel ascentInput, string userId)
        {
            this.NullCheck(ascentInput, nameof(ascentInput));
            this.NullCheck(userId, nameof(userId));

            var ascent = this.mapper.Map <Ascent>(ascentInput);

            ascent.ApplicationUserId = userId;

            await this.ascentsRepository.AddAsync(ascent);

            await this.ascentsRepository.SaveChangesAsync();

            await this.CalculatePointsForUser(userId);
        }
コード例 #5
0
        public async Task <IActionResult> Create(AscentInputModel ascentInput)
        {
            if (!this.ModelState.IsValid)
            {
                this.SetCreateListItems(ascentInput);
                return(this.View(ascentInput));
            }

            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            await this.ascentsService.AddAsync(ascentInput, userId);

            this.TempData[GlobalConstants.MessageKey] = "Successfully added ascent!";

            return(this.RedirectToAction("Index", "Ascents", new { area = "Boulders" }));
        }
コード例 #6
0
        public IActionResult Create(string id)
        {
            var existsBoulder = this.bouldersService
                                .Exists(x => x.Id == id);

            if (!existsBoulder)
            {
                return(this.NotFound());
            }

            var ascent = new AscentInputModel()
            {
                BoulderId = id,
            };

            this.SetCreateListItems(ascent);
            return(this.View(ascent));
        }
コード例 #7
0
        private void SetEditListItems(AscentInputModel ascent)
        {
            ascent.GradesSelectListItems = this.gradesService
                                           .GetMany <GradeViewModel>(orderBySelector: x => x.Text)
                                           .Select(x => new SelectListItem()
            {
                Value = x.Id,
                Text  = x.Text,
            })
                                           .ToList();

            ascent.StylesSelectListItems = this.stylesService
                                           .GetMany <StyleViewModel>()
                                           .Select(x => new SelectListItem()
            {
                Value = x.Id,
                Text  = x.LongText,
            })
                                           .ToList();
        }
コード例 #8
0
        public async Task EditAsync(string id, AscentInputModel ascentInput)
        {
            this.NullCheck(id, nameof(id));
            this.NullCheck(ascentInput, nameof(ascentInput));

            var ascent = this.ascentsRepository
                         .All()
                         .FirstOrDefault(x => x.Id == id);

            ascent.GradeId   = ascentInput.GradeId;
            ascent.Recommend = ascentInput.Recommend;
            ascent.Stars     = ascentInput.Stars;
            ascent.StyleId   = ascentInput.StyleId;
            ascent.Comment   = ascentInput.Comment;
            ascent.Date      = ascentInput.Date;

            await this.ascentsRepository.SaveChangesAsync();

            await this.CalculatePointsForUser(ascent.ApplicationUserId);
        }
コード例 #9
0
        public async Task AddAsyncAddsTheEntityAndSetsItsPoints(
            string userId,
            string boulderId,
            string styleId,
            string gradeId,
            int stars,
            string comment,
            bool recommend,
            int daysToSubtract,
            int weeklyPoints,
            int monthlyPoints,
            int yearlyPoints,
            int allTimePoints)
        {
            var timeSpan   = TimeSpan.FromDays(daysToSubtract);
            var ascentDate = DateTime.UtcNow.Subtract(timeSpan);

            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).Assembly);

            var ascentsRepoMock = new Mock <IDeletableEntityRepository <Ascent> >();
            var pointsRepoMock  = new Mock <IDeletableEntityRepository <Points> >();

            var saved = false;

            // To test if the ascent is actually added
            var realAscentsList = new List <Ascent>();

            // To test if points are calculated correctly
            var fakeAscentsList = new List <Ascent>()
            {
                new Ascent()
                {
                    ApplicationUserId = userId,

                    Grade = new Grade()
                    {
                        Points = 100,
                    },

                    Style = new Style()
                    {
                        BonusPoints = 50,
                    },

                    Date = ascentDate,
                },
            };

            var points = new Points()
            {
                ApplicationUserId = userId,
            };

            var pointsList = new List <Points>()
            {
                points,
            };

            var ascentInput = new AscentInputModel()
            {
                BoulderId = boulderId,
                Stars     = stars,
                StyleId   = styleId,
                Comment   = comment,
                GradeId   = gradeId,
                Recommend = recommend,
                Date      = ascentDate,
            };

            ascentsRepoMock.Setup(x => x.All())
            .Returns(realAscentsList.AsQueryable());

            ascentsRepoMock.Setup(x => x.AllAsNoTracking())
            .Returns(fakeAscentsList.AsQueryable());

            ascentsRepoMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            ascentsRepoMock.Setup(x => x.AddAsync(It.IsAny <Ascent>()))
            .Callback((Ascent ascent) =>
            {
                realAscentsList.Add(ascent);
            });

            pointsRepoMock.Setup(x => x.All())
            .Returns(pointsList.AsQueryable());

            var ascentsService = new AscentsService(ascentsRepoMock.Object, pointsRepoMock.Object, AutoMapperConfig.MapperInstance);

            // Act
            await ascentsService.AddAsync(ascentInput, userId);

            // Assert
            var ascent = realAscentsList[0];

            Assert.True(saved);
            Assert.Equal(boulderId, ascent.BoulderId);
            Assert.Equal(stars, ascent.Stars);
            Assert.Equal(styleId, ascent.StyleId);
            Assert.Equal(comment, ascent.Comment);
            Assert.Equal(gradeId, ascent.GradeId);
            Assert.Equal(recommend, ascent.Recommend);
            Assert.Equal(ascentDate, ascent.Date);

            Assert.Equal(weeklyPoints, points.Weekly);
            Assert.Equal(monthlyPoints, points.Monthly);
            Assert.Equal(yearlyPoints, points.Yearly);
            Assert.Equal(allTimePoints, points.AllTime);
        }