Exemplo n.º 1
0
 public static ChapterContentView Map(this ChapterContentModel source)
 => new ChapterContentView
 {
     Id            = source.Id,
     ChapterId     = source.ChapterId,
     BookId        = source.BookId,
     ChapterNumber = source.ChapterNumber,
     Language      = source.Language,
     Text          = source.Text
 };
Exemplo n.º 2
0
        public async Task <ChapterContentModel> AddChapterContent(int libraryId, ChapterContentModel content, CancellationToken cancellationToken)
        {
            using (var connection = _connectionProvider.GetConnection())
            {
                var sql     = @"Insert Into ChapterContent (ChapterId, Language, Text)
                            Values (@ChapterId, @Language, @Text)";
                var command = new CommandDefinition(sql, new { ChapterId = content.ChapterId, Language = content.Language, Text = content.Text }, cancellationToken: cancellationToken);
                await connection.ExecuteAsync(command);

                return(await GetChapterContent(libraryId, content.BookId, content.ChapterNumber, content.Language, cancellationToken));
            }
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <ChapterModel> > GetChaptersByBook(int libraryId, int bookId, CancellationToken cancellationToken)
        {
            using (var connection = _connectionProvider.GetConnection())
            {
                var chapters = new Dictionary <int, ChapterModel>();

                var sql     = @"Select c.*, cc.Id as ContentId, cc.Language As ContentLanguage,
                            a.Name As WriterAccountName, ar.Name As ReviewerAccountName
                            From Chapter c
                            Inner Join Book b On b.Id = c.BookId
                            Left Outer Join ChapterContent cc On c.Id = cc.ChapterId
                            LEFT OUTER JOIN [Accounts] a ON a.Id = c.WriterAccountId
                            LEFT OUTER JOIN [Accounts] ar ON ar.Id = c.ReviewerAccountId
                            Where b.Id = @BookId AND b.LibraryId = @LibraryId
                            Order By c.ChapterNumber";
                var command = new CommandDefinition(sql, new { LibraryId = libraryId, BookId = bookId }, cancellationToken: cancellationToken);
                await connection.QueryAsync <ChapterModel, int?, string, string, string, ChapterModel>(command, (c, contentId, contentLangugage, WriterAccountName, ReviewerAccountName) =>
                {
                    if (!chapters.TryGetValue(c.Id, out ChapterModel chapter))
                    {
                        c.WriterAccountName   = WriterAccountName;
                        c.ReviewerAccountName = ReviewerAccountName;

                        chapters.Add(c.Id, chapter = c);
                    }

                    chapter = chapters[c.Id];
                    if (contentId != null)
                    {
                        var content = chapter.Contents.SingleOrDefault(x => x.Id == contentId);
                        if (content == null)
                        {
                            ChapterContentModel cc = new ChapterContentModel();
                            cc.BookId        = bookId;
                            cc.ChapterId     = c.Id;
                            cc.ChapterNumber = c.ChapterNumber;
                            cc.Id            = contentId.Value;
                            cc.Language      = contentLangugage;
                            chapter.Contents.Add(cc);
                        }
                    }

                    return(chapter);
                }, splitOn : "ContentId,ContentLanguage,WriterAccountName,ReviewerAccountName");

                return(chapters.Values);
            }
        }
Exemplo n.º 4
0
        private ChapterContentView Render(ChapterContentModel source, int libraryId, bool addText = true)
        {
            var result = source.Map();

            if (!addText)
            {
                result.Text = null;
            }

            var links = new List <LinkView>
            {
                _linkRenderer.Render(new Link
                {
                    ActionName = nameof(ChapterController.GetChapterContent),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Self,
                    Language   = source.Language,
                    Parameters = new { libraryId = libraryId, bookId = source.BookId, chapterNumber = source.ChapterNumber }
                }),
                _linkRenderer.Render(new Link
                {
                    ActionName = nameof(ChapterController.GetChapterById),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Chapter,
                    Parameters = new { libraryId = libraryId, bookId = source.BookId, chapterNumber = source.ChapterNumber }
                }),
                _linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.GetBookById),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Book,
                    Parameters = new { libraryId = libraryId, bookId = source.BookId }
                })
            };

            links.Add(_linkRenderer.Render(new Link
            {
                ActionName = nameof(ChapterController.GetChapterContent),
                Method     = HttpMethod.Get,
                Rel        = RelTypes.Content,
                Language   = source.Language,
                Parameters = new { libraryId = libraryId, bookId = source.BookId, chapterNumber = source.ChapterNumber }
            }));

            if (_userHelper.IsWriter(libraryId) || _userHelper.IsAdmin || _userHelper.IsLibraryAdmin(libraryId))
            {
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(ChapterController.UpdateChapterContent),

                    Method     = HttpMethod.Put,
                    Rel        = RelTypes.Update,
                    Language   = source.Language,
                    Parameters = new
                    {
                        libraryId     = libraryId,
                        bookId        = source.BookId,
                        chapterNumber = source.ChapterNumber
                    }
                }));

                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(ChapterController.DeleteChapterContent),
                    Method     = HttpMethod.Delete,
                    Rel        = RelTypes.Delete,
                    Language   = source.Language,
                    Parameters = new { libraryId = libraryId, bookId = source.BookId, chapterNumber = source.ChapterNumber }
                }));
            }

            result.Links = links;
            return(result);
        }
Exemplo n.º 5
0
 public ChapterContentView Render(ChapterContentModel source, int libraryId)
 {
     return(Render(source, libraryId, true));
 }