コード例 #1
0
        private async Task <Anime> InsertAnimeAsync(long malId)
        {
            var existingAnime = await _animeRepository.GetAsync(malId);

            if (existingAnime != null)
            {
                return(existingAnime);
            }

            var parsedData = await _malApiService.GetAnimeDataAsync(malId);

            if (parsedData is null)
            {
                return(null);
            }

            var newAnime = new Anime
            {
                Id            = Guid.NewGuid(),
                MalId         = malId,
                Title         = parsedData.Title,
                ImageUrl      = parsedData.ImageUrl,
                About         = parsedData.About,
                AiringDate    = parsedData.AiringDate ?? DateTime.MinValue,
                EnglishTitle  = parsedData.EnglishTitle,
                KanjiTitle    = parsedData.JapaneseTitle,
                Popularity    = parsedData.Popularity,
                TitleSynonyms = !string.IsNullOrWhiteSpace(parsedData.TitleSynonyms) ? parsedData.TitleSynonyms : string.Empty,
                StatusId      = JikanParserHelper.GetUpdatedAnimeStatus(null, parsedData.Status),
                TypeId        = JikanParserHelper.GetUpdatedAnimeType(null, parsedData.Type),
                SeasonId      = string.IsNullOrEmpty(parsedData.Season)
                                ? await MatchSeasonByDateAsync(parsedData.AiringDate)
                                : await MatchSeasonBySeasonAsync(parsedData.Season)
            };

            await _animeRepository.AddAsync(newAnime);

            Console.WriteLine($"Inserted anime with malId {malId} ({parsedData.Title})");

            return(newAnime);
        }
コード例 #2
0
        public async Task <AnimeResponse> SaveAsync(Anime anime)
        {
            try
            {
                //Check if the category ID is valid before adding the anime, to avoid errors.
                var existingCategory = await _categoryRepository.FindByIdAsync(anime.CategoryId);

                if (existingCategory == null)
                {
                    return(new AnimeResponse("Invalid category."));
                }
                await _animeRepository.AddAsync(anime);

                await _unitOfWork.CompleteAsync();

                return(new AnimeResponse(anime));
            }
            catch (Exception ex)
            {
                // return error before injecting data into the database
                return(new AnimeResponse($"Sorry!. An error occurred when saving the category: {ex.Message}")); //using string literal to concatenate
            }
            //try
            //{
            //     //Check if the category ID is valid before adding the anime, to avoid errors.
            //    var existingCategory = await _categoryRepository.FindByIdAsync(anime.CategoryId);
            //    if (existingCategory == null)
            //        return new AnimeResponse("Invalid category.");

            //    await _animeRepository.AddAsync(anime);
            //    await _unitOfWork.CompleteAsync();

            //    return new AnimeResponse(anime);
            //}
            //catch (Exception ex)
            //{

            //    return new AnimeResponse($"An error occurred when saving the Anime: {ex.Message}");
            //}
        }