Tartalomjegyzék

< Web API

.Net Core Web API készítés

Dolgozók tárolása

A projekt alapkönyvtárában létrehozok egy Employee.cs fájlt:

Employee.cs
namespace App01
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public string City { get; set; } = string.Empty;
        public double Salary { get; set; }
    }
}

Kontroller

Létrehozok egy kontrollert:

EmployeeControler.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace App01.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
 
    }
}

Létrehozom benne a Get() metódust:

EmployeeController.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace App01.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        [HttpGet]
        public async Task<ActionResult<List<Employee>>> Get()
        {
            var employees = new List<Employee>
            {
                new Employee{ 
                    Id=1, 
                    Name="Per Ede", 
                    City="Szeged", 
                    Salary=2830000
                }
            };
            return Ok(employees);
        }
    }
}

Elindítom a projektet, ha az még nem fut:

dotnet watch

A „dotnet run” is használható, de minden változtatáshoz újra kell indítani.

A watch is lebeszélhető az változás utáni újratöltésről:

dotnet watch --no-hot-reload

Swagger:

https://localhost:7040/swagger/

Az employees külön

Controllers/EmployeeController.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace App01.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
 
        private static List<Employee> employees = new List<Employee>
        {
            new Employee{ 
                Id=1, 
                Name="Per Ede", 
                City="Szeged", 
                Salary=2830000
            }
        };
 
        [HttpGet]
        public async Task<ActionResult<List<Employee>>> Get()
        {
            return Ok(employees);
        }
    }
}

Post metódus

        [HttpPost]
        public async Task<ActionResult<List<Employee>>> AddEmployee(Employee employee)
        {
            employees.Add(employee);
            return Ok(employees);
        }

API teljes

Controllers/EmployeeController.cs
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
 
namespace App01.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
 
        private static List<Employee> employees = new List<Employee>
        {
            new Employee{ 
                Id=1, 
                Name="Per Ede", 
                City="Szeged", 
                Salary=2830000
            },
            new Employee{ 
                Id=2, 
                Name="Pod Tamás", 
                City="Szolnok", 
                Salary=2549000
            }
 
        };
 
        [HttpGet]
        public async Task<ActionResult<List<Employee>>> Get()
        {
            return Ok(employees);
        }
 
        [HttpGet("{id}")]
        public async Task<ActionResult<List<Employee>>> Get(int id)
        {
            var employee = employees.Find(emp => emp.Id == id);
            if (employee == null) {
                return BadRequest("A dolgozó nem található");
            }
            return Ok(employee);
        }
 
        [HttpPost]
        public async Task<ActionResult<List<Employee>>> AddEmployee(Employee employee)
        {
            employees.Add(employee);
            return Ok(employees);
        }
 
        [HttpPut]
        public async Task<ActionResult<List<Employee>>> UpdateEmployee(Employee request)
        {
            var employee = employees.Find(emp => emp.Id == request.Id);
            if (employee == null) {
                return BadRequest("A dolgozó nem található");
            }            
            employee.Name = request.Name;
            employee.City = request.City;
            employee.Salary = request.Salary;
 
            return Ok(employees);
        }
 
        [HttpDelete("{id}")]
        public async Task<ActionResult<List<Employee>>> Delete(int id)
        {
            var employee = employees.Find(emp => emp.Id == id);
            if (employee == null) {
                return BadRequest("A dolgozó nem található");
            }
            employees.Remove(employee);
            return Ok(employee);
        }
 
    }
}