diff --git a/StudentWebApi/Controllers/StudentController.cs b/StudentWebApi/Controllers/StudentController.cs new file mode 100644 index 0000000..a74c7d2 --- /dev/null +++ b/StudentWebApi/Controllers/StudentController.cs @@ -0,0 +1,48 @@ +using Microsoft.AspNetCore.Mvc; +using StudentWebApi.Models; +using StudentWebApi.Services; + +namespace StudentWebApi.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class StudentController : ControllerBase + { + private readonly StudentService _studentService; + public StudentController(StudentService studentService) + { + _studentService = studentService ?? throw new ArgumentNullException(nameof(studentService)); + } + + [HttpGet] + public ActionResult GetAllStudent() + { + return Ok(_studentService.GetAllStudents()); + } + + [HttpGet("{id}")] + public ActionResult GetStudentById(long id) + { + return Ok(_studentService.GetStudentById(id)); + } + + [HttpPost] + public ActionResult AddStudent([FromBody] Student student) + { + Student createdStudent = _studentService.AddStudent(student); + return createdStudent; + } + + [HttpPut("{id}")] + public ActionResult UpdateStudent(long id, [FromBody] Student student) + { + return _studentService.UpdateStudent(id, student); + } + + [HttpDelete("{id}")] + public ActionResult DeleteStudent(long id) + { + return Ok(_studentService.DeleteStudent(id)); + } + } +} \ No newline at end of file diff --git a/StudentWebApi/Models/Student.cs b/StudentWebApi/Models/Student.cs new file mode 100644 index 0000000..ce647cd --- /dev/null +++ b/StudentWebApi/Models/Student.cs @@ -0,0 +1,20 @@ +namespace StudentWebApi.Models +{ + public class Student + { + public Student() { } + + + public Student(long studentId, string name, string course) + { + this.StudentId = studentId; + this.Name = name; + this.Course = course; + } + + public long StudentId { get; set; } + public required string Name { get; set; } + public required string Course { get; set; } + + } +} \ No newline at end of file diff --git a/StudentWebApi/Program.cs b/StudentWebApi/Program.cs new file mode 100644 index 0000000..5a77b1c --- /dev/null +++ b/StudentWebApi/Program.cs @@ -0,0 +1,25 @@ +using StudentWebApi.Services; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi + +builder.Services.AddControllers(); +builder.Services.AddScoped(); +builder.Services.AddOpenApi(); + +var app = builder.Build(); + +app.MapControllers(); +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + + + + +app.Run(); + diff --git a/StudentWebApi/Properties/launchSettings.json b/StudentWebApi/Properties/launchSettings.json new file mode 100644 index 0000000..178cce5 --- /dev/null +++ b/StudentWebApi/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:9090", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7175;http://localhost:9090", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/StudentWebApi/Services/IStudentService.cs b/StudentWebApi/Services/IStudentService.cs new file mode 100644 index 0000000..afc7352 --- /dev/null +++ b/StudentWebApi/Services/IStudentService.cs @@ -0,0 +1,16 @@ +using StudentWebApi.Models; + +namespace StudentWebApi.Services +{ + public interface IStudentService + { + List GetAllStudents(); + + Student? GetStudentById(long id); + Student AddStudent(Student student); + + Student UpdateStudent(long id, Student student); + + Boolean DeleteStudent(long id); + } +} \ No newline at end of file diff --git a/StudentWebApi/Services/StudentService.cs b/StudentWebApi/Services/StudentService.cs new file mode 100644 index 0000000..0665263 --- /dev/null +++ b/StudentWebApi/Services/StudentService.cs @@ -0,0 +1,62 @@ +using StudentWebApi.Models; + +namespace StudentWebApi.Services +{ + public class StudentService : IStudentService + { + private static Dictionary students = new Dictionary(); + private static long id = 1; + public Student AddStudent(Student student) + { + if (student.StudentId == 0) + { + student.StudentId = id++; + } + + students.Add(student.StudentId, student); + return student; + } + + public bool DeleteStudent(long id) + { + if (students.ContainsKey(id)) + { + students.Remove(id); + return true; + } + + return false; + } + + public List GetAllStudents() + { + return students.Values.ToList(); + } + + public Student? GetStudentById(long id) + { + if (students.ContainsKey(id)) + { + return students[id]; + } + + return null; + } + + public Student? UpdateStudent(long id, Student student) + { + if (students.ContainsKey(id)) + { + Student currStudent = students[id]; + + currStudent.Name = student.Name; + + currStudent.Course = student.Course; + + return currStudent; + } + + return null; + } + } +} \ No newline at end of file diff --git a/StudentWebApi/StudentWebApi.sln b/StudentWebApi/StudentWebApi.sln new file mode 100644 index 0000000..a09a74b --- /dev/null +++ b/StudentWebApi/StudentWebApi.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = ".", "..csproj", "{FEA91663-7214-B722-A16E-B69FFDC74FA0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FEA91663-7214-B722-A16E-B69FFDC74FA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FEA91663-7214-B722-A16E-B69FFDC74FA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FEA91663-7214-B722-A16E-B69FFDC74FA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FEA91663-7214-B722-A16E-B69FFDC74FA0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {96E96260-0E2B-40B7-BE67-E2653B8E402B} + EndGlobalSection +EndGlobal diff --git a/StudentWebApi/appsettings.Development.json b/StudentWebApi/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/StudentWebApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/StudentWebApi/appsettings.json b/StudentWebApi/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/StudentWebApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/StudentWebApi/bin/Debug/net9.0/Microsoft.AspNetCore.OpenApi.dll b/StudentWebApi/bin/Debug/net9.0/Microsoft.AspNetCore.OpenApi.dll new file mode 100644 index 0000000..81cb94f Binary files /dev/null and b/StudentWebApi/bin/Debug/net9.0/Microsoft.AspNetCore.OpenApi.dll differ diff --git a/StudentWebApi/bin/Debug/net9.0/Microsoft.OpenApi.dll b/StudentWebApi/bin/Debug/net9.0/Microsoft.OpenApi.dll new file mode 100644 index 0000000..d9f09da Binary files /dev/null and b/StudentWebApi/bin/Debug/net9.0/Microsoft.OpenApi.dll differ diff --git a/StudentWebApi/bin/Debug/net9.0/appsettings.Development.json b/StudentWebApi/bin/Debug/net9.0/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/StudentWebApi/bin/Debug/net9.0/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/StudentWebApi/bin/Debug/net9.0/appsettings.json b/StudentWebApi/bin/Debug/net9.0/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/StudentWebApi/bin/Debug/net9.0/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/StudentWebApi/obj/Debug/net9.0/apphost.exe b/StudentWebApi/obj/Debug/net9.0/apphost.exe new file mode 100644 index 0000000..f430cb6 Binary files /dev/null and b/StudentWebApi/obj/Debug/net9.0/apphost.exe differ diff --git a/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.endpoints.json b/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.endpoints.json new file mode 100644 index 0000000..03fc3ed --- /dev/null +++ b/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.endpoints.json @@ -0,0 +1,5 @@ +{ + "Version": 1, + "ManifestType": "Build", + "Endpoints": [] +} \ No newline at end of file diff --git a/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.json b/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.json new file mode 100644 index 0000000..47af2e2 --- /dev/null +++ b/StudentWebApi/obj/Debug/net9.0/staticwebassets.build.json @@ -0,0 +1,12 @@ +{ + "Version": 1, + "Hash": "22N0qOQgQTdqW+HURum5y+2b1v1Ky9vtgbintF3y0BU=", + "Source": ".", + "BasePath": "_content/.", + "Mode": "Default", + "ManifestType": "Build", + "ReferencedProjectsConfiguration": [], + "DiscoveryPatterns": [], + "Assets": [], + "Endpoints": [] +} \ No newline at end of file diff --git a/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.build...props b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.build...props new file mode 100644 index 0000000..d0b8569 --- /dev/null +++ b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.build...props @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildMultiTargeting...props b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildMultiTargeting...props new file mode 100644 index 0000000..dcab9ae --- /dev/null +++ b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildMultiTargeting...props @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildTransitive...props b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildTransitive...props new file mode 100644 index 0000000..fd482da --- /dev/null +++ b/StudentWebApi/obj/Debug/net9.0/staticwebassets/msbuild.buildTransitive...props @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/StudentWebApi/obj/project.assets.json b/StudentWebApi/obj/project.assets.json new file mode 100644 index 0000000..4d7406e --- /dev/null +++ b/StudentWebApi/obj/project.assets.json @@ -0,0 +1,156 @@ +{ + "version": 3, + "targets": { + "net9.0": { + "Microsoft.AspNetCore.OpenApi/9.0.4": { + "type": "package", + "dependencies": { + "Microsoft.OpenApi": "1.6.17" + }, + "compile": { + "lib/net9.0/Microsoft.AspNetCore.OpenApi.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net9.0/Microsoft.AspNetCore.OpenApi.dll": { + "related": ".xml" + } + }, + "frameworkReferences": [ + "Microsoft.AspNetCore.App" + ] + }, + "Microsoft.OpenApi/1.6.17": { + "type": "package", + "compile": { + "lib/netstandard2.0/Microsoft.OpenApi.dll": { + "related": ".pdb;.xml" + } + }, + "runtime": { + "lib/netstandard2.0/Microsoft.OpenApi.dll": { + "related": ".pdb;.xml" + } + } + } + } + }, + "libraries": { + "Microsoft.AspNetCore.OpenApi/9.0.4": { + "sha512": "GfZWPbZz1aAtEO3wGCkpeyRc0gzr/+VRHnUgY/YjqVPDlHbeKWCXw3IxKarQdo9myC2O1QBf652Mo50QqbXYRg==", + "type": "package", + "path": "microsoft.aspnetcore.openapi/9.0.4", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "Icon.png", + "PACKAGE.md", + "THIRD-PARTY-NOTICES.TXT", + "lib/net9.0/Microsoft.AspNetCore.OpenApi.dll", + "lib/net9.0/Microsoft.AspNetCore.OpenApi.xml", + "microsoft.aspnetcore.openapi.9.0.4.nupkg.sha512", + "microsoft.aspnetcore.openapi.nuspec" + ] + }, + "Microsoft.OpenApi/1.6.17": { + "sha512": "Le+kehlmrlQfuDFUt1zZ2dVwrhFQtKREdKBo+rexOwaCoYP0/qpgT9tLxCsZjsgR5Itk1UKPcbgO+FyaNid/bA==", + "type": "package", + "path": "microsoft.openapi/1.6.17", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "README.md", + "lib/netstandard2.0/Microsoft.OpenApi.dll", + "lib/netstandard2.0/Microsoft.OpenApi.pdb", + "lib/netstandard2.0/Microsoft.OpenApi.xml", + "microsoft.openapi.1.6.17.nupkg.sha512", + "microsoft.openapi.nuspec" + ] + } + }, + "projectFileDependencyGroups": { + "net9.0": [ + "Microsoft.AspNetCore.OpenApi >= 9.0.4" + ] + }, + "packageFolders": { + "C:\\Users\\Rafael\\.nuget\\packages\\": {}, + "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {} + }, + "project": { + "version": "1.0.0", + "restore": { + "projectUniqueName": "C:\\Users\\Rafael\\OneDrive\\Desktop\\Carbajal\\csharp-asp.net-api\\carbajal-asp.net-api\\StudentWebApi\\..csproj", + "projectName": ".", + "projectPath": "C:\\Users\\Rafael\\OneDrive\\Desktop\\Carbajal\\csharp-asp.net-api\\carbajal-asp.net-api\\StudentWebApi\\..csproj", + "packagesPath": "C:\\Users\\Rafael\\.nuget\\packages\\", + "outputPath": "C:\\Users\\Rafael\\OneDrive\\Desktop\\Carbajal\\csharp-asp.net-api\\carbajal-asp.net-api\\StudentWebApi\\obj\\", + "projectStyle": "PackageReference", + "fallbackFolders": [ + "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages" + ], + "configFilePaths": [ + "C:\\Users\\Rafael\\AppData\\Roaming\\NuGet\\NuGet.Config", + "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config", + "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" + ], + "originalTargetFrameworks": [ + "net9.0" + ], + "sources": { + "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {}, + "C:\\Program Files\\dotnet\\library-packs": {}, + "https://api.nuget.org/v3/index.json": {} + }, + "frameworks": { + "net9.0": { + "targetAlias": "net9.0", + "projectReferences": {} + } + }, + "warningProperties": { + "warnAsError": [ + "NU1605" + ] + }, + "restoreAuditProperties": { + "enableAudit": "true", + "auditLevel": "low", + "auditMode": "direct" + }, + "SdkAnalysisLevel": "9.0.200" + }, + "frameworks": { + "net9.0": { + "targetAlias": "net9.0", + "dependencies": { + "Microsoft.AspNetCore.OpenApi": { + "target": "Package", + "version": "[9.0.4, )" + } + }, + "imports": [ + "net461", + "net462", + "net47", + "net471", + "net472", + "net48", + "net481" + ], + "assetTargetFallback": true, + "warn": true, + "frameworkReferences": { + "Microsoft.AspNetCore.App": { + "privateAssets": "none" + }, + "Microsoft.NETCore.App": { + "privateAssets": "all" + } + }, + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.203/PortableRuntimeIdentifierGraph.json" + } + } + } +} \ No newline at end of file diff --git a/StudentWebApi/obj/project.nuget.cache b/StudentWebApi/obj/project.nuget.cache new file mode 100644 index 0000000..912b0b2 --- /dev/null +++ b/StudentWebApi/obj/project.nuget.cache @@ -0,0 +1,11 @@ +{ + "version": 2, + "dgSpecHash": "deW0E47jbxQ=", + "success": true, + "projectFilePath": "C:\\Users\\Rafael\\OneDrive\\Desktop\\Carbajal\\csharp-asp.net-api\\carbajal-asp.net-api\\StudentWebApi\\..csproj", + "expectedPackageFiles": [ + "C:\\Users\\Rafael\\.nuget\\packages\\microsoft.aspnetcore.openapi\\9.0.4\\microsoft.aspnetcore.openapi.9.0.4.nupkg.sha512", + "C:\\Users\\Rafael\\.nuget\\packages\\microsoft.openapi\\1.6.17\\microsoft.openapi.1.6.17.nupkg.sha512" + ], + "logs": [] +} \ No newline at end of file