diff --git a/20230415.1/w21library.classes/Book.cs b/20230415.1/w21library.classes/Book.cs new file mode 100644 index 0000000..d01c415 --- /dev/null +++ b/20230415.1/w21library.classes/Book.cs @@ -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; } + } +} \ No newline at end of file diff --git a/20230415.1/w21library.classes/Intf/IBook.cs b/20230415.1/w21library.classes/Intf/IBook.cs new file mode 100644 index 0000000..7ebda5b --- /dev/null +++ b/20230415.1/w21library.classes/Intf/IBook.cs @@ -0,0 +1,9 @@ +namespace w21library.classes.Intf +{ + public interface IBook + { + string Uuid { get; set; } + string Author { get; set; } + string Name { get; set; } + } +} \ No newline at end of file diff --git a/20230415.1/w21library.classes/Intf/ILibrary.cs b/20230415.1/w21library.classes/Intf/ILibrary.cs new file mode 100644 index 0000000..8694354 --- /dev/null +++ b/20230415.1/w21library.classes/Intf/ILibrary.cs @@ -0,0 +1,9 @@ +namespace w21library.classes.Intf +{ + public interface ILibrary + { + IEnumerable GetActiveBooks(); + bool RemoveBook(string guid); + bool AddBook(string name, string author); + } +} \ No newline at end of file diff --git a/20230415.1/w21library.classes/Library.cs b/20230415.1/w21library.classes/Library.cs new file mode 100644 index 0000000..484cb39 --- /dev/null +++ b/20230415.1/w21library.classes/Library.cs @@ -0,0 +1,34 @@ +using w21library.classes.Intf; + +namespace w21library.classes +{ + public class Library : ILibrary + { + protected List BookList = new List(); + public IEnumerable GetActiveBooks() + { + List activeBookList = new List(); + 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; + } + + } + +} \ No newline at end of file diff --git a/20230415.1/w21library.classes/TestLibrary.cs b/20230415.1/w21library.classes/TestLibrary.cs new file mode 100644 index 0000000..45b49bf --- /dev/null +++ b/20230415.1/w21library.classes/TestLibrary.cs @@ -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")); + } + } + +} \ No newline at end of file diff --git a/20230415.1/w21library.classes/w21library.classes.csproj b/20230415.1/w21library.classes/w21library.classes.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/20230415.1/w21library.classes/w21library.classes.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/20230415.1/w21library/Controllers/LibraryController.cs b/20230415.1/w21library/Controllers/LibraryController.cs new file mode 100644 index 0000000..9d85cd8 --- /dev/null +++ b/20230415.1/w21library/Controllers/LibraryController.cs @@ -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 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); + } + } +} \ No newline at end of file diff --git a/20230415.1/w21library/Program.cs b/20230415.1/w21library/Program.cs new file mode 100644 index 0000000..bdd44f6 --- /dev/null +++ b/20230415.1/w21library/Program.cs @@ -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(testLibrary); + builder.Services.AddControllers(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + app.MapControllers(); + + app.Run(); + } + } +} \ No newline at end of file diff --git a/20230415.1/w21library/Properties/launchSettings.json b/20230415.1/w21library/Properties/launchSettings.json new file mode 100644 index 0000000..d62ea4c --- /dev/null +++ b/20230415.1/w21library/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/20230415.1/w21library/appsettings.Development.json b/20230415.1/w21library/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/20230415.1/w21library/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/20230415.1/w21library/appsettings.json b/20230415.1/w21library/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/20230415.1/w21library/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/20230415.1/w21library/w21library.csproj b/20230415.1/w21library/w21library.csproj new file mode 100644 index 0000000..9be0b80 --- /dev/null +++ b/20230415.1/w21library/w21library.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/20230415.1/w21library/w21library.sln b/20230415.1/w21library/w21library.sln new file mode 100644 index 0000000..ee90190 --- /dev/null +++ b/20230415.1/w21library/w21library.sln @@ -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 diff --git a/20230415.1/w21libraryconsole/Program.cs b/20230415.1/w21libraryconsole/Program.cs new file mode 100644 index 0000000..7a35554 --- /dev/null +++ b/20230415.1/w21libraryconsole/Program.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/20230415.1/w21libraryconsole/w21libraryconsole.csproj b/20230415.1/w21libraryconsole/w21libraryconsole.csproj new file mode 100644 index 0000000..127c390 --- /dev/null +++ b/20230415.1/w21libraryconsole/w21libraryconsole.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +