|
|
|
@ -1,12 +1,15 @@
|
|
|
|
|
using BukiVedi.App.Requests;
|
|
|
|
|
using BukiVedi.App.Responces;
|
|
|
|
|
using BukiVedi.App.Services.Mappers;
|
|
|
|
|
using BukiVedi.Shared.Entities;
|
|
|
|
|
using BukiVedi.Shared.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
|
|
|
|
|
namespace BukiVedi.App.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize("authorized")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class BooksController
|
|
|
|
|
: BaseController
|
|
|
|
@ -18,7 +21,6 @@ namespace BukiVedi.App.Controllers
|
|
|
|
|
_library = library;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//[Authorize]
|
|
|
|
|
[HttpPost("/api/books/search")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> Search([FromBody] QueryRequest request)
|
|
|
|
|
{
|
|
|
|
@ -26,11 +28,113 @@ namespace BukiVedi.App.Controllers
|
|
|
|
|
return Ok(books.Select(b => BookEntityMapper.Map(b)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost("/api/books/aisearch")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> AiSearch([FromBody] QueryRequest request)
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/search/author/{id}")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> SearchByAuthor([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
var books = (await _library.SearchBooks(request.Query)).ToArray();
|
|
|
|
|
var books = (await _library.SearchBooksByAuthor(id)).ToArray();
|
|
|
|
|
return Ok(books.Select(b => BookEntityMapper.Map(b)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/{id}/favorite")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> AddToFavorite([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
if (await Tables.Books.ExistById(id))
|
|
|
|
|
{
|
|
|
|
|
var account_id = OperationContext.OperationInitiator.Id;
|
|
|
|
|
if (!string.IsNullOrEmpty(account_id))
|
|
|
|
|
{
|
|
|
|
|
var exists_fiter = Builders<FavoriteBook>.Filter.And
|
|
|
|
|
(
|
|
|
|
|
Builders<FavoriteBook>.Filter.Eq(f => f.UserId, account_id),
|
|
|
|
|
Builders<FavoriteBook>.Filter.Eq(f => f.BookId, id)
|
|
|
|
|
);
|
|
|
|
|
if (await Tables.FavoriteBooks.Exists(exists_fiter) == false)
|
|
|
|
|
{
|
|
|
|
|
await Tables.FavoriteBooks.Write(new FavoriteBook { BookId = id, UserId = account_id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/{id}/block")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> BlockBook([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
if (await Tables.Books.ExistById(id))
|
|
|
|
|
{
|
|
|
|
|
var account_id = OperationContext.OperationInitiator.Id;
|
|
|
|
|
if (!string.IsNullOrEmpty(account_id))
|
|
|
|
|
{
|
|
|
|
|
var exists_fiter = Builders<DisgustingBook>.Filter.And
|
|
|
|
|
(
|
|
|
|
|
Builders<DisgustingBook>.Filter.Eq(f => f.UserId, account_id),
|
|
|
|
|
Builders<DisgustingBook>.Filter.Eq(f => f.BookId, id)
|
|
|
|
|
);
|
|
|
|
|
if (await Tables.DisgustingBooks.Exists(exists_fiter) == false)
|
|
|
|
|
{
|
|
|
|
|
await Tables.DisgustingBooks.Write(new DisgustingBook { BookId = id, UserId = account_id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/{id}/author/block")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> BlockBookAuthor([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
var book = await Tables.Books.GetById(id);
|
|
|
|
|
if (book != null)
|
|
|
|
|
{
|
|
|
|
|
var account_id = OperationContext.OperationInitiator.Id;
|
|
|
|
|
var authors = book.AuthorIds;
|
|
|
|
|
if (!string.IsNullOrEmpty(account_id) && authors?.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var author in authors)
|
|
|
|
|
{
|
|
|
|
|
var exists_fiter = Builders<DisgustingAuthor>.Filter.And
|
|
|
|
|
(
|
|
|
|
|
Builders<DisgustingAuthor>.Filter.Eq(f => f.UserId, account_id),
|
|
|
|
|
Builders<DisgustingAuthor>.Filter.Eq(f => f.AuthorId, author)
|
|
|
|
|
);
|
|
|
|
|
if (await Tables.DisgustingAuthors.Exists(exists_fiter) == false)
|
|
|
|
|
{
|
|
|
|
|
await Tables.DisgustingAuthors.Write(new DisgustingAuthor { AuthorId = author, UserId = account_id });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/{id}/read")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> AddBookToReadingQueue([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
if (await Tables.Books.ExistById(id))
|
|
|
|
|
{
|
|
|
|
|
var account_id = OperationContext.OperationInitiator.Id;
|
|
|
|
|
if (!string.IsNullOrEmpty(account_id))
|
|
|
|
|
{
|
|
|
|
|
var exists_fiter = Builders<ReadQueueItem>.Filter.And
|
|
|
|
|
(
|
|
|
|
|
Builders<ReadQueueItem>.Filter.Eq(f => f.UserId, account_id),
|
|
|
|
|
Builders<ReadQueueItem>.Filter.Eq(f => f.BookId, id)
|
|
|
|
|
);
|
|
|
|
|
if (await Tables.ReadQueue.Exists(exists_fiter) == false)
|
|
|
|
|
{
|
|
|
|
|
await Tables.ReadQueue.Write(new ReadQueueItem { BookId = id, UserId = account_id, Timestamp = Timestamp.UtcNow });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("/api/books/author/{id}")]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<BookResponse>>> AiSearch([FromRoute] string id)
|
|
|
|
|
{
|
|
|
|
|
var books = (await _library.SearchBooksByAuthor(id)).ToArray();
|
|
|
|
|
return Ok(books.Select(b => BookEntityMapper.Map(b)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|