From 67128aedca38d7fa3089d33bf8f4a6cc8ef5495d Mon Sep 17 00:00:00 2001 From: Aurelien Rebourg Date: Thu, 4 Mar 2021 16:11:28 +0000 Subject: [PATCH] add students controller --- Controllers/StudentsController.cs | 61 +++++++++++++++++++++++++++++++ DTO/StudentDTO.cs | 15 ++++++++ Data/Context.cs | 2 + Models/Student.cs | 11 ++++++ Models/Student_description.cs | 16 ++++++++ TestApplication.csproj | 6 +-- 6 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 Controllers/StudentsController.cs create mode 100644 DTO/StudentDTO.cs create mode 100644 Models/Student.cs create mode 100644 Models/Student_description.cs diff --git a/Controllers/StudentsController.cs b/Controllers/StudentsController.cs new file mode 100644 index 0000000..e39cd08 --- /dev/null +++ b/Controllers/StudentsController.cs @@ -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>> 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 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; + } + } +} \ No newline at end of file diff --git a/DTO/StudentDTO.cs b/DTO/StudentDTO.cs new file mode 100644 index 0000000..243f997 --- /dev/null +++ b/DTO/StudentDTO.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Data/Context.cs b/Data/Context.cs index bc5e93b..709a2fe 100644 --- a/Data/Context.cs +++ b/Data/Context.cs @@ -9,5 +9,7 @@ namespace TestApplication.Data public DbSet Values {get; set;} public DbSet Book {get; set;} public DbSet Book_Description {get; set;} + public DbSet Student {get; set;} + public DbSet Student_Description { get; set; } } } \ No newline at end of file diff --git a/Models/Student.cs b/Models/Student.cs new file mode 100644 index 0000000..ef62cd6 --- /dev/null +++ b/Models/Student.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Models/Student_description.cs b/Models/Student_description.cs new file mode 100644 index 0000000..337169f --- /dev/null +++ b/Models/Student_description.cs @@ -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; } + } +} \ No newline at end of file diff --git a/TestApplication.csproj b/TestApplication.csproj index dbae294..35a7932 100644 --- a/TestApplication.csproj +++ b/TestApplication.csproj @@ -3,8 +3,8 @@ netcoreapp3.1 - - - + + + \ No newline at end of file