From dc26f01753d5705c699f18e316197a23514baa57 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 25 Jul 2023 10:24:46 +0330 Subject: [PATCH] #400 /api/pdf/pdfbook/by/contributer/{authorId}/groupby/role --- RMuseum/Controllers/PDFLibraryController.cs | 21 ++++++++ .../PDFLibrary/ViewModels/AuthorRoleCount.cs | 18 +++++++ RMuseum/RMuseum.xml | 36 +++++++++++++ RMuseum/Services/IPDFLibraryService.cs | 8 +++ .../Implementation/PDFLibraryService.cs | 52 +++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 RMuseum/Models/PDFLibrary/ViewModels/AuthorRoleCount.cs diff --git a/RMuseum/Controllers/PDFLibraryController.cs b/RMuseum/Controllers/PDFLibraryController.cs index 2299caab..765d87ef 100644 --- a/RMuseum/Controllers/PDFLibraryController.cs +++ b/RMuseum/Controllers/PDFLibraryController.cs @@ -661,6 +661,27 @@ namespace RMuseum.Controllers return Ok(pdfBooksInfo.Result.Books); } + /// + /// get published pdf books by author stats (group by role) + /// + /// + /// + [HttpGet("pdfbook/by/contributer/{authorId}/groupby/role")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + + public async Task GetPublishedPDFBookbyAuthorGroupedByRoleAsync(int authorId) + { + var res = await _pdfService.GetPublishedPDFBookbyAuthorGroupedByRoleAsync(authorId); + if (!string.IsNullOrEmpty(res.ExceptionString)) + { + return BadRequest(res.ExceptionString); + } + + return Ok(res.Result); + } + /// /// add a new mukti volume pdf collection /// diff --git a/RMuseum/Models/PDFLibrary/ViewModels/AuthorRoleCount.cs b/RMuseum/Models/PDFLibrary/ViewModels/AuthorRoleCount.cs new file mode 100644 index 00000000..1cfbbe6a --- /dev/null +++ b/RMuseum/Models/PDFLibrary/ViewModels/AuthorRoleCount.cs @@ -0,0 +1,18 @@ +namespace RMuseum.Models.PDFLibrary.ViewModels +{ + /// + /// Author Role Count + /// + public class AuthorRoleCount + { + /// + /// Role + /// + public string Role { get; set; } + + /// + /// Counts + /// + public int Count { get; set; } + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 09228baa..31ae4619 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2499,6 +2499,13 @@ + + + get published pdf books by author stats (group by role) + + + + add a new mukti volume pdf collection @@ -12224,6 +12231,21 @@ Attributes + + + Author Role Count + + + + + Role + + + + + Counts + + New PDFBook view model @@ -17752,6 +17774,13 @@ + + + get published pdf books by author stats (group by role) + + + + add book @@ -18688,6 +18717,13 @@ + + + get published pdf books by author stats (group by role) + + + + add book diff --git a/RMuseum/Services/IPDFLibraryService.cs b/RMuseum/Services/IPDFLibraryService.cs index dabbf73c..70dd949c 100644 --- a/RMuseum/Services/IPDFLibraryService.cs +++ b/RMuseum/Services/IPDFLibraryService.cs @@ -131,6 +131,14 @@ namespace RMuseum.Services /// Task> GetPublishedPDFBooksByAuthorAsync(PagingParameterModel paging, int authorId, string role); + /// + /// get published pdf books by author stats (group by role) + /// + /// + /// + + Task> GetPublishedPDFBookbyAuthorGroupedByRoleAsync(int authorId); + /// /// add book /// diff --git a/RMuseum/Services/Implementation/PDFLibraryService.cs b/RMuseum/Services/Implementation/PDFLibraryService.cs index 2e19e6a6..a67f648c 100644 --- a/RMuseum/Services/Implementation/PDFLibraryService.cs +++ b/RMuseum/Services/Implementation/PDFLibraryService.cs @@ -789,6 +789,58 @@ namespace RMuseum.Services.Implementation } } + /// + /// get published pdf books by author stats (group by role) + /// + /// + /// + + public async Task> GetPublishedPDFBookbyAuthorGroupedByRoleAsync(int authorId) + { + try + { + var books = await _context.PDFBooks.AsNoTracking().Include(b => b.Contributers).ThenInclude(c => c.Author) + .Where(a => a.Status == PublishStatus.Published && a.Contributers.Any(a => a.Author.Id == authorId)) + .ToListAsync(); + Dictionary roleCount = new Dictionary(); + foreach (var book in books) + { + foreach (var contributer in book.Contributers) + { + if(contributer.Author.Id == authorId) + { + if (roleCount.ContainsKey(contributer.Role)) + { + roleCount[contributer.Role] = 1; + } + else + { + roleCount[contributer.Role]++; + } + } + } + } + + List authorRoles = new List(); + foreach(var role in roleCount.Keys) + { + authorRoles.Add + ( + new AuthorRoleCount() + { + Role = role, + Count = roleCount[role] + } + ); + } + return new RServiceResult(authorRoles.ToArray()); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// add book ///