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
92 changes: 92 additions & 0 deletions StudentApi/Controllers/StudentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.AspNetCore.Mvc;
using Models;
using Services.Interfaces;

namespace Controllers;

[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
private readonly IStudentService _studentService;

public StudentController(IStudentService studentService)
{
_studentService = studentService;
}

// GET: api/student
[HttpGet]
public IActionResult GetAllStudents()
{
var allStudents = _studentService.GetAllStudents();

if (allStudents == null || allStudents.Count == 0)
{
return NotFound("No Student Found");
}

return Ok(allStudents);
}

// GET: api/student/{id}
[HttpGet("{id}")]
public IActionResult GetStudentById(long id)
{
var foundStudent = _studentService.GetStudentById(id);

if (foundStudent == null)
{
return NotFound($"Student {id} is not found");
}

return Ok(foundStudent);
}

// POST: api/student
[HttpPost]
public IActionResult AddStudent([FromBody] Student student)
{
if (string.IsNullOrWhiteSpace(student.Name) || string.IsNullOrWhiteSpace(student.Course))
{
return BadRequest("Name and course are required");
}

Student savedStudent = _studentService.AddStudent(student);
return Ok(savedStudent);
}

// PUT: api/student/{id}
[HttpPut("{id}")]
public IActionResult UpdateStudent(long id, [FromBody] Student student)
{
if (string.IsNullOrEmpty(student.Name) || string.IsNullOrEmpty(student.Course))
return BadRequest("Incomplete Fields!");

student.StudentId = id;


var updatedStudent = _studentService.UpdateStudent(student);

if (updatedStudent == null)
{
return NotFound($"Student {id} not found");
}

return Ok(updatedStudent);
}

// DELETE: api/student/{id}
[HttpDelete("{id}")]
public IActionResult DeleteStudent(long id)
{
var deleted = _studentService.DeleteStudent(id);

if (!deleted)
{
return NotFound($"Student {id} is not found");
}

return NoContent();
}
}
22 changes: 22 additions & 0 deletions StudentApi/Models/Student.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Models;

public class Student
{
public long StudentId { get; set; }
public string Name { get; set; } = null!;
public string Course { get; set; } = null!;

// Default constructor
public Student()
{
}

// Constructor with parameters
public Student(long studentId, string name, string course)
{
StudentId = studentId;
Name = name;
Course = course;
}

}
33 changes: 33 additions & 0 deletions StudentApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Models;
using Services.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

// For Dependency Injection (Uses a single instance of dictionary; which is the in-memory studentDb)
builder.Services.AddSingleton<IStudentService, StudentService>();

// For Controller
builder.Services.AddControllers();

var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())

{
app.MapOpenApi();

}

app.UseHttpsRedirection();
app.MapControllers();

app.Run();





23 changes: 23 additions & 0 deletions StudentApi/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:7118;http://localhost:9090",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
65 changes: 65 additions & 0 deletions StudentApi/Services/Implementations/StudentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Models;
using Services.Interfaces;

public class StudentService : IStudentService
{
// In-memory db
private readonly Dictionary<long, Student> studentsDB = new Dictionary<long, Student>();

long studentCount = 1;


public Student AddStudent(Student student)
{

if (student.StudentId == 0)
{
student.StudentId = studentCount++;
}

long studentId = student.StudentId;
studentsDB[studentId] = student;

return student;
}

public List<Student> GetAllStudents()
{
return studentsDB.Values.ToList();
}

public Student? GetStudentById(long id)
{
Student? foundStudent = studentsDB.TryGetValue(id, out Student? student) ? student : null;

if (foundStudent == null)
{
return null;
}

return foundStudent;
}

public Student? UpdateStudent(Student student)
{
long studentId = student.StudentId;

if (!studentsDB.ContainsKey(studentId))
{
return null;
}

if (string.IsNullOrWhiteSpace(student.Name) || string.IsNullOrWhiteSpace(student.Course))
{
return null;
}

studentsDB[studentId] = student;
return student;
}

public bool DeleteStudent(long id)
{
return studentsDB.Remove(id);
}
}
15 changes: 15 additions & 0 deletions StudentApi/Services/Interfaces/IStudentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Models;

namespace Services.Interfaces;

public interface IStudentService
{
Student AddStudent(Student student);
List<Student> GetAllStudents();
Student? GetStudentById(long id);

Student? UpdateStudent(Student student);
bool DeleteStudent(long id);


}
13 changes: 13 additions & 0 deletions StudentApi/StudentApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions StudentApi/StudentApi.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@StudentApi_HostAddress = http://localhost:5189

GET {{StudentApi_HostAddress}}/weatherforecast/
Accept: application/json

###
8 changes: 8 additions & 0 deletions StudentApi/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 StudentApi/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.
59 changes: 59 additions & 0 deletions StudentApi/bin/Debug/net9.0/StudentApi.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v9.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v9.0": {
"StudentApi/1.0.0": {
"dependencies": {
"Microsoft.AspNetCore.OpenApi": "9.0.2"
},
"runtime": {
"StudentApi.dll": {}
}
},
"Microsoft.AspNetCore.OpenApi/9.0.2": {
"dependencies": {
"Microsoft.OpenApi": "1.6.17"
},
"runtime": {
"lib/net9.0/Microsoft.AspNetCore.OpenApi.dll": {
"assemblyVersion": "9.0.2.0",
"fileVersion": "9.0.225.6704"
}
}
},
"Microsoft.OpenApi/1.6.17": {
"runtime": {
"lib/netstandard2.0/Microsoft.OpenApi.dll": {
"assemblyVersion": "1.6.17.0",
"fileVersion": "1.6.17.0"
}
}
}
}
},
"libraries": {
"StudentApi/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Microsoft.AspNetCore.OpenApi/9.0.2": {
"type": "package",
"serviceable": true,
"sha512": "sha512-JUndpjRNdG8GvzBLH/J4hen4ehWaPcshtiQ6+sUs1Bcj3a7dOsmWpDloDlpPeMOVSlhHwUJ3Xld0ClZjsFLgFQ==",
"path": "microsoft.aspnetcore.openapi/9.0.2",
"hashPath": "microsoft.aspnetcore.openapi.9.0.2.nupkg.sha512"
},
"Microsoft.OpenApi/1.6.17": {
"type": "package",
"serviceable": true,
"sha512": "sha512-Le+kehlmrlQfuDFUt1zZ2dVwrhFQtKREdKBo+rexOwaCoYP0/qpgT9tLxCsZjsgR5Itk1UKPcbgO+FyaNid/bA==",
"path": "microsoft.openapi/1.6.17",
"hashPath": "microsoft.openapi.1.6.17.nupkg.sha512"
}
}
}
Binary file added StudentApi/bin/Debug/net9.0/StudentApi.dll
Binary file not shown.
Binary file added StudentApi/bin/Debug/net9.0/StudentApi.exe
Binary file not shown.
Binary file added StudentApi/bin/Debug/net9.0/StudentApi.pdb
Binary file not shown.
19 changes: 19 additions & 0 deletions StudentApi/bin/Debug/net9.0/StudentApi.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"runtimeOptions": {
"tfm": "net9.0",
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "9.0.0"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "9.0.0"
}
],
"configProperties": {
"System.GC.Server": true,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Version": 1,
"ManifestType": "Build",
"Endpoints": []
}
8 changes: 8 additions & 0 deletions StudentApi/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"
}
}
}
Loading