diff --git a/Joseph/StageThreeTask/Library Management System/Controllers/AuthorController.cs b/Joseph/StageThreeTask/Library Management System/Controllers/AuthorController.cs new file mode 100644 index 0000000..828d524 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Controllers/AuthorController.cs @@ -0,0 +1,77 @@ +using AutoMapper; +using Library_Management_System.Models.Dtos; +using Library_Management_System.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Library_Management_System.Controllers +{ + [Route("api")] + [ApiController] + public class AuthorController : ControllerBase + { + private readonly IAuthor _author; + private readonly IMapper _mapper; + public AuthorController(IAuthor author, IMapper mapper) + { + _author = author; + _mapper = mapper; + } + + + //Create + [HttpPost("author/create")] + public ActionResult Create([FromBody]AuthorForCreationDto author) + { + //Author bookAuthor = _mapper.Map(author); + + if (author == null) { return BadRequest(); } + var status = _author.CreateAuthor(author); + return Ok(status); + } + + //GetAllAuthors + [HttpGet("author/getAll")] + public ActionResult GetAllAuthors() + { + var allAuthors = _author.GetAllAuthors(); + + //return Ok(_mapper.Map(allAuthors)); + List allAuthorsDto = new List(); + foreach (var authors in allAuthors) + { + allAuthorsDto.Add(new AuthorForDisplayDto() + { + AuthorDetails = authors.AuthorDetails, + AuthorName = authors.AuthorName, + //Publisher= authors.Publisher, + Books = authors.Books, + }); + } + return Ok(allAuthorsDto); + } + + //GetAnAuthor + [HttpGet("author/getAuthor")] + public ActionResult GetAnAuthor(string authorName) + { + if (authorName == null) { return BadRequest(); } + var authors = _author.GetAuthor(authorName); + if (authors == null) + { + return Ok("No author not found"); + } + + return Ok(authors); + } + + //GetBooksAttachedToAnAuthor + [HttpGet("author/getBooksByAuthor")] + public ActionResult GetBooksAttachedToAnAuthor(string authorName) + { + if (authorName == null) { return BadRequest(); } + var books = _author.GetAllBooksByAuthor(authorName); + return Ok(books); + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Controllers/BookController.cs b/Joseph/StageThreeTask/Library Management System/Controllers/BookController.cs new file mode 100644 index 0000000..3213f09 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Controllers/BookController.cs @@ -0,0 +1,53 @@ +using AutoMapper; +using Library_Management_System.Models.Dtos; +using Library_Management_System.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Library_Management_System.Controllers +{ + [Route("api")] + [ApiController] + public class BookController : ControllerBase + { + private readonly IBook _book; + private readonly IMapper _mapper; + public BookController(IBook book, IMapper mapper) + { + _book = book; + _mapper = mapper; + } + + // Create + [HttpPost("create-book")] + public ActionResult Create(BookForCreationDto bookDto) + { + if (bookDto == null) + { + return BadRequest(); + } + var book = _book.CreateBook(bookDto); + return Ok(book); + } + + // GetABook + [HttpGet("get-book")] + public ActionResult GetABook(string bookName) + { + if (bookName == null) + { + return BadRequest(); + } + var book = _book.GetABook(bookName); + return Ok(book); + } + + // GetAllBooks + [HttpGet("books")] + public ActionResult GetAllBooks() + { + var books = _book.GetAllBooks(); + return Ok(books); + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Controllers/PublisherController.cs b/Joseph/StageThreeTask/Library Management System/Controllers/PublisherController.cs new file mode 100644 index 0000000..11524ce --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Controllers/PublisherController.cs @@ -0,0 +1,67 @@ +using AutoMapper; +using Library_Management_System.Models.Dtos; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Library_Management_System.Controllers +{ + [Route("api")] + [ApiController] + public class PublisherController : ControllerBase + { + private readonly IPublisher _publisher; + private readonly IMapper _mapper; + public PublisherController(IPublisher publisher, IMapper mapper) + { + _publisher = publisher; + _mapper = mapper; + } + + /// + /// Endpoint creates a new publisher + /// + /// An object representing a publisher + /// 200(Success) or 400(BadRequest) + [HttpPost("publisher/create")] + public ActionResult Create(PublisherForCreation publisherDto) + { + + if (publisherDto == null) { return BadRequest(); } + _publisher.CreatePublisher(publisherDto); + return Ok("Publisher created successfully"); + } + + //Get A Publisher + [HttpGet("publisher/getpublisher")] + public ActionResult GetAPublisher(string publisherName) + { + if (publisherName == null) + { + return BadRequest(); + } + var publisher = _publisher.GetAPublisher(publisherName); + return Ok(publisher); + } + + //GetAllPublishers + [HttpGet("publisher/getAllPublisher")] + public ActionResult GetAllPublishers() + { + var publishers = _publisher.GetAllPublishers(); + return Ok(publishers); + } + + //GetAuthorsAttachedToAPublisher + [HttpGet("publisher/getAuthorsByPublishers")] + public ActionResult GetAuthorsAttachedToAPublisher(string publisherName) + { + if (publisherName == null) + { + return BadRequest(); + } + var authors = _publisher.GetAuthorsAttachedToAPublisher(publisherName); + return Ok(authors); + } + } + +} diff --git a/Joseph/StageThreeTask/Library Management System/Data/ApplicationDbContext.cs b/Joseph/StageThreeTask/Library Management System/Data/ApplicationDbContext.cs new file mode 100644 index 0000000..e669986 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Data/ApplicationDbContext.cs @@ -0,0 +1,29 @@ +using Library_Management_System.Entities; + +namespace Library_Management_System.Data +{ + public class ApplicationDbContext: DbContext + { + public ApplicationDbContext(DbContextOptions options): base(options) { } + + + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasOne(book => book.Book) + .WithMany(book_author => book_author.Book_Authors) + .HasForeignKey(book_id => book_id.BookId); + + modelBuilder.Entity() + .HasOne(author => author.Author) + .WithMany(book_author => book_author.Book_Authors) + .HasForeignKey(author_id => author_id.AuthorId); + } + + public DbSet Publishers { get; set; } + public DbSet Authors { get; set; } + public DbSet Books { get; set; } + public DbSet Book_Authors { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Entities/Author.cs b/Joseph/StageThreeTask/Library Management System/Entities/Author.cs new file mode 100644 index 0000000..85e1d42 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Entities/Author.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace Library_Management_System.Entities +{ + public class Author + { + [Key] + public int AuthorId { get; set; } + [Required] + public string AuthorName { get; set; } + + [Required] + [MaxLength(100)] + public string AuthorDetails { get; set; } + public List Book_Authors { get; set; } + public int PublisherId { get; set; } + public Publisher Publisher { get; set; } + + + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Entities/Book.cs b/Joseph/StageThreeTask/Library Management System/Entities/Book.cs new file mode 100644 index 0000000..e73102a --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Entities/Book.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace Library_Management_System.Entities +{ + public class Book + { + [Key] + public int BookId { get; set; } + + public string Title { get; set; } + + [MaxLength(150)] + public string Description { get; set; } + + [Required] + public string ISBNNumber { get; set; } + + public string Version { get; set; } + + public List Book_Authors { get; set; } + + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Entities/Book_Author.cs b/Joseph/StageThreeTask/Library Management System/Entities/Book_Author.cs new file mode 100644 index 0000000..4d9515c --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Entities/Book_Author.cs @@ -0,0 +1,12 @@ +namespace Library_Management_System.Entities +{ + public class Book_Author + { + public int Id { get; set; } + public int BookId { get; set; } + public Book Book { get; set; } + + public int AuthorId { get; set; } + public Author Author { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Entities/Publisher.cs b/Joseph/StageThreeTask/Library Management System/Entities/Publisher.cs new file mode 100644 index 0000000..b6d3949 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Entities/Publisher.cs @@ -0,0 +1,21 @@ +namespace Library_Management_System.Entities +{ + public class Publisher + { + [Key] + public int PublisherId { get; set; } + + [Required] + public string PublisherName { get; set; } + + [Required] + [MaxLength(250)] + public string CopyRightLicense { get; set; } + + [Required] + public string PublisherAddress { get; set; } + + + public List Authors{ get; set; } = new List(); + } +} diff --git a/Joseph/StageThreeTask/Library Management System/GlobalUsing.cs b/Joseph/StageThreeTask/Library Management System/GlobalUsing.cs new file mode 100644 index 0000000..a5160fe --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/GlobalUsing.cs @@ -0,0 +1,5 @@ +global using Microsoft.EntityFrameworkCore; +global using System.ComponentModel.DataAnnotations; +global using Library_Management_System.Entities; +global using Library_Management_System.Services; +global using Library_Management_System.Models.Dtos; diff --git a/Joseph/StageThreeTask/Library Management System/Library Management System.csproj b/Joseph/StageThreeTask/Library Management System/Library Management System.csproj new file mode 100644 index 0000000..36fab79 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Library Management System.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + enable + Library_Management_System + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/Joseph/StageThreeTask/Library Management System/Library Management System.sln b/Joseph/StageThreeTask/Library Management System/Library Management System.sln new file mode 100644 index 0000000..3757298 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Library Management System.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library Management System", "Library Management System.csproj", "{BAA55578-8C10-47D8-8551-D99071DD4D3A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BAA55578-8C10-47D8-8551-D99071DD4D3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAA55578-8C10-47D8-8551-D99071DD4D3A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAA55578-8C10-47D8-8551-D99071DD4D3A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAA55578-8C10-47D8-8551-D99071DD4D3A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D5372C8E-773E-4F5A-A7CF-BB441F2F6028} + EndGlobalSection +EndGlobal diff --git a/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.Designer.cs b/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.Designer.cs new file mode 100644 index 0000000..8539672 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.Designer.cs @@ -0,0 +1,179 @@ +// +using Library_Management_System.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Library_Management_System.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20230312015015_InitialMigrations")] + partial class InitialMigrations + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.Property("AuthorId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("AuthorId"), 1L, 1); + + b.Property("AuthorDetails") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("AuthorName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PublisherId") + .HasColumnType("int"); + + b.HasKey("AuthorId"); + + b.HasIndex("PublisherId"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book", b => + { + b.Property("BookId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("BookId"), 1L, 1); + + b.Property("Description") + .IsRequired() + .HasMaxLength(150) + .HasColumnType("nvarchar(150)"); + + b.Property("ISBNNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Version") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("BookId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book_Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("AuthorId") + .HasColumnType("int"); + + b.Property("BookId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.HasIndex("BookId"); + + b.ToTable("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Publisher", b => + { + b.Property("PublisherId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("PublisherId"), 1L, 1); + + b.Property("CopyRightLicense") + .IsRequired() + .HasMaxLength(250) + .HasColumnType("nvarchar(250)"); + + b.Property("PublisherAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PublisherName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("PublisherId"); + + b.ToTable("Publishers"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.HasOne("Library_Management_System.Entities.Publisher", "Publisher") + .WithMany("Authors") + .HasForeignKey("PublisherId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Publisher"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book_Author", b => + { + b.HasOne("Library_Management_System.Entities.Author", "Author") + .WithMany("Book_Authors") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Library_Management_System.Entities.Book", "Book") + .WithMany("Book_Authors") + .HasForeignKey("BookId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + + b.Navigation("Book"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.Navigation("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book", b => + { + b.Navigation("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Publisher", b => + { + b.Navigation("Authors"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.cs b/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.cs new file mode 100644 index 0000000..5e1e112 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Migrations/20230312015015_InitialMigrations.cs @@ -0,0 +1,120 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Library_Management_System.Migrations +{ + public partial class InitialMigrations : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Books", + columns: table => new + { + BookId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Title = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(150)", maxLength: 150, nullable: false), + ISBNNumber = table.Column(type: "nvarchar(max)", nullable: false), + Version = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Books", x => x.BookId); + }); + + migrationBuilder.CreateTable( + name: "Publishers", + columns: table => new + { + PublisherId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PublisherName = table.Column(type: "nvarchar(max)", nullable: false), + CopyRightLicense = table.Column(type: "nvarchar(250)", maxLength: 250, nullable: false), + PublisherAddress = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Publishers", x => x.PublisherId); + }); + + migrationBuilder.CreateTable( + name: "Authors", + columns: table => new + { + AuthorId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + AuthorName = table.Column(type: "nvarchar(max)", nullable: false), + AuthorDetails = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), + PublisherId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Authors", x => x.AuthorId); + table.ForeignKey( + name: "FK_Authors_Publishers_PublisherId", + column: x => x.PublisherId, + principalTable: "Publishers", + principalColumn: "PublisherId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Book_Authors", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BookId = table.Column(type: "int", nullable: false), + AuthorId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Book_Authors", x => x.Id); + table.ForeignKey( + name: "FK_Book_Authors_Authors_AuthorId", + column: x => x.AuthorId, + principalTable: "Authors", + principalColumn: "AuthorId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Book_Authors_Books_BookId", + column: x => x.BookId, + principalTable: "Books", + principalColumn: "BookId", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Authors_PublisherId", + table: "Authors", + column: "PublisherId"); + + migrationBuilder.CreateIndex( + name: "IX_Book_Authors_AuthorId", + table: "Book_Authors", + column: "AuthorId"); + + migrationBuilder.CreateIndex( + name: "IX_Book_Authors_BookId", + table: "Book_Authors", + column: "BookId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Book_Authors"); + + migrationBuilder.DropTable( + name: "Authors"); + + migrationBuilder.DropTable( + name: "Books"); + + migrationBuilder.DropTable( + name: "Publishers"); + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Migrations/ApplicationDbContextModelSnapshot.cs b/Joseph/StageThreeTask/Library Management System/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..66e1c83 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,177 @@ +// +using Library_Management_System.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Library_Management_System.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.Property("AuthorId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("AuthorId"), 1L, 1); + + b.Property("AuthorDetails") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("AuthorName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PublisherId") + .HasColumnType("int"); + + b.HasKey("AuthorId"); + + b.HasIndex("PublisherId"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book", b => + { + b.Property("BookId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("BookId"), 1L, 1); + + b.Property("Description") + .IsRequired() + .HasMaxLength(150) + .HasColumnType("nvarchar(150)"); + + b.Property("ISBNNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Version") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("BookId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book_Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("AuthorId") + .HasColumnType("int"); + + b.Property("BookId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.HasIndex("BookId"); + + b.ToTable("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Publisher", b => + { + b.Property("PublisherId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("PublisherId"), 1L, 1); + + b.Property("CopyRightLicense") + .IsRequired() + .HasMaxLength(250) + .HasColumnType("nvarchar(250)"); + + b.Property("PublisherAddress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PublisherName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("PublisherId"); + + b.ToTable("Publishers"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.HasOne("Library_Management_System.Entities.Publisher", "Publisher") + .WithMany("Authors") + .HasForeignKey("PublisherId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Publisher"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book_Author", b => + { + b.HasOne("Library_Management_System.Entities.Author", "Author") + .WithMany("Book_Authors") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Library_Management_System.Entities.Book", "Book") + .WithMany("Book_Authors") + .HasForeignKey("BookId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + + b.Navigation("Book"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Author", b => + { + b.Navigation("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Book", b => + { + b.Navigation("Book_Authors"); + }); + + modelBuilder.Entity("Library_Management_System.Entities.Publisher", b => + { + b.Navigation("Authors"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForBookCreationDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForBookCreationDto.cs new file mode 100644 index 0000000..09959fa --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForBookCreationDto.cs @@ -0,0 +1,9 @@ +namespace Library_Management_System.Models.Dtos +{ + public class AuthorForBookCreationDto + { + [Required] + public string AuthorName { get; set; } + + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForCreationDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForCreationDto.cs new file mode 100644 index 0000000..f52fa91 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForCreationDto.cs @@ -0,0 +1,18 @@ +namespace Library_Management_System.Models.Dtos +{ + /// + /// Dto used for author creation + /// + public class AuthorForCreationDto + { + [Required] + public string AuthorName { get; set; } + + [Required] + [MaxLength(100)] + public string AuthorDetails { get; set; } + + public PublisherForCreation Publisher { get; set; } + + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForDisplayDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForDisplayDto.cs new file mode 100644 index 0000000..fa62e62 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/AuthorForDisplayDto.cs @@ -0,0 +1,13 @@ +namespace Library_Management_System.Models.Dtos +{ + public class AuthorForDisplayDto + { + public string AuthorName { get; set; } + + public string AuthorDetails { get; set; } + + public List Books { get; set; } + //public Publisher Publisher { get; set; } + + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForAuthorDIsplayDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForAuthorDIsplayDto.cs new file mode 100644 index 0000000..7d9a254 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForAuthorDIsplayDto.cs @@ -0,0 +1,16 @@ +namespace Library_Management_System.Models.Dtos +{ + /// + /// Dto used for displaying books by authors when an author details are to be displayed + /// + public class BookForAuthorDIsplayDto + { + public string Title { get; set; } + + public string Description { get; set; } + + public string ISBNNumber { get; set; } + + public string Version { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForCreationDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForCreationDto.cs new file mode 100644 index 0000000..3b4f244 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForCreationDto.cs @@ -0,0 +1,18 @@ +namespace Library_Management_System.Models.Dtos +{ + public class BookForCreationDto + { + public string Title { get; set; } + + [MaxLength(150)] + public string Description { get; set; } + + [Required] + public string ISBNNumber { get; set; } + + public string Version { get; set; } + + public List Authors { get; set; } + public string Publisher { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForDisplayDto.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForDisplayDto.cs new file mode 100644 index 0000000..77a6e65 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/BookForDisplayDto.cs @@ -0,0 +1,18 @@ +namespace Library_Management_System.Models.Dtos +{ + /// + /// Dto used for display of book when using any book endpoints + /// + public class BookForDisplayDto + { + public string Title { get; set; } + + public string Description { get; set; } + + public string ISBNNumber { get; set; } + + public string Version { get; set; } + + public List Authors { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForCreation.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForCreation.cs new file mode 100644 index 0000000..e9f4b22 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForCreation.cs @@ -0,0 +1,19 @@ +namespace Library_Management_System.Models.Dtos +{ + /// + /// Dto used when creating a publisher + /// + public class PublisherForCreation + { + + [Required] + public string PublisherName { get; set; } + + [Required] + [MaxLength(250)] + public string CopyRightLicense { get; set; } + + [Required] + public string PublisherAddress { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForDisplay.cs b/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForDisplay.cs new file mode 100644 index 0000000..fe4f88e --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Models/Dtos/PublisherForDisplay.cs @@ -0,0 +1,12 @@ +namespace Library_Management_System.Models.Dtos +{ + /// + /// Dto used when displaying details of a publisher + /// + public class PublisherForDisplay + { + public string PublisherName { get; set; } + + public string PublisherAddress { get; set; } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Program.cs b/Joseph/StageThreeTask/Library Management System/Program.cs new file mode 100644 index 0000000..05ffbb4 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Program.cs @@ -0,0 +1,34 @@ +using Library_Management_System.Data; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); + +builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Joseph/StageThreeTask/Library Management System/Properties/launchSettings.json b/Joseph/StageThreeTask/Library Management System/Properties/launchSettings.json new file mode 100644 index 0000000..6c066ee --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51021", + "sslPort": 44357 + } + }, + "profiles": { + "Library_Management_System": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7199;http://localhost:5045", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/AuthorService.cs b/Joseph/StageThreeTask/Library Management System/Services/AuthorService.cs new file mode 100644 index 0000000..ed2d691 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/AuthorService.cs @@ -0,0 +1,138 @@ +using Library_Management_System.Data; +using Library_Management_System.Entities; +using System.Linq; + +namespace Library_Management_System.Services +{ + /// + /// Provides implementations for the IAuthor interface + /// + public class AuthorService : IAuthor + { + public readonly ApplicationDbContext _context; + + public AuthorService(ApplicationDbContext context) + { + _context = context; + } + + public string CreateAuthor(AuthorForCreationDto author) + { + + + var publisherName = author.Publisher.PublisherName; + var publisher = _context.Publishers.FirstOrDefault(x=>x.PublisherName.ToLower()== publisherName.ToLower()); + if (publisher != null) + { + var bookAuthor = new Author() + { + AuthorDetails = author.AuthorDetails, + AuthorName = author.AuthorName, + Publisher = publisher, + }; + + _context.Authors.Add(bookAuthor); + _context.SaveChanges(); + return "Author created successfully"; + } + else + { + return "Publisher Not Found"; + } + } + + public IEnumerable GetAllAuthors() + { + + var authorlist = _context.Authors.Select(author => author); + if (authorlist.Count() <= 0) + { + return Enumerable.Empty(); + } + var authors = new List(); + foreach (var author in authorlist) + { + var books_id = _context.Book_Authors.Where(a => a.AuthorId == author.AuthorId).Select(b => b.BookId); + var books = new List(); + foreach (var book_id in books_id) + { + var book = _context.Books.FirstOrDefault(b => b.BookId == book_id); + books.Add(new BookForAuthorDIsplayDto + { + Title = book.Title, + Description= book.Description, + ISBNNumber= book.ISBNNumber, + Version = book.Version + }); + } + authors.Add(new AuthorForDisplayDto + { + AuthorDetails = author.AuthorDetails, + AuthorName = author.AuthorName, + Books = books, + }); + } + return authors; + } + + public IEnumerable GetAllBooksByAuthor(string authorName) + { + var author = _context.Authors. + FirstOrDefault(x => x.AuthorName == authorName); + + if (author != null) + { + var books = new List(); + var books_id = _context.Book_Authors.Where(a => a.AuthorId == author.AuthorId).Select(b => b.BookId); + foreach (var book_id in books_id) + { + var book = _context.Books.FirstOrDefault(x => x.BookId == book_id); + books.Add(new BookForAuthorDIsplayDto + { + Title = book.Title, + Description = book.Description, + ISBNNumber = book.ISBNNumber, + Version = book.Version + }); + + } + return books; + } + return Enumerable.Empty(); + } + + public List GetAuthor(string authorName) + { + var author = _context.Authors. + FirstOrDefault(x => x.AuthorName == authorName); + + if (author != null) + { + var authorDto = new List(); + + var books_id = _context.Book_Authors.Where(a => a.AuthorId == author.AuthorId).Select(b => b.BookId); + var books = new List(); + foreach (var book_id in books_id) + { + var book = _context.Books.FirstOrDefault(b => b.BookId == book_id); + books.Add(new BookForAuthorDIsplayDto + { + Title = book.Title, + Description = book.Description, + ISBNNumber = book.ISBNNumber, + Version = book.Version + }); + } + + authorDto.Add(new AuthorForDisplayDto + { + AuthorDetails = author.AuthorDetails, + AuthorName = author.AuthorName, + Books = books, + }); + return authorDto; + } + return Enumerable.Empty().ToList(); + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/BookService.cs b/Joseph/StageThreeTask/Library Management System/Services/BookService.cs new file mode 100644 index 0000000..a3dd3f9 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/BookService.cs @@ -0,0 +1,151 @@ +using Library_Management_System.Data; +using Library_Management_System.Entities; + +namespace Library_Management_System.Services +{ + /// + /// Provides implementations for the BookAuthor class + /// + public class BookService : IBook + { + public readonly ApplicationDbContext _context; + + public BookService(ApplicationDbContext context) + { + _context = context; + } + + public string CreateBook(BookForCreationDto bookDto) + { + string operationStatus; + var authors = new List(); + var authorExist = false; + + var authorName = bookDto.Authors; + // Check if author exist + foreach (var author in authorName) + { + + var auth = _context.Authors + .Where(x => x.AuthorName == author.AuthorName) + .Select(x => x); + if (auth.Count() <= 0) + { + authorExist = false; + break; + } + else + { + var a = _context.Authors.FirstOrDefault(x => x.AuthorName.ToLower() == author.AuthorName.ToLower()); + authors.Add(a); + authorExist = true; + } + }; + + //checked if publisher exist + if (!authorExist) + { + operationStatus = "One or more authors not found in the database"; + return operationStatus; + } + + var pub = _context.Publishers.FirstOrDefault(x => x.PublisherName == bookDto.Publisher); + if (pub == null) + { + operationStatus = "No publisher found with the supplied name"; + return operationStatus; + } + else + { + var author = _context.Authors.Select(x=> x.AuthorName).FirstOrDefault(); + var book = new Book + { + Description = bookDto.Description, + ISBNNumber = bookDto.ISBNNumber, + Title = bookDto.Title, + Version = bookDto.Version, + }; + + _context.Books.Add(book); + _context.SaveChanges(); + + foreach (var authorN in authors) + { + var book_author = new Book_Author + { + AuthorId = authorN.AuthorId, + BookId = book.BookId, + }; + _context.Book_Authors.Add(book_author); + _context.SaveChanges(); + } + + operationStatus = "Book creation successfull"; + return operationStatus; + + } + } + + + public BookForDisplayDto GetABook(string bookName) + { + var book = _context.Books.FirstOrDefault(x => x.Title.Contains(bookName)); + + if (book != null) + { + var book_author = _context.Book_Authors.Where(x => x.BookId == book.BookId).Select(x => x.AuthorId); + var authorList = new List(); + foreach (var author_id in book_author) + { + var author = _context.Authors.FirstOrDefault(x => x.AuthorId == author_id); + authorList.Add(new AuthorForBookCreationDto + { + AuthorName = author.AuthorName + }); + } + + var bookDto = new BookForDisplayDto + { + Title = book.Title, + Authors = authorList, + Description = book.Description, + ISBNNumber = book.ISBNNumber, + Version = book.Version + }; + + return bookDto; + } + return new BookForDisplayDto(); + + } + + public IEnumerable GetAllBooks() + { + var books = _context.Books.Select(x=>x); + var bookDto = new List(); + var authorList = new List(); + + foreach (var book in books) + { + var book_author = _context.Book_Authors.Where(x => x.BookId == book.BookId).Select(x => x.AuthorId); + foreach (var author_id in book_author) + { + authorList.Add(new AuthorForBookCreationDto + { + AuthorName = _context.Authors.FirstOrDefault(a=>a.AuthorId == author_id).AuthorName, + }); + } + + bookDto.Add(new BookForDisplayDto + { + ISBNNumber = book.ISBNNumber, + Authors = authorList, + Description = book.Description, + Title = book.Title, + Version = book.Version + }); + } + return bookDto; + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/IAuthor.cs b/Joseph/StageThreeTask/Library Management System/Services/IAuthor.cs new file mode 100644 index 0000000..31dc37a --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/IAuthor.cs @@ -0,0 +1,10 @@ +namespace Library_Management_System.Services +{ + public interface IAuthor + { + public string CreateAuthor(AuthorForCreationDto author); + public List GetAuthor(string authorName); + IEnumerable GetAllAuthors(); + IEnumerable GetAllBooksByAuthor(string authorName); + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/IBook.cs b/Joseph/StageThreeTask/Library Management System/Services/IBook.cs new file mode 100644 index 0000000..b29cc5d --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/IBook.cs @@ -0,0 +1,9 @@ +namespace Library_Management_System.Services +{ + public interface IBook + { + public string CreateBook(BookForCreationDto bookDto); + public BookForDisplayDto GetABook(string bookName); + public IEnumerable GetAllBooks(); + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/IPublisher.cs b/Joseph/StageThreeTask/Library Management System/Services/IPublisher.cs new file mode 100644 index 0000000..064ab6d --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/IPublisher.cs @@ -0,0 +1,10 @@ +namespace Library_Management_System.Services +{ + public interface IPublisher + { + public void CreatePublisher(PublisherForCreation publisherDto); + public List GetAPublisher(string publisher); + public IEnumerable GetAllPublishers(); + public List GetAuthorsAttachedToAPublisher(string publisherName); + } +} diff --git a/Joseph/StageThreeTask/Library Management System/Services/PublisherService.cs b/Joseph/StageThreeTask/Library Management System/Services/PublisherService.cs new file mode 100644 index 0000000..04ebd1a --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/Services/PublisherService.cs @@ -0,0 +1,84 @@ +using Library_Management_System.Data; + +namespace Library_Management_System.Services +{ + /// + /// Provides implementation for the IPublisher interface + /// + public class PublisherService : IPublisher + { + private readonly ApplicationDbContext _context; + + public PublisherService(ApplicationDbContext context) + { + _context = context; + } + + public void CreatePublisher(PublisherForCreation publisherDto) + { + var publisher = new Publisher() + { + CopyRightLicense = publisherDto.CopyRightLicense, + PublisherAddress = publisherDto.PublisherAddress, + PublisherName = publisherDto.PublisherName, + }; + + _context.Publishers.Add(publisher); + _context.SaveChanges(); + } + + public IEnumerable GetAllPublishers() + { + var publisherDto = new List(); + var publishers = _context.Publishers.Select(x=>x); + foreach (var publisher in publishers) + { + publisherDto.Add(new PublisherForDisplay + { + PublisherName = publisher.PublisherName, + PublisherAddress = publisher.PublisherAddress, + }); + } + + return publisherDto; + } + + public List GetAPublisher(string publisherName) + { + var publishers = _context.Publishers + .Where(x => x.PublisherName.Contains(publisherName)) + .Select(x => x).ToList(); + + var publishersDto = new List(); + //Convert to PublisherForDisplay obj + foreach(var publisher in publishers) + { + publishersDto.Add(new PublisherForDisplay + { + PublisherName = publisher.PublisherName, + PublisherAddress = publisher.PublisherAddress, + }); + } + + return publishersDto; + } + + public List GetAuthorsAttachedToAPublisher(string publisherName) + { + var authors = _context.Authors + .Where(x => x.Publisher.PublisherName.ToLower() == publisherName.ToLower()) + .Select(x => x).ToList(); + var authorDisplayDto = new List(); + foreach(var author in authors) + { + authorDisplayDto.Add(new AuthorForDisplayDto + { + AuthorDetails = author.AuthorDetails, + AuthorName = author.AuthorName, + }); + } + + return authorDisplayDto; + } + } +} diff --git a/Joseph/StageThreeTask/Library Management System/appsettings.Development.json b/Joseph/StageThreeTask/Library Management System/appsettings.Development.json new file mode 100644 index 0000000..cb1b6db --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/appsettings.Development.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + + "ConnectionStrings": { + "DefaultConnection":"Server=(localdb)\\MSSQLLocalDB;Database=LibraryManagement;Trusted_Connection=True;MultipleActiveResultSets=true" + } +} diff --git a/Joseph/StageThreeTask/Library Management System/appsettings.json b/Joseph/StageThreeTask/Library Management System/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Joseph/StageThreeTask/Library Management System/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}