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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
################################################################################
/.vs
/.idea
/web-library/appsettings.json
/web-library/bin
/web-library/obj
/web-library/web-library.csproj.user
Expand Down
64 changes: 0 additions & 64 deletions web-library/Book/Controller/BookController.cs

This file was deleted.

3 changes: 2 additions & 1 deletion web-library/Book/Entity/Book.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace web_library.Book.Entity;

using web_library.Genre.Entity;
Expand Down Expand Up @@ -46,7 +47,7 @@ public Book(string iSBN, string title, string author, string publisher, DateOnly

public void AddGenre(Genre genre)
{
this.Genres.Add(genre);
Genres.Add(genre);
}


Expand Down
2 changes: 2 additions & 0 deletions web-library/Book/Entity/BookCopy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using web_library.BookReservation.Entity;

namespace web_library.Book.Entity;

Expand All @@ -10,6 +11,7 @@ public class BookCopy
[Column("book_id")]
public int BookId { get; set; }
public Book Book { get; set; }
public Reservation? reservation { get; set; }
public BookCopy() { }
public BookCopy(Book book)
{
Expand Down
7 changes: 5 additions & 2 deletions web-library/Book/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace web_library.Book.DataProvider
using web_library.Shared;
namespace web_library.Book.Repository
{
using Entity;

public class BookRepository : IBookRepository
{
private readonly DataContext _context;
Expand All @@ -22,7 +24,8 @@ public IEnumerable<Book> GetAll()

public Book GetByIdOrThrow(int id)
{
throw new NotImplementedException();
Book? book = _context.Books.Find(id) ?? throw new NotFoundException("Book not found");
return book;
}

public void Remove(Book entity)
Expand Down
4 changes: 3 additions & 1 deletion web-library/Book/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace web_library.Book.DataProvider
namespace web_library.Book.Repository
{
using Entity;
using web_library.Book.Entity;

public interface IBookRepository : IGenericRepository<Book>
{
public void Update(Book book);
Expand Down
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]
public int book_id { get; set; }
}
}
22 changes: 18 additions & 4 deletions web-library/Book/Service/BookService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using web_library.Book.DataProvider;
using web_library.Book.Request;
namespace web_library.Book.Service
namespace web_library.Book.Service
{
using Entity;
using Genre.Entity;
using Newtonsoft.Json;
using web_library.Book.Entity;
using web_library.Book.Repository;
using web_library.Genre.Repository;

public class BookService : IBookService
{
private readonly IBookRepository _bookRepository;
private readonly IBookCopyRepository _bookCopyRepository;
public BookService(IBookRepository bookRepository, IBookCopyRepository bookCopyRepository)
private readonly IGenreRepository _genreRepository;

public BookService(IBookRepository bookRepository, IBookCopyRepository bookCopyRepository, IGenreRepository genreRepository)
{
_bookRepository = bookRepository;
_bookCopyRepository = bookCopyRepository;
_genreRepository = genreRepository;
}

public void createBook(CreateBookRequest request)
Expand All @@ -34,5 +38,15 @@ public void createBook(CreateBookRequest request)

return;
}
public void assigneGenre(AssigneGenreToBookRequest request)
{
Book? book = _bookRepository.GetByIdOrThrow(request.book_id);
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(AssigneGenreToBookRequest request);
}
}
32 changes: 32 additions & 0 deletions web-library/BookReservation/Entity/Reservation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace web_library.BookReservation.Entity
{
using System.ComponentModel.DataAnnotations;
using web_library.Book.Entity;

[Table("reservations")]
public class Reservation
{
[Column("id")]
[Key]
public int Id { get; set; }
[Column("reservation_start_date")]
public DateOnly reservationStartDate { get; }
[Column("reservation_end_date")]
public DateOnly reservationEndDate { get; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rezerwacja nie ma end date(zrob serwis ktory na podstawie daty sprawdza czy nie powinno sie jej zdjąć, jak jest wypozyczona to czekaj z wygasnieciem do konca wypozyczenia +3 dni, jak nie to poprostu 3 dni

[Column("book_copy_id")]
public int bookCopyId { get; set; }
public BookCopy bookCopy { get; set; } = null!;

public Reservation()
{
reservationStartDate = new();
reservationEndDate = reservationStartDate.AddMonths(+1);
}
public Reservation(BookCopy book)
{
bookCopy = book;
}
}
}
99 changes: 53 additions & 46 deletions web-library/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,72 @@
using Microsoft.EntityFrameworkCore;
using web_library.User.Entity;

namespace web_library;

public class DataContext : DbContext
namespace web_library
{
protected readonly IConfiguration Configuration;

public DataContext(IConfiguration configuration)
{
Configuration = configuration;
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to postgres with connection string from app settings
options.UseNpgsql(Configuration.GetConnectionString("WebApiDatabase"));
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
using Api.Book.Entity;
using Api.Genre.Entity;
using Api.User.Entity;
using Api.BookReservation.Entity;
using web_library.Book.Entity;
using web_library.BookReservation.Entity;
using web_library.User.Entity;

public class DataContext : DbContext
{
modelBuilder.Entity<Book.Entity.Book>()
.HasMany(left => left.Genres)
.WithMany(right => right.Books)
.UsingEntity<Dictionary<string, object>>(
"book_genres",
j => j.HasOne<Genre.Entity.Genre>().WithMany().HasForeignKey("genre_id"),
j => j.HasOne<Book.Entity.Book>().WithMany().HasForeignKey("book_id")
);
protected readonly IConfiguration Configuration;

modelBuilder.Entity<User.Entity.User>(entity =>
public DataContext(IConfiguration configuration)
{
entity.ToTable("users");
entity.HasIndex(u => u.Email).IsUnique();
Configuration = configuration;
}

entity.HasOne(u => u.UserBasicInfo)
.WithOne(ubi => ubi.User)
.HasForeignKey<UserBasicInfo>(ubi => ubi.UserId)
.OnDelete(DeleteBehavior.Cascade);
});
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to postgres with connection string from app settings
options.UseNpgsql(Configuration.GetConnectionString("WebApiDatabase"));
}

modelBuilder.Entity<UserBasicInfo>(entity =>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
entity.ToTable("user_basic_info");
modelBuilder.Entity<Book>()
.HasMany(left => left.Genres)
.WithMany(right => right.Books)
.UsingEntity<Dictionary<string, object>>(
"book_genres",
j => j.HasOne<Genre>().WithMany().HasForeignKey("genre_id"),
j => j.HasOne<Book>().WithMany().HasForeignKey("book_id")
);

entity.Property(ubi => ubi.UserId).IsRequired();
modelBuilder.Entity<User>(entity =>
{
entity.ToTable("users");
entity.HasIndex(u => u.Email).IsUnique();

entity.HasIndex(ubi => ubi.UserId).IsUnique();
entity.HasOne(u => u.UserBasicInfo)
.WithOne(ubi => ubi.User)
.HasForeignKey<UserBasicInfo>(ubi => ubi.UserId)
.OnDelete(DeleteBehavior.Cascade);
});

});
modelBuilder.Entity<UserBasicInfo>(entity =>
{
entity.ToTable("user_basic_info");

base.OnModelCreating(modelBuilder);
}
entity.Property(ubi => ubi.UserId).IsRequired();

entity.HasIndex(ubi => ubi.UserId).IsUnique();

public DbSet<User.Entity.User> Users { get; set; }
});

base.OnModelCreating(modelBuilder);
}

public DbSet<Book.Entity.Book> Books { get; set; }
public DbSet<Book.Entity.BookCopy> BooksCopy { get; set; }
public DbSet<Genre.Entity.Genre> Genres { get; set; }
public DbSet<User> Users { get; set; }


public DbSet<UserBasicInfo> UserBasicInfos { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<BookCopy> BooksCopy { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<UserBasicInfo> UserBasicInfos { get; set; }
}
}
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();
}
}
}
}
1 change: 1 addition & 0 deletions web-library/Genre/Entity/Genre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace web_library.Genre.Entity
{
using web_library.Book.Entity;

[Table("genres")]
public class Genre
{
Expand Down
Loading