15 changed files with 302 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||||
|
using w21library.classes.Intf; |
||||
|
|
||||
|
namespace w21library.classes |
||||
|
{ |
||||
|
public class Book : IBook |
||||
|
{ |
||||
|
public Book(string author, string name) |
||||
|
{ |
||||
|
Name = name; |
||||
|
Author = author; |
||||
|
Uuid = Guid.NewGuid().ToString("N"); |
||||
|
} |
||||
|
|
||||
|
public string Name { get; set; } |
||||
|
public string Author { get; set; } |
||||
|
public string Uuid { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace w21library.classes.Intf |
||||
|
{ |
||||
|
public interface IBook |
||||
|
{ |
||||
|
string Uuid { get; set; } |
||||
|
string Author { get; set; } |
||||
|
string Name { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace w21library.classes.Intf |
||||
|
{ |
||||
|
public interface ILibrary |
||||
|
{ |
||||
|
IEnumerable<IBook> GetActiveBooks(); |
||||
|
bool RemoveBook(string guid); |
||||
|
bool AddBook(string name, string author); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using w21library.classes.Intf; |
||||
|
|
||||
|
namespace w21library.classes |
||||
|
{ |
||||
|
public class Library : ILibrary |
||||
|
{ |
||||
|
protected List<Book> BookList = new List<Book>(); |
||||
|
public IEnumerable<IBook> GetActiveBooks() |
||||
|
{ |
||||
|
List<Book> activeBookList = new List<Book>(); |
||||
|
foreach (var book in BookList) |
||||
|
{ |
||||
|
activeBookList.Add(book); |
||||
|
} |
||||
|
return activeBookList; |
||||
|
} |
||||
|
|
||||
|
public bool RemoveBook(string guid) |
||||
|
{ |
||||
|
var book = BookList.FirstOrDefault(p => p.Uuid.Equals(guid)); |
||||
|
if (book == null) return false; |
||||
|
return BookList.Remove(book); |
||||
|
} |
||||
|
|
||||
|
public bool AddBook(string name, string author) |
||||
|
{ |
||||
|
var book = new Book(author, name); |
||||
|
BookList.Add(book); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
namespace w21library.classes |
||||
|
{ |
||||
|
public class TestLibrary : Library |
||||
|
{ |
||||
|
public TestLibrary() |
||||
|
{ |
||||
|
BookList.Add(new Book("author 1", "name 1")); |
||||
|
BookList.Add(new Book("author 1", "name 2")); |
||||
|
BookList.Add(new Book("author 2", "name 3")); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,53 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using w21library.classes.Intf; |
||||
|
|
||||
|
namespace w21library.Controllers |
||||
|
{ |
||||
|
[ApiController] |
||||
|
[Route("/v1/library")] |
||||
|
public class LibraryControllerV1 : ControllerBase |
||||
|
{ |
||||
|
private readonly ILibrary _library; |
||||
|
|
||||
|
public LibraryControllerV1(ILibrary library) |
||||
|
{ |
||||
|
_library = library; |
||||
|
} |
||||
|
|
||||
|
[HttpGet("books")] |
||||
|
public IEnumerable<IBook> Get() |
||||
|
{ |
||||
|
return _library.GetActiveBooks(); |
||||
|
} |
||||
|
|
||||
|
[HttpGet("books/{guid}")] |
||||
|
public IBook? Get(string guid) |
||||
|
{ |
||||
|
return _library |
||||
|
.GetActiveBooks() |
||||
|
.FirstOrDefault(p => p.Uuid.Equals(guid)); |
||||
|
} |
||||
|
|
||||
|
[HttpDelete("books/{guid}")] |
||||
|
public void Delete( |
||||
|
// [FromQuery]
|
||||
|
string guid) |
||||
|
{ |
||||
|
bool result = _library.RemoveBook(guid); |
||||
|
Response.StatusCode = result |
||||
|
? 200 |
||||
|
: 404; |
||||
|
} |
||||
|
|
||||
|
[HttpPost("books")] |
||||
|
public bool Insert( |
||||
|
[FromForm] // [FromQuery]
|
||||
|
string name, |
||||
|
[FromForm] |
||||
|
string author |
||||
|
) |
||||
|
{ |
||||
|
return _library.AddBook(name, author); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
using w21library.classes; |
||||
|
using w21library.classes.Intf; |
||||
|
|
||||
|
namespace w21library |
||||
|
{ |
||||
|
public class Program |
||||
|
{ |
||||
|
public static void Main(string[] args) |
||||
|
{ |
||||
|
var builder = WebApplication.CreateBuilder(args); |
||||
|
|
||||
|
// Add services to the container.
|
||||
|
TestLibrary testLibrary = new TestLibrary(); |
||||
|
builder.Services.AddSingleton<ILibrary>(testLibrary); |
||||
|
builder.Services.AddControllers(); |
||||
|
|
||||
|
var app = builder.Build(); |
||||
|
|
||||
|
// Configure the HTTP request pipeline.
|
||||
|
app.MapControllers(); |
||||
|
|
||||
|
app.Run(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
{ |
||||
|
"$schema": "https://json.schemastore.org/launchsettings.json", |
||||
|
"iisSettings": { |
||||
|
"windowsAuthentication": false, |
||||
|
"anonymousAuthentication": true, |
||||
|
"iisExpress": { |
||||
|
"applicationUrl": "http://localhost:23970", |
||||
|
"sslPort": 0 |
||||
|
} |
||||
|
}, |
||||
|
"profiles": { |
||||
|
"w21library": { |
||||
|
"commandName": "Project", |
||||
|
"dotnetRunMessages": true, |
||||
|
"launchBrowser": true, |
||||
|
"launchUrl": "v1/library/books", |
||||
|
"applicationUrl": "http://localhost:5106", |
||||
|
"environmentVariables": { |
||||
|
"ASPNETCORE_ENVIRONMENT": "Development" |
||||
|
} |
||||
|
}, |
||||
|
"IIS Express": { |
||||
|
"commandName": "IISExpress", |
||||
|
"launchBrowser": true, |
||||
|
"launchUrl": "v1/library/books", |
||||
|
"environmentVariables": { |
||||
|
"ASPNETCORE_ENVIRONMENT": "Development" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
}, |
||||
|
"AllowedHosts": "*" |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk.Web"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<Nullable>enable</Nullable> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w21library.classes\w21library.classes.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,37 @@ |
|||||
|
|
||||
|
Microsoft Visual Studio Solution File, Format Version 12.00 |
||||
|
# Visual Studio Version 17 |
||||
|
VisualStudioVersion = 17.2.32616.157 |
||||
|
MinimumVisualStudioVersion = 10.0.40219.1 |
||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w21library", "w21library.csproj", "{948CE295-6EA9-4BC3-BC8F-49366D97EBC1}" |
||||
|
EndProject |
||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w21library.classes", "..\w21library.classes\w21library.classes.csproj", "{5E6E6FCB-8C53-4852-BF8F-AD3C383304D2}" |
||||
|
EndProject |
||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "w21libraryconsole", "..\w21libraryconsole\w21libraryconsole.csproj", "{79208B04-C191-453E-8595-4F3109C2133F}" |
||||
|
EndProject |
||||
|
Global |
||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||
|
Debug|Any CPU = Debug|Any CPU |
||||
|
Release|Any CPU = Release|Any CPU |
||||
|
EndGlobalSection |
||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||
|
{948CE295-6EA9-4BC3-BC8F-49366D97EBC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
|
{948CE295-6EA9-4BC3-BC8F-49366D97EBC1}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
|
{948CE295-6EA9-4BC3-BC8F-49366D97EBC1}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
|
{948CE295-6EA9-4BC3-BC8F-49366D97EBC1}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
|
{5E6E6FCB-8C53-4852-BF8F-AD3C383304D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
|
{5E6E6FCB-8C53-4852-BF8F-AD3C383304D2}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
|
{5E6E6FCB-8C53-4852-BF8F-AD3C383304D2}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
|
{5E6E6FCB-8C53-4852-BF8F-AD3C383304D2}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
|
{79208B04-C191-453E-8595-4F3109C2133F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
|
{79208B04-C191-453E-8595-4F3109C2133F}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
|
{79208B04-C191-453E-8595-4F3109C2133F}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
|
{79208B04-C191-453E-8595-4F3109C2133F}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
|
EndGlobalSection |
||||
|
GlobalSection(SolutionProperties) = preSolution |
||||
|
HideSolutionNode = FALSE |
||||
|
EndGlobalSection |
||||
|
GlobalSection(ExtensibilityGlobals) = postSolution |
||||
|
SolutionGuid = {70EC55B2-4455-422F-8F47-D9F9846901E2} |
||||
|
EndGlobalSection |
||||
|
EndGlobal |
||||
@ -0,0 +1,20 @@ |
|||||
|
using w21library.classes; |
||||
|
|
||||
|
namespace w21libraryconsole |
||||
|
{ |
||||
|
internal class Program |
||||
|
{ |
||||
|
static void Main(string[] args) |
||||
|
{ |
||||
|
TestLibrary testLibrary = new TestLibrary(); |
||||
|
string uid = String.Empty; |
||||
|
foreach (var book in testLibrary.GetActiveBooks()) |
||||
|
{ |
||||
|
uid = book.Uuid; |
||||
|
Console.WriteLine($"[{book.Author} // {book.Name}"); |
||||
|
} |
||||
|
testLibrary.AddBook("asdas", "asasdfasdasd"); |
||||
|
testLibrary.RemoveBook(uid); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>net6.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\w21library.classes\w21library.classes.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
Loading…
Reference in new issue