Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.
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
48 changes: 48 additions & 0 deletions StudentWebApi/Controllers/StudentController.cs
Original file line number Diff line number Diff line change
@@ -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<Student> GetAllStudent()
{
return Ok(_studentService.GetAllStudents());
}

[HttpGet("{id}")]
public ActionResult<Student?> GetStudentById(long id)
{
return Ok(_studentService.GetStudentById(id));
}

[HttpPost]
public ActionResult<Student> AddStudent([FromBody] Student student)
{
Student createdStudent = _studentService.AddStudent(student);
return createdStudent;
}

[HttpPut("{id}")]
public ActionResult<Student?> UpdateStudent(long id, [FromBody] Student student)
{
return _studentService.UpdateStudent(id, student);
}

[HttpDelete("{id}")]
public ActionResult<Boolean> DeleteStudent(long id)
{
return Ok(_studentService.DeleteStudent(id));
}
}
}
20 changes: 20 additions & 0 deletions StudentWebApi/Models/Student.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
25 changes: 25 additions & 0 deletions StudentWebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -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<StudentService>();
builder.Services.AddOpenApi();

var app = builder.Build();

app.MapControllers();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}




app.Run();

23 changes: 23 additions & 0 deletions StudentWebApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
16 changes: 16 additions & 0 deletions StudentWebApi/Services/IStudentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using StudentWebApi.Models;

namespace StudentWebApi.Services
{
public interface IStudentService
{
List<Student> GetAllStudents();

Student? GetStudentById(long id);
Student AddStudent(Student student);

Student UpdateStudent(long id, Student student);

Boolean DeleteStudent(long id);
}
}
62 changes: 62 additions & 0 deletions StudentWebApi/Services/StudentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using StudentWebApi.Models;

namespace StudentWebApi.Services
{
public class StudentService : IStudentService
{
private static Dictionary<long, Student> students = new Dictionary<long, Student>();
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<Student> 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;
}
}
}
24 changes: 24 additions & 0 deletions StudentWebApi/StudentWebApi.sln
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions StudentWebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions StudentWebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions StudentWebApi/bin/Debug/net9.0/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions StudentWebApi/bin/Debug/net9.0/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Binary file added StudentWebApi/obj/Debug/net9.0/apphost.exe
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Version": 1,
"ManifestType": "Build",
"Endpoints": []
}
12 changes: 12 additions & 0 deletions StudentWebApi/obj/Debug/net9.0/staticwebassets.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Version": 1,
"Hash": "22N0qOQgQTdqW+HURum5y+2b1v1Ky9vtgbintF3y0BU=",
"Source": ".",
"BasePath": "_content/.",
"Mode": "Default",
"ManifestType": "Build",
"ReferencedProjectsConfiguration": [],
"DiscoveryPatterns": [],
"Assets": [],
"Endpoints": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Project>
<Import Project="Microsoft.AspNetCore.StaticWebAssetEndpoints.props" />
<Import Project="Microsoft.AspNetCore.StaticWebAssets.props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<Import Project="..\build\..props" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<Import Project="..\buildMultiTargeting\..props" />
</Project>
Loading