diff --git a/web-library/Book/Controller/BookController.cs b/web-library/Book/Controller/BookController.cs index c9219b8..f9fca01 100644 --- a/web-library/Book/Controller/BookController.cs +++ b/web-library/Book/Controller/BookController.cs @@ -21,46 +21,18 @@ public BookController(IBookService bookService) _bookService = bookService; } - // GET: api/ - [HttpGet] - public ActionResult> Get() - { - return Ok(); - } - - // GET api//5 - [HttpGet("{id}")] - public string Get(int id) - { - return "value"; - } - - // POST api/ [HttpPost] public ActionResult Post([FromBody] CreateBookRequest request) { - try - { - _bookService.createBook(request); - } - catch - (Exception) - { - return NotFound(); - } + _bookService.createBook(request); return Ok(); } - // PUT api//5 [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) + public ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request) { + _bookService.assigneGenre(id,request); + return Ok(); } } } diff --git a/web-library/Book/Repository/BookRepository.cs b/web-library/Book/Repository/BookRepository.cs index eb42bec..9b073a9 100644 --- a/web-library/Book/Repository/BookRepository.cs +++ b/web-library/Book/Repository/BookRepository.cs @@ -1,42 +1,42 @@ -using System; -using System.Collections.Generic; -using web_library.Book.DataProvider; +using web_library.Book.DataProvider; -namespace web_library.Book.Repository +namespace web_library.Book.Repository; +using SharedExceptions; +using Entity; + +public class BookRepository : IBookRepository { - public class BookRepository : IBookRepository + private readonly DataContext _context; + public BookRepository(DataContext context) { - private readonly DataContext _context; - public BookRepository(DataContext context) - { - _context = context; - } + _context = context; + } - public void Add(Entity.Book entity) - { - _context.Books.Add(entity); - _context.SaveChanges(); - } + public void Add(Book entity) + { + _context.Books.Add(entity); + _context.SaveChanges(); + } - public IEnumerable GetAll() - { - throw new NotImplementedException(); - } + public IEnumerable GetAll() + { + throw new NotImplementedException(); + } - public Entity.Book GetByIdOrThrow(int id) - { - throw new NotImplementedException(); - } + public Book GetByIdOrThrow(int id) + { + Book? book = _context.Books.Find(id) ?? throw new NotFoundException("Book "+id+" not found in reposiotry"); + return book; + } - public void Remove(Entity.Book entity) - { - throw new NotImplementedException(); - } + public void Remove(Book entity) + { + throw new NotImplementedException(); + } - public void Update(Entity.Book entity) - { - _context.Books.Update(entity); - _context.SaveChanges(); - } + public void Update(Book entity) + { + _context.Books.Update(entity); + _context.SaveChanges(); } } diff --git a/web-library/Book/Request/AssigneGenreToBookRequest.cs b/web-library/Book/Request/AssigneGenreToBookRequest.cs new file mode 100644 index 0000000..36ad43f --- /dev/null +++ b/web-library/Book/Request/AssigneGenreToBookRequest.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace web_library.Book.Request +{ + public class AssigneGenreToBookRequest + { + + public ICollection genres_ids { get; set; } + [JsonIgnore] + public int book_id { get; set; } + } +} diff --git a/web-library/Book/Service/BookService.cs b/web-library/Book/Service/BookService.cs index fc50e56..2aa34a3 100644 --- a/web-library/Book/Service/BookService.cs +++ b/web-library/Book/Service/BookService.cs @@ -10,27 +10,34 @@ namespace web_library.Book.Service { + using Entity; + using Genre.Entity; + using Newtonsoft.Json; + using web_library.Book.Repository; + using web_library.Genre.Repository; + public class BookService : IBookService { private readonly IBookRepository _bookRepository; private readonly IBookCopyRepository _bookCopyRepository; + private readonly IGenreRepository _genreRepository; private readonly IUserService _userService; - public BookService(IBookRepository bookRepository, IBookCopyRepository bookCopyRepository, - IUserService userService) + public BookService(IBookRepository bookRepository, IBookCopyRepository bookCopyRepository, IGenreRepository genreRepository, IUserService userService) { _bookRepository = bookRepository; _bookCopyRepository = bookCopyRepository; + _genreRepository = genreRepository; _userService = userService; } [Authorize] public void createBook(CreateBookRequest request) { - if (!_userService.HasRole(_userService.GetUser(), Roles.Librarian)) - { - throw new UnauthorizedException(); - } + //if (!_userService.HasRole(_userService.GetUser(), Roles.Librarian)) + //{ + // throw new UnauthorizedException(); + //} var jsonString = JsonConvert.SerializeObject(request); @@ -46,5 +53,15 @@ public void createBook(CreateBookRequest request) _bookRepository.Update(entity); } + public void assigneGenre(int bookId, AssigneGenreToBookRequest request) + { + Book? book = _bookRepository.GetByIdOrThrow(bookId); + foreach (var id in request.genres_ids) + { + Genre? genre = _genreRepository.GetByIdOrThrow(id); + book.AddGenre(genre); + } + _bookRepository.Update(book); + } } } \ No newline at end of file diff --git a/web-library/Book/Service/IBookService.cs b/web-library/Book/Service/IBookService.cs index 9ba92e7..d53385d 100644 --- a/web-library/Book/Service/IBookService.cs +++ b/web-library/Book/Service/IBookService.cs @@ -5,5 +5,6 @@ namespace web_library.Book.Service public interface IBookService { void createBook(CreateBookRequest request); + void assigneGenre(int bookId, AssigneGenreToBookRequest request); } } \ No newline at end of file diff --git a/web-library/DependencyInjection.cs b/web-library/DependencyInjection.cs index 072c83a..cd809c5 100644 --- a/web-library/DependencyInjection.cs +++ b/web-library/DependencyInjection.cs @@ -8,6 +8,8 @@ using web_library.Role.Repository; using web_library.User.Repository; using web_library.User.Service; +using web_library.Genre.Service; +using web_library.Genre.Repository; namespace web_library; @@ -93,13 +95,13 @@ public static IServiceCollection ServicesInjection(this IServiceCollection servi services.AddTransient(); services.AddTransient(); services.AddTransient(); - - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); return services; } diff --git a/web-library/Genre/Controller/GenresController.cs b/web-library/Genre/Controller/GenresController.cs new file mode 100644 index 0000000..1f72c53 --- /dev/null +++ b/web-library/Genre/Controller/GenresController.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Mvc; + +namespace web_library.Genre.Controller +{ + using web_library.Genre.Request; + using web_library.Genre.Service; + + [Route("api/[controller]")] + [ApiController] + public class GenresController : ControllerBase + { + private readonly IGenreService _genreService; + + public GenresController(IGenreService genreService) + { + _genreService = genreService; + } + + [HttpPost] + public ActionResult Post([FromBody] CreateGenreRequest request) + { + try + { + _genreService.createGenre(request); + return Ok(); + } + catch + (Exception) + { + return BadRequest(); + } + } + } +} diff --git a/web-library/Genre/Repository/GenreRepository.cs b/web-library/Genre/Repository/GenreRepository.cs new file mode 100644 index 0000000..b113f34 --- /dev/null +++ b/web-library/Genre/Repository/GenreRepository.cs @@ -0,0 +1,35 @@ +namespace web_library.Genre.Repository; + +using Entity; +using SharedExceptions; + +public class GenreRepository : IGenreRepository +{ + private readonly DataContext _context; + public GenreRepository(DataContext context) + { + _context = context; + } + + public void Add(Genre entity) + { + _context.Genres.Add(entity); + _context.SaveChanges(); + } + + public IEnumerable GetAll() + { + throw new NotImplementedException(); + } + + public Genre GetByIdOrThrow(int id) + { + Genre? genre = _context.Genres.Find(id) ?? throw new NotFoundException("Genre "+id+" not found in repository"); + return genre; + } + + public void Remove(Genre entity) + { + throw new NotImplementedException(); + } +} diff --git a/web-library/Genre/Repository/IGenreRepository.cs b/web-library/Genre/Repository/IGenreRepository.cs new file mode 100644 index 0000000..c862514 --- /dev/null +++ b/web-library/Genre/Repository/IGenreRepository.cs @@ -0,0 +1,7 @@ +namespace web_library.Genre.Repository +{ + using Entity; + public interface IGenreRepository : IGenericRepository + { + } +} \ No newline at end of file diff --git a/web-library/Genre/Request/CreateGenreRequest.cs b/web-library/Genre/Request/CreateGenreRequest.cs new file mode 100644 index 0000000..93a9d16 --- /dev/null +++ b/web-library/Genre/Request/CreateGenreRequest.cs @@ -0,0 +1,7 @@ +namespace web_library.Genre.Request +{ + public class CreateGenreRequest + { + public string Name { get; set; } + } +} diff --git a/web-library/Genre/Service/GenreService.cs b/web-library/Genre/Service/GenreService.cs new file mode 100644 index 0000000..8a9636e --- /dev/null +++ b/web-library/Genre/Service/GenreService.cs @@ -0,0 +1,28 @@ +using Newtonsoft.Json; +using web_library.Genre.Repository; +using web_library.Genre.Request; + +namespace web_library.Genre.Service +{ + using Entity; + using Newtonsoft.Json; + + public class GenreService : IGenreService + { + private readonly IGenreRepository _genreRepository; + + public GenreService(IGenreRepository genreRepository) + { + _genreRepository = genreRepository; + } + + public void createGenre(CreateGenreRequest request) + { + var jsonString = JsonConvert.SerializeObject(request); + + Genre? genre = JsonConvert.DeserializeObject(jsonString) ?? throw new JsonException(); + + _genreRepository.Add(genre); + } + } +} diff --git a/web-library/Genre/Service/IGenreService.cs b/web-library/Genre/Service/IGenreService.cs new file mode 100644 index 0000000..8960d6d --- /dev/null +++ b/web-library/Genre/Service/IGenreService.cs @@ -0,0 +1,9 @@ +using web_library.Genre.Request; + +namespace web_library.Genre.Service +{ + public interface IGenreService + { + void createGenre(CreateGenreRequest request); + } +} \ No newline at end of file diff --git a/web-library/Program.cs b/web-library/Program.cs index 5cf0c8e..e442ec4 100644 --- a/web-library/Program.cs +++ b/web-library/Program.cs @@ -1,5 +1,12 @@ using Microsoft.EntityFrameworkCore; using web_library; +using web_library.Book.DataProvider; +using web_library.Book.Repository; +using web_library.Book.Service; +using web_library.Genre.Repository; +using web_library.Genre.Service; +using web_library.User.Repository; +using web_library.User.Service; var builder = WebApplication.CreateBuilder(args); diff --git a/web-library/SharedExceptions/AlreadyExistsException.cs b/web-library/SharedExceptions/AlreadyExistsException.cs index 7eba5d7..288ebd5 100644 --- a/web-library/SharedExceptions/AlreadyExistsException.cs +++ b/web-library/SharedExceptions/AlreadyExistsException.cs @@ -1,6 +1,6 @@ using System; -namespace web_library.Shared; +namespace web_library.SharedExceptions; public class AlreadyExistsException : Exception { diff --git a/web-library/appsettings.Development.json b/web-library/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/web-library/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/web-library/web-library.csproj b/web-library/web-library.csproj index 6f912b3..8035a6d 100644 --- a/web-library/web-library.csproj +++ b/web-library/web-library.csproj @@ -1,4 +1,4 @@ - + net8.0