61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using TestApplication.Data;
|
|
using TestApplication.DTO;
|
|
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace TestApplication.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class StudentsController : ControllerBase
|
|
{
|
|
private readonly Context _context;
|
|
|
|
public StudentsController(Context context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<StudentDTO>>> GetStudents()
|
|
{
|
|
var student = from students in _context.Student
|
|
join student_descriptions in _context.Student_Description on students.id equals student_descriptions.students_id
|
|
select new StudentDTO
|
|
{
|
|
Student_id = students.id,
|
|
Student_grade = students.grade,
|
|
};
|
|
|
|
return await student.ToListAsync();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public ActionResult<StudentDTO> GetStudent_byId(int id)
|
|
{
|
|
var student = from students in _context.Student
|
|
join student_descriptions in _context.Student_Description on students.id equals student_descriptions.students_id
|
|
select new StudentDTO
|
|
{
|
|
Student_id = students.id,
|
|
Student_grade = students.grade,
|
|
Age = student_descriptions.age,
|
|
First_name = student_descriptions.first_name,
|
|
Last_name = student_descriptions.last_name,
|
|
Address = student_descriptions.address,
|
|
Country = student_descriptions.country
|
|
};
|
|
|
|
var student_by_id = student.ToList().Find(x => x.Student_id == id);
|
|
|
|
if (student_by_id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return student_by_id;
|
|
}
|
|
}
|
|
} |