Blacker.MangaScraper.Library.DAL.StorageDAL.StoreChapterInfo C# (CSharp) Method

StoreChapterInfo() public method

public StoreChapterInfo ( DownloadedChapterInfo chapterInfo ) : bool
chapterInfo DownloadedChapterInfo
return bool
        public bool StoreChapterInfo(DownloadedChapterInfo chapterInfo)
        {
            if (chapterInfo == null)
                throw new ArgumentNullException("chapterInfo");

            if (chapterInfo.ChapterRecord == null)
                throw new ArgumentException("Chapter record is invalid.", "chapterInfo");

            if (String.IsNullOrEmpty(chapterInfo.ChapterRecord.ChapterId))
                throw new ArgumentException("Chapter record id is invalid.", "chapterInfo");

            if (chapterInfo.ChapterRecord.MangaRecord == null)
                throw new ArgumentException("Manga record is invalid.", "chapterInfo");

            if (String.IsNullOrEmpty(chapterInfo.ChapterRecord.MangaRecord.MangaId))
                throw new ArgumentException("Manga record id is invalid.", "chapterInfo");

            const string insertMangaSql = @"INSERT OR IGNORE
                                                INTO Mangas(
                                                        ScraperId,
                                                        MangaId,
                                                        MangaName,
                                                        Url)
                                                VALUES (
                                                        @ScraperId,
                                                        @MangaId,
                                                        @MangaName,
                                                        @Url)";

            const string insertChapterSql = @"INSERT OR REPLACE
                                                INTO Chapters(
                                                        ScraperId,
                                                        MangaId,
                                                        ChapterId,
                                                        ChapterName,
                                                        Url,
                                                        Downloaded,
                                                        Path,
                                                        DownloadFolder,
                                                        FormatProviderId)
                                                VALUES (
                                                        @ScraperId,
                                                        @MangaId,
                                                        @ChapterId,
                                                        @ChapterName,
                                                        @Url,
                                                        @Downloaded,
                                                        @Path,
                                                        @DownloadFolder,
                                                        @FormatProviderId);";

            int affectedRows;

            using (var connection = GetConnection())
            using (var transaction = connection.BeginTransaction())
            {
                using (var command = GetTextCommand(insertMangaSql))
                {
                    command.Parameters.AddWithValue("@ScraperId", chapterInfo.ChapterRecord.MangaRecord.Scraper);
                    command.Parameters.AddWithValue("@MangaId", chapterInfo.ChapterRecord.MangaRecord.MangaId);
                    command.Parameters.AddWithValue("@MangaName", chapterInfo.ChapterRecord.MangaRecord.MangaName);
                    command.Parameters.AddWithValue("@Url", chapterInfo.ChapterRecord.MangaRecord.Url);

                    ExecuteNonQuery(command, connection, transaction);
                }

                using (var command = GetTextCommand(insertChapterSql))
                {
                    command.Parameters.AddWithValue("@ScraperId", chapterInfo.ChapterRecord.MangaRecord.Scraper);
                    command.Parameters.AddWithValue("@MangaId", chapterInfo.ChapterRecord.MangaRecord.MangaId);
                    command.Parameters.AddWithValue("@ChapterId", chapterInfo.ChapterRecord.ChapterId);
                    command.Parameters.AddWithValue("@ChapterName", chapterInfo.ChapterRecord.ChapterName);
                    command.Parameters.AddWithValue("@Url", chapterInfo.ChapterRecord.Url);
                    command.Parameters.AddWithValue("@Downloaded", GetDBSafeDateTime(chapterInfo.Downloaded));
                    command.Parameters.AddWithValue("@Path", chapterInfo.Path);
                    command.Parameters.AddWithValue("@DownloadFolder", chapterInfo.DownloadFolder);
                    command.Parameters.AddWithValue("@FormatProviderId", chapterInfo.DownloadFormatProviderId);

                    affectedRows = ExecuteNonQuery(command, connection, transaction);
                }

                CommitTransaction(transaction);
            }

            return affectedRows == 1;
        }