|
3 | 3 | using System.Linq; |
4 | 4 | using System.Net; |
5 | 5 | using System.Net.Http; |
| 6 | +using ASP.NET_Core.Models; |
6 | 7 | using ASP_NET_Core.Models; |
7 | 8 | using DevExtreme.AspNet.Data; |
8 | 9 | using DevExtreme.AspNet.Mvc; |
9 | 10 | using Microsoft.AspNetCore.Mvc; |
10 | | -using System.Text.Json; |
| 11 | +using Newtonsoft.Json; |
11 | 12 |
|
12 | 13 | namespace ASP_NET_Core.Controllers; |
13 | 14 |
|
| 15 | +[Route("api/[controller]/[action]")] |
14 | 16 | [Route("api/[controller]/[action]")] |
15 | 17 | public class SampleDataController: Controller { |
16 | | - |
17 | 18 | [HttpGet] |
18 | | - public object Get(DataSourceLoadOptions loadOptions) { |
19 | | - return DataSourceLoader.Load(SampleData.Orders, loadOptions); |
| 19 | + public object GetStudents(DataSourceLoadOptions loadOptions) { |
| 20 | + return DataSourceLoader.Load(SampleData.Students, loadOptions); |
20 | 21 | } |
21 | 22 |
|
22 | 23 | [HttpGet] |
23 | | - public object GetStudents(DataSourceLoadOptions loadOptions) |
24 | | - { |
25 | | - return DataSourceLoader.Load(SampleData.Students, loadOptions); |
| 24 | + public object GetStudentSubjects(DataSourceLoadOptions loadOptions) { |
| 25 | + return DataSourceLoader.Load(SampleData.StudentSubjects, loadOptions); |
26 | 26 | } |
27 | 27 |
|
28 | 28 | [HttpPost] |
29 | | - public IActionResult InsertStudent(string values) |
30 | | - { |
31 | | - var newStudent = JsonSerializer.Deserialize<Student>(values, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
32 | | - if (newStudent == null) return BadRequest(); |
| 29 | + public IActionResult InsertStudent(string values) { |
| 30 | + var newStudent = new Student(); |
| 31 | + JsonConvert.PopulateObject(values, newStudent); |
33 | 32 |
|
34 | 33 | newStudent.ID = SampleData.Students.Count() + 1; |
35 | 34 | SampleData.Students.Add(newStudent); |
36 | 35 |
|
37 | 36 | return Ok(newStudent); |
38 | 37 | } |
39 | 38 |
|
| 39 | + [HttpPost] |
| 40 | + public IActionResult InsertStudentSubject(string values) { |
| 41 | + var newStudentSubject = new StudentSubject(); |
| 42 | + JsonConvert.PopulateObject(values, newStudentSubject); |
| 43 | + |
| 44 | + newStudentSubject.ID = SampleData.StudentSubjects.Count() + 1; |
| 45 | + SampleData.StudentSubjects.Add(newStudentSubject); |
| 46 | + |
| 47 | + return Ok(newStudentSubject); |
| 48 | + } |
| 49 | + |
40 | 50 | [HttpPut] |
41 | | - public IActionResult UpdateStudent(int key, string values) |
42 | | - { |
| 51 | + public IActionResult UpdateStudent(int key, string values) { |
43 | 52 | var student = SampleData.Students.First(s => s.ID == key); |
44 | | - var updatedStudent = JsonSerializer.Deserialize<Student>(values, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); |
45 | | - if (updatedStudent == null) return BadRequest(); |
46 | | - |
47 | | - student.Name = updatedStudent.Name ?? student.Name; |
48 | | - student.Subjects = updatedStudent.Subjects ?? student.Subjects; |
| 53 | + JsonConvert.PopulateObject(values, student); |
49 | 54 |
|
50 | 55 | return Ok(student); |
51 | 56 | } |
52 | 57 |
|
| 58 | + [HttpPut] |
| 59 | + public IActionResult UpdateStudentSubject(int key, string values) { |
| 60 | + var studentSubject = SampleData.StudentSubjects.First(s => s.ID == key); |
| 61 | + JsonConvert.PopulateObject(values, studentSubject); |
| 62 | + |
| 63 | + return Ok(studentSubject); |
| 64 | + } |
| 65 | + |
53 | 66 | [HttpDelete] |
54 | | - public void DeleteStudent(int key) |
55 | | - { |
| 67 | + public void DeleteStudent(int key) { |
56 | 68 | var student = SampleData.Students.First(s => s.ID == key); |
57 | 69 | SampleData.Students.Remove(student); |
58 | 70 | } |
59 | 71 |
|
| 72 | + [HttpDelete] |
| 73 | + public void DeleteStudentSubject(int key) { |
| 74 | + var studentSubject = SampleData.StudentSubjects.First(s => s.ID == key); |
| 75 | + SampleData.StudentSubjects.Remove(studentSubject); |
| 76 | + } |
60 | 77 | } |
0 commit comments