add students controller
This commit is contained in:
parent
0629e702ce
commit
67128aedca
|
|
@ -0,0 +1,61 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using TestApplication.Models;
|
||||
|
||||
namespace TestApplication.DTO
|
||||
{
|
||||
public class StudentDTO
|
||||
{
|
||||
public int Student_id { get; set; }
|
||||
public string Student_grade { get; set; }
|
||||
public int Age { get; set; }
|
||||
public string First_name { get; set; }
|
||||
public string Last_name { get; set; }
|
||||
public string Address { get; set; }
|
||||
public string Country { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -9,5 +9,7 @@ namespace TestApplication.Data
|
|||
public DbSet<Values> Values {get; set;}
|
||||
public DbSet<Book> Book {get; set;}
|
||||
public DbSet<Book_description> Book_Description {get; set;}
|
||||
public DbSet<Student> Student {get; set;}
|
||||
public DbSet<Student_description> Student_Description { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models
|
||||
{
|
||||
public class Student
|
||||
{
|
||||
[Key]
|
||||
public int id { get; set; }
|
||||
public string grade { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TestApplication.Models
|
||||
{
|
||||
public class Student_description
|
||||
{
|
||||
[Key]
|
||||
public int id { get; set; }
|
||||
public int students_id { get; set; }
|
||||
public int age { get; set; }
|
||||
public string first_name { get; set; }
|
||||
public string last_name { get; set; }
|
||||
public string address { get; set; }
|
||||
public string country { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue