Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 4 additions & 32 deletions web-library/Book/Controller/BookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,18 @@ public BookController(IBookService bookService)
_bookService = bookService;
}

// GET: api/<ValuesController>
[HttpGet]
public ActionResult<IEnumerable<Book>> Get()
{
return Ok();
}

// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

// POST api/<ValuesController>
[HttpPost]
public ActionResult Post([FromBody] CreateBookRequest request)
{
try
{
_bookService.createBook(request);
}
catch
(Exception)
{
return NotFound();
}
_bookService.createBook(request);
return Ok();
}

// PUT api/<ValuesController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/<ValuesController>/5
[HttpDelete("{id}")]
public void Delete(int id)
public ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request)
{
_bookService.assigneGenre(id,request);
return Ok();
}
}
}
64 changes: 32 additions & 32 deletions web-library/Book/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Entity.Book> GetAll()
{
throw new NotImplementedException();
}
public IEnumerable<Book> 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();
}
}
12 changes: 12 additions & 0 deletions web-library/Book/Request/AssigneGenreToBookRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace web_library.Book.Request
{
public class AssigneGenreToBookRequest
{

public ICollection<int> genres_ids { get; set; }
[JsonIgnore]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Po co to ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request do kontrolera książki który do isniejącej książki dodaje gatunki

public int book_id { get; set; }
}
}
29 changes: 23 additions & 6 deletions web-library/Book/Service/BookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
}
}
1 change: 1 addition & 0 deletions web-library/Book/Service/IBookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace web_library.Book.Service
public interface IBookService
{
void createBook(CreateBookRequest request);
void assigneGenre(int bookId, AssigneGenreToBookRequest request);
}
}
6 changes: 4 additions & 2 deletions web-library/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -93,13 +95,13 @@ public static IServiceCollection ServicesInjection(this IServiceCollection servi
services.AddTransient<IBookRepository, BookRepository>();
services.AddTransient<IBookService, BookService>();
services.AddTransient<IBookCopyRepository, BookCopyRepository>();


services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IUserBasicInfoRepository, UserBasicInfoRepository>();
services.AddTransient<IUserService, UserService>();
services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IRoleRepository, RoleRepository>();
services.AddTransient<IGenreRepository,GenreRepository>();
services.AddTransient<IGenreService, GenreService>();

return services;
}
Expand Down
34 changes: 34 additions & 0 deletions web-library/Genre/Controller/GenresController.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
35 changes: 35 additions & 0 deletions web-library/Genre/Repository/GenreRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Genre> 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();
}
}
7 changes: 7 additions & 0 deletions web-library/Genre/Repository/IGenreRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace web_library.Genre.Repository
{
using Entity;
public interface IGenreRepository : IGenericRepository<Genre>
{
}
}
7 changes: 7 additions & 0 deletions web-library/Genre/Request/CreateGenreRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace web_library.Genre.Request
{
public class CreateGenreRequest
{
public string Name { get; set; }
}
}
28 changes: 28 additions & 0 deletions web-library/Genre/Service/GenreService.cs
Original file line number Diff line number Diff line change
@@ -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<Genre>(jsonString) ?? throw new JsonException();

_genreRepository.Add(genre);
}
}
}
9 changes: 9 additions & 0 deletions web-library/Genre/Service/IGenreService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using web_library.Genre.Request;

namespace web_library.Genre.Service
{
public interface IGenreService
{
void createGenre(CreateGenreRequest request);
}
}
7 changes: 7 additions & 0 deletions web-library/Program.cs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
2 changes: 1 addition & 1 deletion web-library/SharedExceptions/AlreadyExistsException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace web_library.Shared;
namespace web_library.SharedExceptions;

public class AlreadyExistsException : Exception
{
Expand Down
8 changes: 0 additions & 8 deletions web-library/appsettings.Development.json

This file was deleted.

2 changes: 1 addition & 1 deletion web-library/web-library.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down