Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/John Peter/Stage 2 Project/bin/Debug/net6.0/Banking.dll",
"args": [],
"cwd": "${workspaceFolder}/John Peter/Stage 2 Project",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/John Peter/Stage 2 Project/Banking.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/John Peter/Stage 2 Project/Banking.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/John Peter/Stage 2 Project/Banking.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
36 changes: 36 additions & 0 deletions John Peter/Library/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/Library.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"requireExactSource": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions John Peter/Library/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Library.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Library.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Library.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
133 changes: 133 additions & 0 deletions John Peter/Library/Context/LibraryContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Microsoft.EntityFrameworkCore;
using Library.Models;

namespace Library.Context;

public partial class LibraryContext : DbContext
{
public LibraryContext() { }

public LibraryContext(DbContextOptions<LibraryContext> options)
: base(options) { }

public virtual DbSet<Author> Authors { get; set; }

public virtual DbSet<Book> Books { get; set; }

public virtual DbSet<Publisher> Publishers { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseMySql(
"name=ConnectionStrings:LocalSQL",
Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.32-mysql")
);

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseCollation("utf8mb4_0900_ai_ci").HasCharSet("utf8mb4");

modelBuilder.Entity<Author>(entity =>
{
entity.HasKey(e => e.Id).HasName("PRIMARY");

entity.ToTable("Author");

entity.HasIndex(e => e.PublisherId, "publisher_id");

entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Name).HasMaxLength(255).HasColumnName("name");
entity.Property(e => e.PublisherId).HasColumnName("publisher_id");

entity
.HasOne(d => d.Publisher)
.WithMany(p => p.Authors)
.HasForeignKey(d => d.PublisherId)
.HasConstraintName("Author_ibfk_1");

entity
.HasMany(d => d.Books)
.WithMany(p => p.Authors)
.UsingEntity<Dictionary<string, object>>(
"AuthorBook",
r =>
r.HasOne<Book>()
.WithMany()
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Author_Book_ibfk_2"),
l =>
l.HasOne<Author>()
.WithMany()
.HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Author_Book_ibfk_1"),
j =>
{
j.HasKey("AuthorId", "BookId")
.HasName("PRIMARY")
.HasAnnotation("MySql:IndexPrefixLength", new[] { 0, 0 });
j.ToTable("Author_Book");
j.HasIndex(new[] { "BookId" }, "book_id");
j.IndexerProperty<int>("AuthorId").HasColumnName("author_id");
j.IndexerProperty<int>("BookId").HasColumnName("book_id");
}
);
});

modelBuilder.Entity<Book>(entity =>
{
entity.HasKey(e => e.Id).HasName("PRIMARY");

entity.ToTable("Book");

entity.HasIndex(e => e.AuthorId, "author_id");

entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.AuthorId).HasColumnName("author_id");
entity.Property(e => e.Title).HasMaxLength(255).HasColumnName("title");

entity
.HasMany(b => b.Authors)
.WithMany(a => a.Books)
.UsingEntity<Dictionary<string, object>>(
"AuthorBook",
b =>
b.HasOne<Author>()
.WithMany()
.HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Author_Book_ibfk_1"),
a =>
a.HasOne<Book>()
.WithMany()
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Author_Book_ibfk_2"),
j =>
{
j.HasKey("AuthorId", "BookId")
.HasName("PRIMARY")
.HasAnnotation("MySql:IndexPrefixLength", new[] { 0, 0 });
j.ToTable("Author_Book");
j.HasIndex(new[] { "BookId" }, "book_id");
j.IndexerProperty<int>("AuthorId").HasColumnName("author_id");
j.IndexerProperty<int>("BookId").HasColumnName("book_id");
}
);
});

modelBuilder.Entity<Publisher>(entity =>
{
entity.HasKey(e => e.Id).HasName("PRIMARY");

entity.ToTable("Publisher");

entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.Name).HasMaxLength(255).HasColumnName("name");
});

OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
51 changes: 51 additions & 0 deletions John Peter/Library/Controllers/AuthorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Library.DTO;
using Library.Interfaces;
using Library.Models;
using Microsoft.AspNetCore.Mvc;

namespace Library.Controllers
{
[ApiController]
[Route("authors")]
public class AuthorController : ControllerBase
{
private readonly IAuthorsRepository authorsRepository;

public AuthorController(IAuthorsRepository authorsRepository)
{
this.authorsRepository = authorsRepository;
}

[HttpPost]
public async Task<ActionResult<AuthorDTO>> CreateAuthorAsync(CreateAuthorDTO authorDTO)
{
Author newAuthor = new() { Name = authorDTO.Name, PublisherId = authorDTO.PublisherId };
return Ok(await authorsRepository.CreateAuthorAsync(newAuthor));
}

[HttpGet]
public async Task<ActionResult<IEnumerable<Author>>> GetAuthorsAsync()
{
var authors = await authorsRepository.GetAuthorsAsync();
return Ok(authors.Select(aut => aut.AuthorList()));
}

[HttpGet("{id}")]
public async Task<ActionResult<AuthorDTO>> GetAuthorByIdAsync(int id)
{
var author = await authorsRepository.GetAuthorByIdAsync(id);
if (author is null)
{
return NotFound();
}
return Ok(author);
}

[HttpGet("{id}/books")]
public async Task<ActionResult<IEnumerable<Book>>> GetAuthorBooksByIdAsync(int id)
{
var books = await authorsRepository.GetBooksByAuthorIdAsync(id);
return Ok(books.Select(book => book.BookList()));
}
}
}
36 changes: 36 additions & 0 deletions John Peter/Library/Controllers/BookController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Library.Models;
using Microsoft.AspNetCore.Mvc;

namespace Library.Controllers
{
[ApiController]
[Route("books")]
public class BooksController : ControllerBase
{
private readonly IBooksRepository booksRepository;

public BooksController(IBooksRepository repository)
{
this.booksRepository = repository;
}

[HttpPost]
public async Task<ActionResult<Book>> CreateBookAsync(CreateBookDTO bookDTO)
{
Book newBook = new() { Title = bookDTO.Title, AuthorId = bookDTO.AuthorId };
return Ok(await booksRepository.CreateBookAsync(newBook));
}

[HttpGet("{id}")]
public async Task<ActionResult<BookDTO>> GetBookByIdAsync(int id)
{
return Ok(await booksRepository.GetBookAsync(id));
}

[HttpGet]
public async Task<ActionResult<IEnumerable<BookDTO>>> GetAllBooks()
{
return Ok(await booksRepository.GetBooksAsync());
}
}
}
Loading