From 927ce4826b2d751d27c4c6682512932912b757ef Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 2 Dec 2023 03:44:29 +0100 Subject: [PATCH 1/8] add controller for genre --- .../Genre/Controller/GenresController.cs | 38 ++++++ .../Genre/Repository/GenreRepository.cs | 33 +++++ .../Genre/Repository/IGenreRepository.cs | 7 + .../Genre/Request/CreateGenreRequest.cs | 7 + web-library/Genre/Service/GenreService.cs | 27 ++++ web-library/Genre/Service/IGenreService.cs | 9 ++ .../Migrations/20231201221253_UserWithInfo.cs | 124 ------------------ ...46_userAndUserBasicInfoEntity.Designer.cs} | 55 +++++++- ...231202024346_userAndUserBasicInfoEntity.cs | 74 +++++++++++ web-library/Program.cs | 6 +- web-library/User/Entity/UserBasicInfo.cs | 2 +- web-library/appsettings.Development.json | 3 + 12 files changed, 254 insertions(+), 131 deletions(-) create mode 100644 web-library/Genre/Controller/GenresController.cs create mode 100644 web-library/Genre/Repository/GenreRepository.cs create mode 100644 web-library/Genre/Repository/IGenreRepository.cs create mode 100644 web-library/Genre/Request/CreateGenreRequest.cs create mode 100644 web-library/Genre/Service/GenreService.cs create mode 100644 web-library/Genre/Service/IGenreService.cs delete mode 100644 web-library/Migrations/20231201221253_UserWithInfo.cs rename web-library/Migrations/{20231201221253_UserWithInfo.Designer.cs => 20231202024346_userAndUserBasicInfoEntity.Designer.cs} (78%) create mode 100644 web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs diff --git a/web-library/Genre/Controller/GenresController.cs b/web-library/Genre/Controller/GenresController.cs new file mode 100644 index 0000000..112151e --- /dev/null +++ b/web-library/Genre/Controller/GenresController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; + +namespace web_library.Genre.Controller +{ + using Microsoft.AspNetCore.Authorization; + 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; + } + + // POST: api/Genres + // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 + [HttpPost] + [AllowAnonymous] + public ActionResult Post([FromBody] CreateGenreRequest request) + { + try + { + _genreService.createGenre(request); + } + catch + (Exception) + { + return NotFound(); + } + return Ok(); + } + } +} diff --git a/web-library/Genre/Repository/GenreRepository.cs b/web-library/Genre/Repository/GenreRepository.cs new file mode 100644 index 0000000..a142acd --- /dev/null +++ b/web-library/Genre/Repository/GenreRepository.cs @@ -0,0 +1,33 @@ +namespace web_library.Genre.Repository +{ + using Entity; + public class GenreRepository : IGenreRepository + { + private readonly DataContext _context; + public GenreRepository(DataContext context) + { + _context = context; + } + + public void Add(Genre entity) + { + _context.Add(entity); + _context.SaveChanges(); + } + + public IEnumerable GetAll() + { + throw new NotImplementedException(); + } + + public Genre GetByIdOrThrow(int id) + { + throw new NotImplementedException(); + } + + 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..c6cbad7 --- /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..cd8b161 --- /dev/null +++ b/web-library/Genre/Service/GenreService.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using web_library.Genre.Repository; +using web_library.Genre.Request; + +namespace web_library.Genre.Service +{ + using Entity; + 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? entity = JsonConvert.DeserializeObject(jsonString) ?? throw new NotImplementedException(); + + _genreRepository.Add(entity); + return; + } + } +} 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/Migrations/20231201221253_UserWithInfo.cs b/web-library/Migrations/20231201221253_UserWithInfo.cs deleted file mode 100644 index 47b528d..0000000 --- a/web-library/Migrations/20231201221253_UserWithInfo.cs +++ /dev/null @@ -1,124 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace web_library.Migrations -{ - /// - public partial class UserWithInfo : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "books", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - isbn = table.Column(type: "text", nullable: false), - title = table.Column(type: "text", nullable: false), - author = table.Column(type: "text", nullable: false), - publisher = table.Column(type: "text", nullable: false), - publication_date = table.Column(type: "date", nullable: false), - location = table.Column(type: "text", nullable: false), - description = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_books", x => x.id); - }); - - migrationBuilder.CreateTable( - name: "users", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - email = table.Column(type: "text", nullable: false), - password = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_users", x => x.id); - }); - - migrationBuilder.CreateTable( - name: "book_copies", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - book_id = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_book_copies", x => x.id); - table.ForeignKey( - name: "FK_book_copies_books_book_id", - column: x => x.book_id, - principalTable: "books", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "user_basic_info", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - userId = table.Column(type: "integer", nullable: false), - first_name = table.Column(type: "text", nullable: false), - last_name = table.Column(type: "text", nullable: false), - phone_number = table.Column(type: "text", nullable: false), - address = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_user_basic_info", x => x.id); - table.ForeignKey( - name: "FK_user_basic_info_users_userId", - column: x => x.userId, - principalTable: "users", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_book_copies_book_id", - table: "book_copies", - column: "book_id"); - - migrationBuilder.CreateIndex( - name: "IX_user_basic_info_userId", - table: "user_basic_info", - column: "userId", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_users_email", - table: "users", - column: "email", - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "book_copies"); - - migrationBuilder.DropTable( - name: "user_basic_info"); - - migrationBuilder.DropTable( - name: "books"); - - migrationBuilder.DropTable( - name: "users"); - } - } -} diff --git a/web-library/Migrations/20231201221253_UserWithInfo.Designer.cs b/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.Designer.cs similarity index 78% rename from web-library/Migrations/20231201221253_UserWithInfo.Designer.cs rename to web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.Designer.cs index f41dd26..125faa7 100644 --- a/web-library/Migrations/20231201221253_UserWithInfo.Designer.cs +++ b/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.Designer.cs @@ -12,8 +12,8 @@ namespace web_library.Migrations { [DbContext(typeof(DataContext))] - [Migration("20231201221253_UserWithInfo")] - partial class UserWithInfo + [Migration("20231202024346_userAndUserBasicInfoEntity")] + partial class userAndUserBasicInfoEntity { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -25,6 +25,21 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("book_genres", b => + { + b.Property("book_id") + .HasColumnType("integer"); + + b.Property("genre_id") + .HasColumnType("integer"); + + b.HasKey("book_id", "genre_id"); + + b.HasIndex("genre_id"); + + b.ToTable("book_genres"); + }); + modelBuilder.Entity("web_library.Book.Entity.Book", b => { b.Property("Id") @@ -93,6 +108,25 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("book_copies"); }); + modelBuilder.Entity("web_library.Genre.Entity.Genre", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text") + .HasColumnName("name"); + + b.HasKey("Id"); + + b.ToTable("genres"); + }); + modelBuilder.Entity("web_library.User.Entity.User", b => { b.Property("Id") @@ -151,7 +185,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("UserId") .HasColumnType("integer") - .HasColumnName("userId"); + .HasColumnName("user_id"); b.HasKey("Id"); @@ -161,6 +195,21 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.ToTable("user_basic_info", (string)null); }); + modelBuilder.Entity("book_genres", b => + { + b.HasOne("web_library.Book.Entity.Book", null) + .WithMany() + .HasForeignKey("book_id") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("web_library.Genre.Entity.Genre", null) + .WithMany() + .HasForeignKey("genre_id") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("web_library.Book.Entity.BookCopy", b => { b.HasOne("web_library.Book.Entity.Book", "Book") diff --git a/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs b/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs new file mode 100644 index 0000000..5e8fc5c --- /dev/null +++ b/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs @@ -0,0 +1,74 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace web_library.Migrations +{ + /// + public partial class userAndUserBasicInfoEntity : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "users", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + email = table.Column(type: "text", nullable: false), + password = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_users", x => x.id); + }); + + migrationBuilder.CreateTable( + name: "user_basic_info", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + user_id = table.Column(type: "integer", nullable: false), + first_name = table.Column(type: "text", nullable: false), + last_name = table.Column(type: "text", nullable: false), + phone_number = table.Column(type: "text", nullable: false), + address = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_user_basic_info", x => x.id); + table.ForeignKey( + name: "FK_user_basic_info_users_user_id", + column: x => x.user_id, + principalTable: "users", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_user_basic_info_user_id", + table: "user_basic_info", + column: "user_id", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_users_email", + table: "users", + column: "email", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "user_basic_info"); + + migrationBuilder.DropTable( + name: "users"); + } + } +} diff --git a/web-library/Program.cs b/web-library/Program.cs index bcda9f1..8045ec8 100644 --- a/web-library/Program.cs +++ b/web-library/Program.cs @@ -45,6 +45,9 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); // Add services to the container. @@ -61,9 +64,6 @@ options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles; }); -builder.Services.AddTransient(); -builder.Services.AddTransient(); -builder.Services.AddTransient(); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/web-library/User/Entity/UserBasicInfo.cs b/web-library/User/Entity/UserBasicInfo.cs index 0fe403f..83c32a6 100644 --- a/web-library/User/Entity/UserBasicInfo.cs +++ b/web-library/User/Entity/UserBasicInfo.cs @@ -9,7 +9,7 @@ public class UserBasicInfo [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } - [Column(name: "userId")] public int UserId { get; set; } + [Column(name: "user_id")] public int UserId { get; set; } [Column(name: "first_name")] public string FirstName { get; set; } diff --git a/web-library/appsettings.Development.json b/web-library/appsettings.Development.json index 0c208ae..43aa661 100644 --- a/web-library/appsettings.Development.json +++ b/web-library/appsettings.Development.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "WebApiDatabase": "Host=localhost; Database=TinyLib; Username=postgres; Password=kuba" + }, "Logging": { "LogLevel": { "Default": "Information", From 5b5e7c4c9470b7aa27a4f56e3c0f505b254f5bad Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 2 Dec 2023 06:17:40 +0100 Subject: [PATCH 2/8] add endpoint for gatunkes --- web-library/Book/Controller/BookController.cs | 19 +++++++--- web-library/Book/Repository/BookRepository.cs | 6 ++- .../Book/Request/AssigneGenreToBookRequest.cs | 12 ++++++ web-library/Book/Service/BookService.cs | 17 ++++++++- web-library/Book/Service/IBookService.cs | 1 + .../Genre/Contoller/GenreController.cs | 31 ++++++++++++++++ .../Genre/Repository/GenreRepository.cs | 37 +++++++++++++++++++ .../Genre/Repository/IGenreRepository.cs | 16 ++++++++ .../Genre/Request/CreateGenreRequest.cs | 7 ++++ web-library/Genre/Service/GenreService.cs | 26 +++++++++++++ web-library/Genre/Service/IGenreService.cs | 9 +++++ web-library/Program.cs | 4 ++ web-library/Shared/NotFoundException.cs | 9 +++++ 13 files changed, 186 insertions(+), 8 deletions(-) create mode 100644 web-library/Book/Request/AssigneGenreToBookRequest.cs create mode 100644 web-library/Genre/Contoller/GenreController.cs create mode 100644 web-library/Genre/Repository/GenreRepository.cs create mode 100644 web-library/Genre/Repository/IGenreRepository.cs create mode 100644 web-library/Genre/Request/CreateGenreRequest.cs create mode 100644 web-library/Genre/Service/GenreService.cs create mode 100644 web-library/Genre/Service/IGenreService.cs create mode 100644 web-library/Shared/NotFoundException.cs diff --git a/web-library/Book/Controller/BookController.cs b/web-library/Book/Controller/BookController.cs index 4d7bacf..aec3eaf 100644 --- a/web-library/Book/Controller/BookController.cs +++ b/web-library/Book/Controller/BookController.cs @@ -28,9 +28,10 @@ public ActionResult> Get() // GET api//5 [HttpGet("{id}")] - public string Get(int id) + public ActionResult Get(int id) { - return "value"; + + return Ok(); } // POST api/ @@ -41,8 +42,7 @@ public ActionResult Post([FromBody] CreateBookRequest request) { _bookService.createBook(request); } - catch - (Exception) + catch (Exception) { return NotFound(); } @@ -51,8 +51,17 @@ public ActionResult Post([FromBody] CreateBookRequest request) // PUT api//5 [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) + public ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request) { + try + { + request.book_id = id; + _bookService.assigneGenre(request); + } catch (Exception) + { + return NotFound(); + } + return Ok(); } // DELETE api//5 diff --git a/web-library/Book/Repository/BookRepository.cs b/web-library/Book/Repository/BookRepository.cs index d2bec19..f4f0510 100644 --- a/web-library/Book/Repository/BookRepository.cs +++ b/web-library/Book/Repository/BookRepository.cs @@ -1,4 +1,5 @@ -namespace web_library.Book.DataProvider +using web_library.Shared; +namespace web_library.Book.DataProvider { using Entity; public class BookRepository : IBookRepository @@ -22,7 +23,8 @@ public IEnumerable 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) 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 c9bce22..af80171 100644 --- a/web-library/Book/Service/BookService.cs +++ b/web-library/Book/Service/BookService.cs @@ -3,17 +3,22 @@ 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; - 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) @@ -34,5 +39,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); + } } } diff --git a/web-library/Book/Service/IBookService.cs b/web-library/Book/Service/IBookService.cs index 9ba92e7..41ddd37 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(AssigneGenreToBookRequest request); } } \ No newline at end of file diff --git a/web-library/Genre/Contoller/GenreController.cs b/web-library/Genre/Contoller/GenreController.cs new file mode 100644 index 0000000..b1165e3 --- /dev/null +++ b/web-library/Genre/Contoller/GenreController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Mvc; +namespace web_library.Genre.Contoller +{ + using Service; + using web_library.Genre.Request; + + [Route("api/[controller]")] + [ApiController] + public class GenreController : ControllerBase + { + private readonly IGenreService _genreService; + + public GenreController(IGenreService genreService) + { + _genreService = genreService; + } + + [HttpPost] + public ActionResult Post([FromBody] CreateGenreRequest request) + { + try + { + _genreService.createGenre(request); + + } catch (Exception) { + return NotFound(); + } + return Ok(); + } + } +} diff --git a/web-library/Genre/Repository/GenreRepository.cs b/web-library/Genre/Repository/GenreRepository.cs new file mode 100644 index 0000000..814aef9 --- /dev/null +++ b/web-library/Genre/Repository/GenreRepository.cs @@ -0,0 +1,37 @@ +using web_library.Shared; +namespace web_library.Genre.Repository +{ + using Entity; + using System.Collections.Generic; + + 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 not found"); + 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..5d319d2 --- /dev/null +++ b/web-library/Genre/Repository/IGenreRepository.cs @@ -0,0 +1,16 @@ + + + + + +namespace web_library.Genre.Repository +{ + using Entity; + public interface IGenreRepository : IGenericRepository + { + void Add(Entity.Genre entity); + IEnumerable GetAll(); + Entity.Genre GetByIdOrThrow(int id); + void Remove(Entity.Genre entity); + } +} \ 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..7263d85 --- /dev/null +++ b/web-library/Genre/Service/GenreService.cs @@ -0,0 +1,26 @@ +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 898017c..29faffe 100644 --- a/web-library/Program.cs +++ b/web-library/Program.cs @@ -7,6 +7,8 @@ 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; @@ -44,6 +46,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); diff --git a/web-library/Shared/NotFoundException.cs b/web-library/Shared/NotFoundException.cs new file mode 100644 index 0000000..8b321dc --- /dev/null +++ b/web-library/Shared/NotFoundException.cs @@ -0,0 +1,9 @@ +namespace web_library.Shared; + +public class NotFoundException : Exception +{ + public NotFoundException(string message) + : base(message) + { + } +} \ No newline at end of file From 6a2fc2f036d58e03cdd2a6ba997aceeb86301d3d Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 2 Dec 2023 06:27:51 +0100 Subject: [PATCH 3/8] v1 fix --- .../20231201221253_UserWithInfo.Designer.cs | 198 ------------------ ...231202024346_userAndUserBasicInfoEntity.cs | 74 ------- 2 files changed, 272 deletions(-) delete mode 100644 web-library/Migrations/20231201221253_UserWithInfo.Designer.cs delete mode 100644 web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs diff --git a/web-library/Migrations/20231201221253_UserWithInfo.Designer.cs b/web-library/Migrations/20231201221253_UserWithInfo.Designer.cs deleted file mode 100644 index f41dd26..0000000 --- a/web-library/Migrations/20231201221253_UserWithInfo.Designer.cs +++ /dev/null @@ -1,198 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; -using web_library; - -#nullable disable - -namespace web_library.Migrations -{ - [DbContext(typeof(DataContext))] - [Migration("20231201221253_UserWithInfo")] - partial class UserWithInfo - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.0") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("web_library.Book.Entity.Book", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Author") - .IsRequired() - .HasColumnType("text") - .HasColumnName("author"); - - b.Property("Description") - .IsRequired() - .HasColumnType("text") - .HasColumnName("description"); - - b.Property("ISBN") - .IsRequired() - .HasColumnType("text") - .HasColumnName("isbn"); - - b.Property("Location") - .IsRequired() - .HasColumnType("text") - .HasColumnName("location"); - - b.Property("Publication_date") - .HasColumnType("date") - .HasColumnName("publication_date"); - - b.Property("Publisher") - .IsRequired() - .HasColumnType("text") - .HasColumnName("publisher"); - - b.Property("Title") - .IsRequired() - .HasColumnType("text") - .HasColumnName("title"); - - b.HasKey("Id"); - - b.ToTable("books"); - }); - - modelBuilder.Entity("web_library.Book.Entity.BookCopy", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("BookId") - .HasColumnType("integer") - .HasColumnName("book_id"); - - b.HasKey("Id"); - - b.HasIndex("BookId"); - - b.ToTable("book_copies"); - }); - - modelBuilder.Entity("web_library.User.Entity.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Email") - .IsRequired() - .HasColumnType("text") - .HasColumnName("email"); - - b.Property("Password") - .IsRequired() - .HasColumnType("text") - .HasColumnName("password"); - - b.HasKey("Id"); - - b.HasIndex("Email") - .IsUnique(); - - b.ToTable("users", (string)null); - }); - - modelBuilder.Entity("web_library.User.Entity.UserBasicInfo", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Address") - .IsRequired() - .HasColumnType("text") - .HasColumnName("address"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("text") - .HasColumnName("first_name"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("text") - .HasColumnName("last_name"); - - b.Property("PhoneNumber") - .IsRequired() - .HasColumnType("text") - .HasColumnName("phone_number"); - - b.Property("UserId") - .HasColumnType("integer") - .HasColumnName("userId"); - - b.HasKey("Id"); - - b.HasIndex("UserId") - .IsUnique(); - - b.ToTable("user_basic_info", (string)null); - }); - - modelBuilder.Entity("web_library.Book.Entity.BookCopy", b => - { - b.HasOne("web_library.Book.Entity.Book", "Book") - .WithMany("Copies") - .HasForeignKey("BookId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Book"); - }); - - modelBuilder.Entity("web_library.User.Entity.UserBasicInfo", b => - { - b.HasOne("web_library.User.Entity.User", "User") - .WithOne("UserBasicInfo") - .HasForeignKey("web_library.User.Entity.UserBasicInfo", "UserId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("User"); - }); - - modelBuilder.Entity("web_library.Book.Entity.Book", b => - { - b.Navigation("Copies"); - }); - - modelBuilder.Entity("web_library.User.Entity.User", b => - { - b.Navigation("UserBasicInfo"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs b/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs deleted file mode 100644 index 5e8fc5c..0000000 --- a/web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.cs +++ /dev/null @@ -1,74 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace web_library.Migrations -{ - /// - public partial class userAndUserBasicInfoEntity : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "users", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - email = table.Column(type: "text", nullable: false), - password = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_users", x => x.id); - }); - - migrationBuilder.CreateTable( - name: "user_basic_info", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - user_id = table.Column(type: "integer", nullable: false), - first_name = table.Column(type: "text", nullable: false), - last_name = table.Column(type: "text", nullable: false), - phone_number = table.Column(type: "text", nullable: false), - address = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_user_basic_info", x => x.id); - table.ForeignKey( - name: "FK_user_basic_info_users_user_id", - column: x => x.user_id, - principalTable: "users", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_user_basic_info_user_id", - table: "user_basic_info", - column: "user_id", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_users_email", - table: "users", - column: "email", - unique: true); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "user_basic_info"); - - migrationBuilder.DropTable( - name: "users"); - } - } -} From c40f25a4e52d45db0068312adb157f5caed84073 Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 2 Dec 2023 07:46:04 +0100 Subject: [PATCH 4/8] remove misspelled directory --- .../Genre/Contoller/GenreController.cs | 31 ------------------- .../Genre/Controller/GenresController.cs | 4 --- web-library/web-library.csproj | 2 +- 3 files changed, 1 insertion(+), 36 deletions(-) delete mode 100644 web-library/Genre/Contoller/GenreController.cs diff --git a/web-library/Genre/Contoller/GenreController.cs b/web-library/Genre/Contoller/GenreController.cs deleted file mode 100644 index b1165e3..0000000 --- a/web-library/Genre/Contoller/GenreController.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -namespace web_library.Genre.Contoller -{ - using Service; - using web_library.Genre.Request; - - [Route("api/[controller]")] - [ApiController] - public class GenreController : ControllerBase - { - private readonly IGenreService _genreService; - - public GenreController(IGenreService genreService) - { - _genreService = genreService; - } - - [HttpPost] - public ActionResult Post([FromBody] CreateGenreRequest request) - { - try - { - _genreService.createGenre(request); - - } catch (Exception) { - return NotFound(); - } - return Ok(); - } - } -} diff --git a/web-library/Genre/Controller/GenresController.cs b/web-library/Genre/Controller/GenresController.cs index 112151e..dada424 100644 --- a/web-library/Genre/Controller/GenresController.cs +++ b/web-library/Genre/Controller/GenresController.cs @@ -2,7 +2,6 @@ namespace web_library.Genre.Controller { - using Microsoft.AspNetCore.Authorization; using web_library.Genre.Request; using web_library.Genre.Service; @@ -17,10 +16,7 @@ public GenresController(IGenreService genreService) _genreService = genreService; } - // POST: api/Genres - // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] - [AllowAnonymous] public ActionResult Post([FromBody] CreateGenreRequest request) { try 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 From 344fcf23b72d60f16f40daad6148f3fbe6bde72c Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Fri, 8 Dec 2023 15:27:19 +0100 Subject: [PATCH 5/8] fixes --- web-library/Genre/Controller/GenresController.cs | 4 ++-- web-library/Genre/Repository/GenreRepository.cs | 2 ++ ...31202031627_userAndUserBasicInfoEntity.Designer.cs | 6 ------ web-library/appsettings.Development.json | 11 ----------- 4 files changed, 4 insertions(+), 19 deletions(-) delete mode 100644 web-library/appsettings.Development.json diff --git a/web-library/Genre/Controller/GenresController.cs b/web-library/Genre/Controller/GenresController.cs index dada424..1f72c53 100644 --- a/web-library/Genre/Controller/GenresController.cs +++ b/web-library/Genre/Controller/GenresController.cs @@ -22,13 +22,13 @@ public ActionResult Post([FromBody] CreateGenreRequest request) try { _genreService.createGenre(request); + return Ok(); } catch (Exception) { - return NotFound(); + return BadRequest(); } - return Ok(); } } } diff --git a/web-library/Genre/Repository/GenreRepository.cs b/web-library/Genre/Repository/GenreRepository.cs index 631c97f..0ae66ec 100644 --- a/web-library/Genre/Repository/GenreRepository.cs +++ b/web-library/Genre/Repository/GenreRepository.cs @@ -1,6 +1,8 @@ namespace web_library.Genre.Repository { using Entity; + using web_library.Shared; + public class GenreRepository : IGenreRepository { private readonly DataContext _context; diff --git a/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs b/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs index ab8ecfb..717fa9d 100644 --- a/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs +++ b/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs @@ -11,12 +11,6 @@ namespace web_library.Migrations { - [DbContext(typeof(DataContext))] -<<<<<<<< HEAD:web-library/Migrations/20231202024346_userAndUserBasicInfoEntity.Designer.cs - [Migration("20231202024346_userAndUserBasicInfoEntity")] -======== - [Migration("20231202031627_userAndUserBasicInfoEntity")] ->>>>>>>> 5-endpoint-do-dodawania-ksiązki:web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs partial class userAndUserBasicInfoEntity { /// diff --git a/web-library/appsettings.Development.json b/web-library/appsettings.Development.json deleted file mode 100644 index 43aa661..0000000 --- a/web-library/appsettings.Development.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "ConnectionStrings": { - "WebApiDatabase": "Host=localhost; Database=TinyLib; Username=postgres; Password=kuba" - }, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} From 6cc7c24890f21149b18ad3bee865c94a786d91cd Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 9 Dec 2023 09:20:34 +0100 Subject: [PATCH 6/8] removed unused endpoints --- web-library/Book/Controller/BookController.cs | 42 ++----------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/web-library/Book/Controller/BookController.cs b/web-library/Book/Controller/BookController.cs index aec3eaf..4800894 100644 --- a/web-library/Book/Controller/BookController.cs +++ b/web-library/Book/Controller/BookController.cs @@ -19,55 +19,19 @@ public BookController(IBookService bookService) _bookService = bookService; } - // GET: api/ - [HttpGet] - public ActionResult> Get() - { - return Ok(); - } - - // GET api//5 - [HttpGet("{id}")] - public ActionResult Get(int id) - { - - return Ok(); - } - - // 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 ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request) { - try - { - request.book_id = id; - _bookService.assigneGenre(request); - } catch (Exception) - { - return NotFound(); - } + request.book_id = id; + _bookService.assigneGenre(request); return Ok(); } - - // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } } } From 3f91dcbe03af94e59ba7b60c8163915dfbdaee85 Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sat, 9 Dec 2023 09:46:51 +0100 Subject: [PATCH 7/8] fix migration --- web-library/Book/Service/BookService.cs | 8 ++++---- .../20231202031627_userAndUserBasicInfoEntity.Designer.cs | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/web-library/Book/Service/BookService.cs b/web-library/Book/Service/BookService.cs index 9465d53..8a5b2ef 100644 --- a/web-library/Book/Service/BookService.cs +++ b/web-library/Book/Service/BookService.cs @@ -34,10 +34,10 @@ public BookService(IBookRepository bookRepository, IBookCopyRepository bookCopyR [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); diff --git a/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs b/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs index 717fa9d..768949e 100644 --- a/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs +++ b/web-library/Migrations/20231202031627_userAndUserBasicInfoEntity.Designer.cs @@ -11,6 +11,8 @@ namespace web_library.Migrations { + [DbContext(typeof(DataContext))] + [Migration("20231202031627_userAndUserBasicInfoEntity")] partial class userAndUserBasicInfoEntity { /// From 3b9770027a7ba231c8722bd5f9cbb86632fbfc06 Mon Sep 17 00:00:00 2001 From: kmaliszewski Date: Sun, 17 Dec 2023 16:37:30 +0100 Subject: [PATCH 8/8] fixes --- web-library/Book/Controller/BookController.cs | 3 +-- web-library/Book/Service/BookService.cs | 4 ++-- web-library/Book/Service/IBookService.cs | 2 +- web-library/Genre/Repository/GenreRepository.cs | 2 -- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/web-library/Book/Controller/BookController.cs b/web-library/Book/Controller/BookController.cs index 13d8de0..f9fca01 100644 --- a/web-library/Book/Controller/BookController.cs +++ b/web-library/Book/Controller/BookController.cs @@ -31,8 +31,7 @@ public ActionResult Post([FromBody] CreateBookRequest request) [HttpPut("{id}")] public ActionResult Put(int id, [FromBody] AssigneGenreToBookRequest request) { - request.book_id = id; - _bookService.assigneGenre(request); + _bookService.assigneGenre(id,request); return Ok(); } } diff --git a/web-library/Book/Service/BookService.cs b/web-library/Book/Service/BookService.cs index 8a5b2ef..2aa34a3 100644 --- a/web-library/Book/Service/BookService.cs +++ b/web-library/Book/Service/BookService.cs @@ -53,9 +53,9 @@ public void createBook(CreateBookRequest request) _bookRepository.Update(entity); } - public void assigneGenre(AssigneGenreToBookRequest request) + public void assigneGenre(int bookId, AssigneGenreToBookRequest request) { - Book? book = _bookRepository.GetByIdOrThrow(request.book_id); + Book? book = _bookRepository.GetByIdOrThrow(bookId); foreach (var id in request.genres_ids) { Genre? genre = _genreRepository.GetByIdOrThrow(id); diff --git a/web-library/Book/Service/IBookService.cs b/web-library/Book/Service/IBookService.cs index 41ddd37..d53385d 100644 --- a/web-library/Book/Service/IBookService.cs +++ b/web-library/Book/Service/IBookService.cs @@ -5,6 +5,6 @@ namespace web_library.Book.Service public interface IBookService { void createBook(CreateBookRequest request); - void assigneGenre(AssigneGenreToBookRequest request); + void assigneGenre(int bookId, AssigneGenreToBookRequest request); } } \ No newline at end of file diff --git a/web-library/Genre/Repository/GenreRepository.cs b/web-library/Genre/Repository/GenreRepository.cs index 23014f6..b113f34 100644 --- a/web-library/Genre/Repository/GenreRepository.cs +++ b/web-library/Genre/Repository/GenreRepository.cs @@ -14,7 +14,6 @@ public GenreRepository(DataContext context) public void Add(Genre entity) { _context.Genres.Add(entity); - _context.Add(entity); _context.SaveChanges(); } @@ -27,7 +26,6 @@ public Genre GetByIdOrThrow(int id) { Genre? genre = _context.Genres.Find(id) ?? throw new NotFoundException("Genre "+id+" not found in repository"); return genre; - throw new NotImplementedException(); } public void Remove(Genre entity)