Skip to content
Merged
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
58 changes: 58 additions & 0 deletions LibraryManagement.Api/Controllers/ReadersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//-----------------------------------------------------------
// Copyright (c) Coalition of Good-Hearted Engineers
// Free To Use To Build Reliable Library Management Solutions
//-----------------------------------------------------------

using LibraryManagement.Api.Models.Foundations.Readers;
using LibraryManagement.Api.Models.Foundations.Readers.Exceptions;
using LibraryManagement.Api.Services.Foundations.Readers;
using Microsoft.AspNetCore.Mvc;
using RESTFulSense.Controllers;

namespace LibraryManagement.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ReadersController : RESTFulController
{
private readonly IReaderService readerService;

public ReadersController(IReaderService readerService)
{
this.readerService = readerService;
}

[HttpPost]
public async ValueTask<ActionResult<Reader>> PostReaderAsync(Reader reader)
{
try
{
Reader postedReader =
await this.readerService.AddReaderAsync(reader);

return Created(postedReader);
}
catch (ReaderValidationException readerValidationException)
{
return BadRequest(readerValidationException.InnerException);
}
catch (ReaderDependencyValidationException readerDependencyValidationException)
when (readerDependencyValidationException.InnerException is AlreadyExistsReaderException)
{
return Conflict(readerDependencyValidationException.InnerException);
}
catch (ReaderDependencyValidationException readerDependencyValidationException)
{
return BadRequest(readerDependencyValidationException.InnerException);
}
catch (ReaderDependencyException readerDependencyException)
{
return InternalServerError(readerDependencyException.InnerException);
}
catch (ReaderServiceException readerServiceException)
{
return InternalServerError(readerServiceException.InnerException);
}
}
}
}
6 changes: 5 additions & 1 deletion LibraryManagement.Api/Models/Foundations/Readers/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Free To Use To Build Reliable Library Management Solutions
//-----------------------------------------------------------

using System.Text.Json.Serialization;
using LibraryManagement.Api.Models.Foundations.Books;

namespace LibraryManagement.Api.Models.Foundations.Readers
Expand All @@ -10,9 +11,12 @@
public class Reader
{
public Guid ReaderId { get; set; }
public string FirstName { get; set; }

Check warning on line 14 in LibraryManagement.Api/Models/Foundations/Readers/Reader.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'FirstName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string LastName { get; set; }

Check warning on line 15 in LibraryManagement.Api/Models/Foundations/Readers/Reader.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'LastName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public DateTimeOffset DateOfBirth { get; set; }
public List<Book> Books { get; set; }

[JsonIgnore]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public List<Book> Books { get; set; } = new();
}
}
2 changes: 2 additions & 0 deletions LibraryManagement.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using LibraryManagement.Api.Brokers.Loggings;
using LibraryManagement.Api.Brokers.Storages;
using LibraryManagement.Api.Services.Foundations.Books;
using LibraryManagement.Api.Services.Foundations.Readers;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -20,6 +21,7 @@
builder.Services.AddTransient<IStorageBroker, StorageBroker>();
builder.Services.AddTransient<ILoggingBroker, LoggingBroker>();
builder.Services.AddTransient<IBookService, BookService>();
builder.Services.AddTransient<IReaderService, ReaderService>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

Expand Down
Loading