[[oktatas:programozas:csharp:dotnetcore:web_api|< Web API]]
====== Dotnet Core - Egyszerű REST API ======
* **Szerző:** Sallai András
* Copyright (c) 2024, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Bevezetés =====
Egyszerű REST API készítés dotnet használatával, kontroller nélkül.
* https://github.com/oktat/emp_api_dotnet.git
Telepítéshez Nuget csomagokat használunk:
* https://www.nuget.org/
===== Projekt készítése =====
dotnet new webapi -minimal -o app01
===== Függőség telepítése =====
dotnet add package Pomelo.EntityFrameworkCore.MySql
===== Könyvtárszerkezet =====
app01/
|-bin
|-database/
| `-create.sql
|-obj/
|-Properties/
|-app01.csproj
|-app01.sln
|-appsettings.Development.json
|-appsettings.json
|-DataService.cs
|-Emplyoee.cs
`-Program.cs
===== Fejlesztői szerver =====
dotnet watch run
===== Adatbázis =====
create database if not exists sargabt;
use sargabt;
create table if not exists employees (
Id int not null primary key auto_increment,
Name varchar(50),
City varchar(50),
Salary double
);
grant all privileges
on sargabt.*
to sargabt@localhost
identified by 'titok';
===== Kapcsolódás az adatbázishoz =====
using Microsoft.EntityFrameworkCore;
class DataService : DbContext {
string str = "Server=localhost; User ID=sargabt; Password=titok; Database=sargabt";
public DbSet Employees { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder ob)
{
ob.UseMySql(str, ServerVersion.AutoDetect(str));
}
}
===== Modell =====
public class Employee {
public int Id { get; set; }
public string Name { get; set; } = "";
public string City { get; set; } = "";
public double Salary { get; set; }
}
===== Routing =====
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var db = new DataService();
app.MapGet("/employees", () => {
var emps = db.Employees.ToList();
return emps;
});
app.MapPost("/employees", (Employee emp) => {
db.Employees.Add(emp);
db.SaveChanges();
return Results.Created(@"/employeeitems/{emp.Id}", emp);
});
app.MapPut("/employees/{id}", (int id, Employee inputEmp) => {
var emp = db.Employees.Find(id);
if (emp is null ) return Results.NotFound();
emp.Name = inputEmp.Name;
emp.City = inputEmp.City;
emp.Salary = inputEmp.Salary;
db.SaveChanges();
return Results.NoContent();
});
app.MapDelete("/employees/{id}", (int id) => {
if(db.Employees.Find(id) is Employee emp) {
db.Employees.Remove(emp);
db.SaveChanges();
return Results.NoContent();
}
return Results.NoContent();
});
app.Run();
===== Végpontok =====
Teles végpont példa:
* localhost:5216/employees
^ Végpont ^ Metódus ^ Auth ^ Leírás ^
| /employees | GET | nem | Az összes dolgozó lekérése |
| /employees | POST | nem | Új dolgozó hozzáadása |
| /employees/{id} | PUT | nem | Dolgozó adatainak frissítése |
| /employees/{id} | DELETE | nem | Dolgozó törlése |
===== Swagger hozzáadása =====
dotnet add package Swashbuckle.AspNetCore
==== Vázlat ====
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.MapGet("/msg", () => "működik");
app.UseSwaggerUI();
app.Run();
==== Kód ====
A "var app = builder.Build(); előtt:
var services = builder.Services;
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
A "var db = new DataService();" után:
app.UseSwagger();
Az "app.Run();" előtt:
app.UseSwaggerUI();
==== Teljes kód ====
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
var app = builder.Build();
var db = new DataService();
app.UseSwagger();
app.MapGet("/employees", () => {
var emps = db.Employees.ToList();
return emps;
});
app.MapPost("/employees", (Employee emp) => {
db.Employees.Add(emp);
db.SaveChanges();
return Results.Created(@"/employeeitems/{emp.Id}", emp);
});
app.MapPut("/employees/{id}", (int id, Employee inputEmp) => {
var emp = db.Employees.Find(id);
if (emp is null ) return Results.NotFound();
emp.Name = inputEmp.Name;
emp.City = inputEmp.City;
emp.Salary = inputEmp.Salary;
db.SaveChanges();
return Results.NoContent();
});
app.MapDelete("/employees/{id}", (int id) => {
if(db.Employees.Find(id) is Employee emp) {
db.Employees.Remove(emp);
db.SaveChanges();
return Results.NoContent();
}
return Results.NoContent();
});
app.UseSwaggerUI();
app.Run();
===== Források =====
* https://learn.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-8.0 (2024)
* https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-8.0 (2024)
* https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-8.0&tabs=visual-studio-code (2024)