コード例 #1
0
        public async Task When_UserAsksForMovieTheMatrix_Then_AttemptsToReadCache_And_ReturnCorrectMovie()
        {
            movieDatabaseProxy
            .GetMovieOrTvSeries("movie", "the matri")
            .Returns(new Web.DTO.Media()
            {
                Id = "123", Name = "The Matrix"
            });

            // just executes the method
            memoryCacheAdapter
            .GetOrSetFromCache("movie_thematri", Arg.Any <Func <Task <Web.DTO.Media> > >())
            .Returns(callinfo => callinfo.ArgAt <Func <Task <Web.DTO.Media> > >(1).Invoke());

            var result = await sut.GetMovieOrTvSeries(Web.DTO.MediaType.movie, "the matri");

            result.Should().NotBeNull();
            result.Id.Should().Be("123");
            result.Name.Should().Be("The Matrix");
        }
コード例 #2
0
        public async Task <DTO.Media> GetMovieOrTvSeries(DTO.MediaType type, string name)
        {
            // could have used usual validators, but because this is simple, and in the spirit of no "magic code", I preferred to do manually
            if (name == null)
            {
                throw new BadRequestException("name is empty");
            }
            name = name.Trim();
            if (name.Length < 2)
            {
                throw new BadRequestException("name has too few characters");
            }

            string cacheKey = $"{type}_{name.ToLower().Replace(" ", "")}";

            var result = await _memoryCache.GetOrSetFromCache(cacheKey, async() =>
            {
                return(await _movieDatabaseProxy.GetMovieOrTvSeries(type.ToString(), name));
            });

            return(result);
        }