Skip to content

Commit a712282

Browse files
Merge pull request #21 from DilmurodDeveloper/users/DilmurodDeveloper/exposers-book-post
EXPOSERS: Post Book
2 parents 18ff5c0 + 4f324c1 commit a712282

File tree

9 files changed

+222
-22
lines changed

9 files changed

+222
-22
lines changed

LibraryManagement.Api.Tests.Unit/Services/Foundations/Books/BookServiceTests.Validations.Add.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public async Task ShouldThrowValidationExceptionOnAddIfBookIsInvalidAndLogItAsyn
6060
key: nameof(Book.BookId),
6161
values: "Id is required");
6262

63-
invalidBookException.AddData(
64-
key: nameof(Book.ReaderId),
65-
values: "Id is required");
66-
6763
invalidBookException.AddData(
6864
key: nameof(Book.BookTitle),
6965
values: "Text is required");

LibraryManagement.Api/Brokers/Storages/StorageBroker.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//-----------------------------------------------------------
55

66
using EFxceptions;
7-
using LibraryManagement.Api.Models.Foundations.Books;
87
using Microsoft.EntityFrameworkCore;
98

109
namespace LibraryManagement.Api.Brokers.Storages
@@ -36,16 +35,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3635
optionsBuilder.UseSqlServer(connectionString);
3736
}
3837

39-
protected override void OnModelCreating(ModelBuilder modelBuilder)
40-
{
41-
modelBuilder.Entity<Book>()
42-
.HasOne(b => b.Reader)
43-
.WithMany(r => r.Books)
44-
.HasForeignKey(b => b.ReaderId);
45-
46-
base.OnModelCreating(modelBuilder);
47-
}
48-
4938
public override void Dispose() { }
5039
}
5140
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//-----------------------------------------------------------
2+
// Copyright (c) Coalition of Good-Hearted Engineers
3+
// Free To Use To Build Reliable Library Management Solutions
4+
//-----------------------------------------------------------
5+
6+
using LibraryManagement.Api.Models.Foundations.Books;
7+
using LibraryManagement.Api.Models.Foundations.Books.Exceptions;
8+
using LibraryManagement.Api.Services.Foundations.Books;
9+
using Microsoft.AspNetCore.Mvc;
10+
using RESTFulSense.Controllers;
11+
12+
namespace LibraryManagement.Api.Controllers
13+
{
14+
[ApiController]
15+
[Route("api/[controller]")]
16+
public class BooksController : RESTFulController
17+
{
18+
private readonly IBookService bookService;
19+
20+
public BooksController(IBookService bookService)
21+
{
22+
this.bookService = bookService;
23+
}
24+
25+
[HttpPost]
26+
public async ValueTask<ActionResult<Book>> PostBookAsync(Book book)
27+
{
28+
try
29+
{
30+
Book postedBook = await this.bookService.AddBookAsync(book);
31+
32+
return Created(postedBook);
33+
}
34+
catch (BookValidationException bookValidationException)
35+
{
36+
return BadRequest(bookValidationException.InnerException);
37+
}
38+
catch (BookDependencyValidationException bookDependencyValidationException)
39+
when (bookDependencyValidationException.InnerException is AlreadyExistsBookException)
40+
{
41+
return Conflict(bookDependencyValidationException.InnerException);
42+
}
43+
catch (BookDependencyValidationException bookDependencyValidationException)
44+
{
45+
return BadRequest(bookDependencyValidationException.InnerException);
46+
}
47+
catch (BookDependencyException bookDependencyException)
48+
{
49+
return InternalServerError(bookDependencyException.InnerException);
50+
}
51+
catch (BookServiceException bookServiceException)
52+
{
53+
return InternalServerError(bookServiceException.InnerException);
54+
}
55+
}
56+
}
57+
}

LibraryManagement.Api/Migrations/20250720142803_MakeReaderNullableInBook.Designer.cs

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace LibraryManagement.Api.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class MakeReaderNullableInBook : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.DropForeignKey(
14+
name: "FK_Books_Readers_ReaderId",
15+
table: "Books");
16+
17+
migrationBuilder.AlterColumn<Guid>(
18+
name: "ReaderId",
19+
table: "Books",
20+
type: "uniqueidentifier",
21+
nullable: true,
22+
oldClrType: typeof(Guid),
23+
oldType: "uniqueidentifier");
24+
25+
migrationBuilder.AddForeignKey(
26+
name: "FK_Books_Readers_ReaderId",
27+
table: "Books",
28+
column: "ReaderId",
29+
principalTable: "Readers",
30+
principalColumn: "ReaderId");
31+
}
32+
33+
/// <inheritdoc />
34+
protected override void Down(MigrationBuilder migrationBuilder)
35+
{
36+
migrationBuilder.DropForeignKey(
37+
name: "FK_Books_Readers_ReaderId",
38+
table: "Books");
39+
40+
migrationBuilder.AlterColumn<Guid>(
41+
name: "ReaderId",
42+
table: "Books",
43+
type: "uniqueidentifier",
44+
nullable: false,
45+
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
46+
oldClrType: typeof(Guid),
47+
oldType: "uniqueidentifier",
48+
oldNullable: true);
49+
50+
migrationBuilder.AddForeignKey(
51+
name: "FK_Books_Readers_ReaderId",
52+
table: "Books",
53+
column: "ReaderId",
54+
principalTable: "Readers",
55+
principalColumn: "ReaderId",
56+
onDelete: ReferentialAction.Cascade);
57+
}
58+
}
59+
}

LibraryManagement.Api/Migrations/StorageBrokerModelSnapshot.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4040
.IsRequired()
4141
.HasColumnType("nvarchar(max)");
4242

43-
b.Property<Guid>("ReaderId")
43+
b.Property<Guid?>("ReaderId")
4444
.HasColumnType("uniqueidentifier");
4545

4646
b.HasKey("BookId");
@@ -76,9 +76,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7676
{
7777
b.HasOne("LibraryManagement.Api.Models.Foundations.Readers.Reader", "Reader")
7878
.WithMany("Books")
79-
.HasForeignKey("ReaderId")
80-
.OnDelete(DeleteBehavior.Cascade)
81-
.IsRequired();
79+
.HasForeignKey("ReaderId");
8280

8381
b.Navigation("Reader");
8482
});

LibraryManagement.Api/Models/Foundations/Books/Book.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
// Free To Use To Build Reliable Library Management Solutions
44
//-----------------------------------------------------------
55

6+
using System.Text.Json.Serialization;
67
using LibraryManagement.Api.Models.Foundations.Readers;
78

89
namespace LibraryManagement.Api.Models.Foundations.Books
910
{
1011
public class Book
1112
{
1213
public Guid BookId { get; set; }
13-
public Guid ReaderId { get; set; }
1414
public string BookTitle { get; set; }
1515
public string Author { get; set; }
1616
public string Genre { get; set; }
17-
public Reader Reader { get; set; }
17+
[JsonIgnore]
18+
public Guid? ReaderId { get; set; }
19+
[JsonIgnore]
20+
public Reader? Reader { get; set; }
1821
}
1922
}

LibraryManagement.Api/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// Free To Use To Build Reliable Library Management Solutions
44
//-----------------------------------------------------------
55

6+
using LibraryManagement.Api.Brokers.Loggings;
67
using LibraryManagement.Api.Brokers.Storages;
8+
using LibraryManagement.Api.Services.Foundations.Books;
79
using Microsoft.OpenApi.Models;
810

911
var builder = WebApplication.CreateBuilder(args);
@@ -15,6 +17,9 @@
1517
};
1618

1719
builder.Services.AddDbContext<StorageBroker>();
20+
builder.Services.AddTransient<IStorageBroker, StorageBroker>();
21+
builder.Services.AddTransient<ILoggingBroker, LoggingBroker>();
22+
builder.Services.AddTransient<IBookService, BookService>();
1823
builder.Services.AddControllers();
1924
builder.Services.AddEndpointsApiExplorer();
2025

LibraryManagement.Api/Services/Foundations/Books/BookService.Validations.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ private void ValidateBookOnAdd(Book book)
1616

1717
Validate(
1818
(Rule: IsInvalid(book.BookId), Parameter: nameof(Book.BookId)),
19-
(Rule: IsInvalid(book.ReaderId), Parameter: nameof(Book.ReaderId)),
2019
(Rule: IsInvalid(book.BookTitle), Parameter: nameof(Book.BookTitle)),
2120
(Rule: IsInvalid(book.Author), Parameter: nameof(Book.Author)),
2221
(Rule: IsInvalid(book.Genre), Parameter: nameof(Book.Genre)));

0 commit comments

Comments
 (0)